package com.knutejohnson.demos; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class Shapes2 extends Canvas implements Runnable { final Random r = new Random(new Date().getTime()); private volatile Thread thread; public void start() { thread = new Thread(this); thread.start(); } public void stop() { thread.interrupt(); } public void run() { while (!Thread.interrupted()) repaint(); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { int x,y,w,h; int width = getWidth(); int height = getHeight(); x = r.nextInt(width * 2) - width; y = r.nextInt(height * 2) - height; w = r.nextInt(width / 2); h = r.nextInt(height / 2); g.setColor(new Color(r.nextInt(16777215))); g.fillOval(x,y,w,h); x = r.nextInt(width * 2) - width; y = r.nextInt(height * 2) - height; w = r.nextInt(width / 2); h = r.nextInt(height / 2); g.setColor(new Color(r.nextInt(16777215))); g.fillRect(x,y,w,h); } public static void main(String[] args) { Frame f = new Frame("Shapes2"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); Shapes2 shapes = new Shapes2(); shapes.setPreferredSize(new Dimension(640,480)); f.add(shapes,BorderLayout.CENTER); f.pack(); f.setVisible(true); shapes.start(); } }