The Java Game Framework.

Examples, tutorials, documentation and download of the Genuts Framework.
Games made with the Genuts Frameworks.
General articles around games.
Links covering all needs around game dev.
Who are we?
Terms of Service

API >> Tutorials >> Interfacing Genuts Framework and Java API >>Displaying the current hour with a sprite

Displaying the current hour with a sprite

In the following example, we are using a SpriteLabel with 2 SpriteWrappers: one to display the current hour, and the second to move the sprite.

SpriteLabel

The SpriteLabel is only a sprite for placing text in the playfield. There is nothing special to say, except that this sprite is a background sprite by default.

TimeWrapper

The goal of the TimeWrapper is to change the text of a SpriteLabel with the desired information. Therefore, we need 3 states:
  • Displays the 24-hour clock format
  • Displays the 12-hour clock format
  • Displays the date
To construct the new text of the SpriteLabel, we use java.text.SimpleDateFormat and java.util.Calendar from the java API. See Java API documentation for more details. By default, we will display the 24-hour clock format. Here is the code needed to set different values:


...
  public void tick(int ticks) {
    if ((ticks % (1000/getParent().getSleepTime()) == 0) ||
	(((SpriteLabel) getActionSprite()).getText() == "")) {
      SimpleDateFormat formatter = null;
      switch (currentFormat) {
      case DISPLAY_24HOURS:
	formatter = new SimpleDateFormat ("HH:mm:ss");
	break;
      case DISPLAY_AM_PMHOURS:
	formatter = new SimpleDateFormat ("a KK:mm:ss");
	break;
      case DISPLAY_DATE:
	formatter = new SimpleDateFormat ("yyyy/MM/dd");
	break;
      }
      String value = formatter.format(Calendar.getInstance().getTime());
      ((SpriteLabel) getActionSprite()).setText(value);
    }
  }
...

You can notice that we don't set a new value on each tick; we wait around one second between each new setting.

MovingWrapper

This MovingWrapper was taken from the Fundamentals tutorial.

But, the size of the SpriteLabel can change between 2 locations; we need to modify our wrapper to consider this possibility during a collision with borders. Here our modifications are:


...
  /**
   * Receives the event that this sprite is in collision
   * with another sprite
   * @param s Sprite with which the collision is
   */
  public void collisionWith(Sprite s) {
    if (s == null) {
      if ((getX() < 0) || ((getX() + getWidth()) > getParent().getWidth())) {
	xspeed *= -1;
	getFinalWrapper().setPosition(Math.min(getX() + xspeed,
					   getParent().getWidth() - getWidth() - 1),
				   getY());
      }
      if ((getY() < 0) || ((getY() + getHeight()) > getParent().getHeight())) {
	yspeed *= -1;
	getFinalWrapper().setPosition(getX(),
				   Math.min(getY() + yspeed,
					   getParent().getHeight() - getHeight() - 1));
      }
    }
    super.collisionWith(s);
  }
...

We composed all these components in the applet BaseTimeApplet.java.

<< Previous Page Next Page >>

API >> Tutorials >> Interfacing Genuts Framework and Java API >>Displaying the current hour with a sprite