1  import java.awt.*;
  2  import java.awt.event.*;
  3  import javax.swing.*;
  4  
  5  /**
  6     This program implements an animation that moves
  7     a car shape.
  8  */
  9  public class AnimationTester
 10  {
 11     public static void main(String[] args)
 12     {
 13        JFrame frame = new JFrame();
 14  
 15        MoveableShape shape = new CarShape(0, 0, CAR_WIDTH);
 16  
 17        ShapeIcon icon = new ShapeIcon(shape,
 18              ICON_WIDTH, ICON_HEIGHT);
 19  
 20        JLabel label = new JLabel(icon);
 21        frame.setLayout(new FlowLayout());
 22        frame.add(label);
 23  
 24        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 25        frame.pack();
 26        frame.setVisible(true);
 27  
 28        final int DELAY = 100;
 29        // Milliseconds between timer ticks
 30        Timer t = new Timer(DELAY, event ->
 31           {
 32              shape.move();
 33              label.repaint();
 34           });
 35        t.start();
 36     }
 37  
 38     private static final int ICON_WIDTH = 400;
 39     private static final int ICON_HEIGHT = 100;
 40     private static final int CAR_WIDTH = 100;
 41  }