Source of AnimationTester.java


  1: import java.awt.*;
  2: import java.awt.event.*;
  3: import javax.swing.*;

  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();

 15:       final MoveableShape shape
 16:             = new CarShape(0, 0, CAR_WIDTH);

 18:       ShapeIcon icon = new ShapeIcon(shape,
 19:             ICON_WIDTH, ICON_HEIGHT);

 21:       final JLabel label = new JLabel(icon);
 22:       frame.setLayout(new FlowLayout());
 23:       frame.add(label);

 25:       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 26:       frame.pack();
 27:       frame.setVisible(true);

 29:       final int DELAY = 100;
 30:       // Milliseconds between timer ticks
 31:       Timer t = new Timer(DELAY, new
 32:          ActionListener()
 33:          {
 34:             public void actionPerformed(ActionEvent event)
 35:             {
 36:                shape.translate(1, 0);
 37:                label.repaint();
 38:             }
 39:          });
 40:       t.start();
 41:    }

 43:    private static final int ICON_WIDTH = 400;
 44:    private static final int ICON_HEIGHT = 100;
 45:    private static final int CAR_WIDTH = 100;
 46: }