//CS5-HMC-Sp.,00-HW10-1 //A. Khakpour, Apr., 00 import java.applet.*; import java.awt.*; // An applet to perform a simple animation. public class Drive1 extends Applet { private final int MOVE = 2; private final int RIGHT_SIDE = 450; private final int NUM_POINTS = 10; private final int PAUSE = 50000; private int[] car_x = {80, 70, 70, 90, 95, 140, 150, 175, 180, 170}; private int[] car_y = {190, 190, 170, 170, 155, 155, 170, 170, 190, 190}; private int[] car_x_update = new int[NUM_POINTS]; // Performs the animation. public void paint (Graphics g) { g.setXORMode(getBackground()); // Loop until the car drives off the right side. for (int offset=1; offset < RIGHT_SIDE; offset += MOVE) { draw_car(offset, g); // draw the car for (int pause=1; pause < PAUSE; pause++); draw_car(offset, g); // erase the car } } // Draws the car at a particular x offset. public void draw_car(int offset, Graphics g) { // Update x coordinates for (int scan = 0; scan < NUM_POINTS; scan++) car_x_update[scan] = car_x[scan] + offset; g.setColor(Color.red); g.drawPolyline(car_x_update, car_y, 10); g.drawArc(80+offset, 180, 20, 20, 0, 180); g.drawArc(150+offset, 180, 20, 20, 0, 180); g.drawLine(100+offset, 190, 150+offset, 190); g.setColor(Color.blue); g.drawOval(83+offset, 183, 14, 14); g.drawOval(153+offset, 183, 14, 14); g.drawLine(85+offset, 170, 85+offset, 140); } }