import java.applet.Applet; import java.awt.Graphics; public class Circle extends Thread { private int x, y, radius=15; private double dx = 2; private double dy = 2; private int width, height; private Applet parent; //------------------------------------------------------- public Circle( Applet parent ) { this.parent = parent; //width = parent.getSize().width; //height = parent.getSize().height; width = parent.size().width; height = parent.size().height; x = radius + (int)Math.floor( (width - 2 * radius) * Math.random() ); y = radius + (int)Math.floor( (height - 2 * radius) * Math.random() ); } public void draw( Graphics g ) { g.fillOval( x, y, radius *2, radius * 2 ); } public void move() { // Change position of Circle x += dx; y += dy; if (x<0) { x=1; dx=-dx; } if (x + 2*radius >= width) { x -= dx; dx=-dx; } if (y<0) { y=1; dy=-dy; } if (y + 2*radius >= height) { y -= dy; dy=-dy; } //System.out.println("Circle at: ("+ x + ", "+ y + ") "); } public void run() { while ( true ) { parent.repaint(); try { Thread.sleep( 100 ); } catch ( InterruptedException e ) {} } } }