import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class Ball extends Applet implements Runnable { int frameNumber = -1; int delay; Thread animatorThread; public void init() { String str; int fps = 10; //How many milliseconds between frames? str = getParameter("fps"); try { if (str != null) { fps = Integer.parseInt(str); } } catch (Exception e) {} delay = (fps > 0) ? (1000 / fps) : 100; } public void start() { //Start animating! if (animatorThread == null) { animatorThread = new Thread(this); } animatorThread.start(); } public void stop() { //Stop the animating thread. animatorThread = null; animatorThread = null; } public void run() { //Just to be nice, lower this thread's priority //so it can't interfere with other processing going on. Thread.currentThread().setPriority(Thread.MIN_PRIORITY); //Remember which thread we are. Thread currentThread = Thread.currentThread(); //This is the animation loop. while (currentThread == animatorThread) { //Advance the animation frame. frameNumber++; //Display it. repaint(); // Delay try { Thread.sleep(delay); } catch (InterruptedException e) { break; } } } public void paint(Graphics g) { setBackground(Color.white); g.setColor(Color.red); Dimension d = getSize(); if (frameNumber % 2 == 1) g.fillOval(0, 0, d.width/2, d.height/2); else g.fillOval(0, d.height/2, d.width/2, d.height/2); } }