1 import java.awt.event.ActionEvent;
2 import java.awt.event.ActionListener;
3 import javax.swing.JFrame;
4 import javax.swing.Timer;
5
6 /**
7 This program moves the rectangle.
8 */
9 public class RectangleMover
10 {
11 private static final int FRAME_WIDTH = 300;
12 private static final int FRAME_HEIGHT = 400;
13
14 public static void main(String[] args)
15 {
16 JFrame frame = new JFrame();
17
18 frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
19 frame.setTitle("An animated rectangle");
20 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21
22 final RectangleComponent component = new RectangleComponent();
23 frame.add(component);
24
25 frame.setVisible(true);
26
27 class TimerListener implements ActionListener
28 {
29 public void actionPerformed(ActionEvent event)
30 {
31 component.moveBy(1, 1);
32 }
33 }
34
35 ActionListener listener = new TimerListener();
36
37 final int DELAY = 100; // Milliseconds between timer ticks
38 Timer t = new Timer(DELAY, listener);
39 t.start();
40 }
41 }