Java 7 for Absolute Beginners

(nextflipdebug5) #1
CHAPTER 10 ■ ANIMATION

if (animating) {
g.setColor(Color.RED);
g.fillOval(xPosition, height / 2, 10 ,10);
xPosition += 20;
if (xPosition > width) {
animating = false;
}
}
}


// The run method controls how often the screen
// gets redrawn and so controls the frame rate
public void run() {
while(animating) {
try {
Thread.sleep(40);
this.repaint();
} catch (InterruptedException ie) {
return;
}
}
}
}


Those two classes create a program that looks like the image in Figure 10-1.

Figure 10-1. ScootBall starting state

Free download pdf