001package horstmann.ch04_animation;
002import java.awt.FlowLayout;
003import javax.swing.JFrame;
004import javax.swing.JLabel;
005import javax.swing.Timer;
006
007/**
008   This program implements an animation that moves
009   a car shape.
010 */
011public class AnimationTester
012{
013        public static void main(String[] args)
014        {
015                JFrame frame = new JFrame();
016
017                final MoveableShape shape
018                = new CarShape(0, 0, CAR_WIDTH);
019
020                ShapeIcon icon = new ShapeIcon(shape,
021                                ICON_WIDTH, ICON_HEIGHT);
022
023                final JLabel label = new JLabel(icon);
024                frame.setLayout(new FlowLayout());
025                frame.add(label);
026
027                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
028                frame.pack();
029                frame.setVisible(true);
030
031                final int DELAY = 100;
032                // Milliseconds between timer ticks
033                Timer t = new Timer(DELAY, event -> {
034                        shape.translate(1, 0);
035                        label.repaint();
036                });
037                t.start();
038        }
039
040        private static final int ICON_WIDTH = 400;
041        private static final int ICON_HEIGHT = 100;
042        private static final int CAR_WIDTH = 100;
043}