import java.awt.Component; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Frame; import java.awt.Button; import java.awt.Panel; import java.awt.BorderLayout; import java.awt.event.WindowEvent; import java.awt.event.WindowAdapter; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.*; public class Ringid { public static void main(String args[]) { final Frame frame = new Frame ("Ilustus ringid"); final Button btnStart = new Button("Start"); final Panel pnlTarget = new Panel (); frame.add(btnStart, BorderLayout.NORTH ); frame.add(pnlTarget, BorderLayout.CENTER); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Thread painter = new Thread(new Runnable() { public void run() { try { while (true) { Thread circle = new CircleThread(pnlTarget); circle.start(); Thread.sleep(100); } } catch (InterruptedException e) {} } }); painter.start(); btnStart.setEnabled(false); } }); frame.setSize(500, 500); frame.setVisible(true); } } //---------------------------------------------------------------------------- // class CircleThread //---------------------------------------------------------------------------- class CircleThread extends Thread { private int x; private int y; private int maxDiameter; private Graphics graphics; public CircleThread(Component target) { Dimension size = target.getSize(); x = (int)(Math.random() * size.width ); y = (int)(Math.random() * size.height); maxDiameter = Math.min(size.width, size.height); graphics = target.getGraphics(); } public void run() { int red = (int) (Math.random() * 255); int green = (int) (Math.random() * 255); int blue = (int) (Math.random() * 255); long pause = (long)(Math.random() * 10); Color color = new Color(red, green, blue); graphics.setColor(color); try { for (int diameter = 1; diameter < maxDiameter; diameter += 2, x--, y--) { graphics.drawOval(x, y, diameter, diameter); Thread.sleep(pause); } } catch (InterruptedException e) {} } }