DrivePanel - animate.addActionListener(new Animator ()); }

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
//******************************************************************** // DrivePanel.java Author: Lewis and Loftus // // Solution to Programming Project 8.16 //******************************************************************** / import javax.swing.*; import java.awt.*; import java.awt.event.*; i public class DrivePanel extends JPanel { private Car car; private Timer animate; private int xOff = 20; private static final int DELAY = 50; // 20 frames per second private static final int INITIAL_X = 20, INITIAL_Y = 50, RESTART_POS = -100; //----------------------------------------------------------------- // Sets up the panel //----------------------------------------------------------------- public DrivePanel(Timer timer) { car = new Car (INITIAL_X, INITIAL_Y, Color.red); animate = timer;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: animate.addActionListener(new Animator ()); } //-----------------------------------------------------------------// Draws the car //-----------------------------------------------------------------public void paintComponent (Graphics page) { super.paintComponent(page); car.draw (page); } //***************************************************************** // Inner class that listens for the timer to move the car and // repaint the screen during the animation. //***************************************************************** private class Animator implements ActionListener { public void actionPerformed (ActionEvent event) { xOff += 3; if (xOff > getWidth()) xOff = RESTART_POS; car.setPosition (xOff, 50); repaint (); } } }...
View Full Document

Ask a homework question - tutors are online