import java.awt.Graphics; import java.util.Date; import java.applet.Applet; public abstract class Sprite { // the screen position of the sprite protected double xpos = 0.0; protected double ypos = 0.0; // the velocity of the sprite protected int xvel = 0; protected int yvel = 0; // the dimentions of the sprite protected int xdim = 0; protected int ydim = 0; // the last time the position was calculated long lastupdate = 0; // class variables // the applet and its dimentions protected static Applet applet; protected static int appwidth = 0; protected static int appheight = 0; // abstract methods: must be // implemented by sub-classes abstract void paint(Graphics g); // constructor public Sprite(Applet a) { appwidth = a.getSize().width; appheight = a.getSize().height; } // calculate my new position public void update() { if (lastupdate == 0) { lastupdate = new Date().getTime(); } long thisupdate = new Date().getTime(); long timediff = (thisupdate - lastupdate) / 10L; lastupdate = thisupdate; // boundaries for horizontal movement if ( (xpos <= 0 && xvel < 0) || ((xpos >= appwidth - xdim) && xvel > 0) ) { xvel = -xvel; } xpos += (double)(xvel*timediff); // boundaries for vertical movement if ( (ypos <= 0 && yvel < 0) || ((ypos >= appheight - ydim) && yvel > 0) ) { yvel = -yvel; } ypos += (double)(yvel*timediff); } // useful flags protected boolean is_visible = false; protected boolean is_active = false; // accessor methods }