// <applet code="OkAnimation" width="300" height="300"></applet>
// use: >appletviewer OkAnimation.java
import java.applet.*;
import java.awt.*;
public class OkAnimation extends Applet implements Runnable
{
int y;
int dy = 10;
Thread t = null;
boolean isRunning = false;
/** Applet method override */
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.fillOval(100, y, 50, 50);
}
/** Applet method override */
public void start()
{
if(!isRunning)
{
isRunning = true;
t = new Thread(this);
t.start();
}
}
/** Applet method override */
public void stop()
{
isRunning = false;
if(t != null)
t.interrupt(); // wake up
t = null;
}
/** Runnable interface implementation */
public void run()
{
while(isRunning)
{
try
{
t.sleep( 1000 );
}
catch(InterruptedException e)
{
stop();
}
if(y + dy < 0 || y + 50 + dy > getHeight()) {
// if the next move puts us out of bounds
// reverse direction.
dy *= -1;
}
y = y+dy;
repaint();
}
}
}