01: import java.awt.*; 02: import java.awt.event.*; 03: import javax.swing.*; 04: 05: /** 06: This program implements an animation that moves 07: a car shape. 08: */ 09: public class AnimationTest 10: { 11: public static void main(String[] args) 12: { 13: JFrame frame = new JFrame(); 14: 15: final MoveableShape shape 16: = new CarShape(0, 0, CAR_WIDTH); 17: 18: ShapeIcon icon = new ShapeIcon(shape, 19: ICON_WIDTH, ICON_HEIGHT); 20: 21: final JLabel label = new JLabel(icon); 22: Container contentPane = frame.getContentPane(); 23: contentPane.setLayout(new FlowLayout()); 24: contentPane.add(label); 25: 26: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 27: frame.pack(); 28: frame.show(); 29: 30: final int DELAY = 100; 31: // milliseconds between timer ticks 32: Timer t = new Timer(DELAY, new 33: ActionListener() 34: { 35: public void actionPerformed(ActionEvent event) 36: { 37: shape.translate(1, 0); 38: label.repaint(); 39: } 40: }); 41: t.start(); 42: } 43: 44: private static final int ICON_WIDTH = 400; 45: private static final int ICON_HEIGHT = 100; 46: private static final int CAR_WIDTH = 100; 47: }