/* * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; /** * */ public class TimerBasedPsychodelic { /** * entry point, create an instance and pass on to createAndShowGUI() */ public static void main (String[] args) { final TimerBasedPsychodelic p = new TimerBasedPsychodelic(); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { p.createAndShowGUI(); } }); int delay = Math.max(1,Integer.parseInt(args[0])); int seed = (args.length > 1) ? Integer.parseInt(args[1]) : 0; int numIterations = (args.length > 2) ? Integer.parseInt(args[2]) : Integer.MAX_VALUE; p.go(delay,seed,numIterations); } public void go(int delay, int seed, int numIterations) { final Random r = new Random(seed); final Color[] colors = new Color[]{Color.BLACK,Color.BLUE,Color.CYAN,Color.DARK_GRAY, Color.GRAY,Color.GREEN,Color.LIGHT_GRAY, Color.MAGENTA,Color.ORANGE,Color.PINK, Color.RED,Color.WHITE,Color.YELLOW}; _counter = numIterations; ActionListener createNextCircle = new ActionListener() { public void actionPerformed(ActionEvent evt) { if (_counter-- < 0) { _timer.stop(); } else if (_frame != null) { _x = r.nextInt(Math.max(1,_frame.getWidth())); _y = r.nextInt(Math.max(1,_frame.getHeight())); _radius = 20 + r.nextInt(30); _color = colors[r.nextInt(colors.length)]; _frame.repaint(); } } }; _timer = new javax.swing.Timer(delay,createNextCircle); _timer.start(); } private JFrame _frame; /** * sets up everything, and then makes Frame visible */ public void createAndShowGUI() { //Create and set up the window. _frame = new JFrame("TimerBasedPsychodelic") { public void paint(Graphics g) { g.setColor(_color); g.fillOval(_x-_radius,_y-_radius,2*_radius,2*_radius); } }; _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); _frame.setPreferredSize(new Dimension(400,400)); _frame.pack(); _frame.setVisible(true); } private int _x = 0; private int _y = 0; private int _radius = 0; private Color _color = null; private int _counter = 0; private javax.swing.Timer _timer; }