import javax.swing.*; import java.util.*; import java.awt.*; import java.io.*; public class Roller { private Circle circle; private Color color; private JFrame w; Random random; public Roller() { int r = 20; Point p = new Point(100, 100); circle = new Circle(r, p); color = Color.RED; w = new JFrame("Rolling around"); w.setSize(250, 250); w.setVisible(true); w.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); random = new Random(); } public void roll() { erase(); Point here = circle.getCenter(); int x = here.x; int y = here.y; int deltax = random.nextInt(31) - 15; int deltay = random.nextInt(31) - 15; Point there = new Point( x + deltax, y + deltay ); circle.setCenter(there); paint(); pause(); } public static void pause() { try { (new Thread()).sleep(500); } catch (Exception e) { return; } } public void erase() { Graphics g = w.getGraphics(); Color background = w.getBackground(); g.setColor( background ); g.fillOval( circle.getCenter().x, circle.getCenter().y, circle.getRadius(), circle.getRadius() ); } public void paint() { Graphics g = w.getGraphics(); g.setColor( this.color ); g.fillOval( circle.getCenter().x, circle.getCenter().y, circle.getRadius(), circle.getRadius() ); } public static void main(String[] args) throws IOException { Roller ball = new Roller( ); System.out.println("Hit enter when ready"); System.in.read(); while ( true) { ball.roll(); } } }