// // CORRECT: the animation is performed in a separate // thread, which will be started by actionPerformed() // of the mouse listener // // rule of thumb: listener methods should not perform // substantial computation, and should not sleep(). // Start separate threads instead. // import java.awt.*; import java.awt.event.*; public class SleepExample extends Frame { private Rectangle position; private static SleepExample ex; private SleepExample () { setSize(400, 400); setTitle("Sleep Example"); position = new Rectangle(150, 0, 100, 100); Button button = new Button("Fall"); button.addActionListener(new ExampleActionListener()); add("North", button); } private class ExampleActionListener implements ActionListener { public void actionPerformed (ActionEvent e) { (new FallingBallThread()).start(); } } class FallingBallThread extends Thread { public void run() { try { position = new Rectangle(150, 0, 100, 100); while (position.y < 400) { position.y = position.y + 10; System.out.println("Position.y: " + position.y); ex.test(); Thread.sleep(10); } } catch (Exception ex) { System.out.println("Exception: " + ex); } } } public void test () { System.out.println("TEST!"); repaint(); } public void paint (Graphics g) { System.out.println("PAINT!"); g.setColor(Color.blue); g.fillOval(position.x, position.y, position.width, position.height); } public static void main (String[] args) { ex = new SleepExample(); ex.show(); } }