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:
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 */publicvoidcollisionWith(Sprites){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);}...