Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: 1 thing missing in car animation
|
Posted: Mar 13, 2002 1:41 PM
|
|
Your so close and its a nice program. you need to add one array element to the array car_x
change this line
private int[] car_x = {80,70,70,95,140,150,175,180,170};
to:
private int[] car_x = {80,70,70,95,140,150,175,180,170,170};
and add the follwing to your paint method: public void draw_car (int offset, Graphics page) {
page.setColor (Color.white); page.fillRect(1,1,449,249);
.....
import java.applet.Applet; import java.awt.*;
// An applet to perform a simple animation.
public class Drive extends Applet{
private Graphics page; private Car car;
//sets up the animation. public void init() { setVisible (true); setSize (450,250);
car = new Car();
page = getGraphics(); page.setXORMode (getBackground());
}//method init
public void start(){
car.drive(getGraphics());
}//method start
}//class Drive
//Represents the car that moves across the screen.
class Car {
private final int MOVE = 2; private final int RIGHT_SIDE = 450; private final int NUM_POINTS = 10; private final int PAUSE = 100000;
//private int[] car_x = {80,70,70,95,140,150,175,180,170}; private int[] car_x = {80,70,70,95,140,150,175,180,170,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 drive (Graphics page) { //Loop until car drives off the right side. for (int offset=1; offset < RIGHT_SIDE; offset += MOVE) { draw_car (offset,page); //draw the car for (int pause=1; pause < PAUSE; pause++); draw_car (offset,page); //erase the car } //private int[] car_x = {80,70,70,95,140,150,175,180,170}; private int[] car_x = {80,70,70,95,140,150,175,180,170,170};
}//method drive
//Draws the car at a particular x offset. public void draw_car (int offset, Graphics page) { page.setColor (Color.white); page.fillRect(1,1,449,249);
page.setColor (Color.red); page.drawPolyline (car_x, car_y,10);//car page.drawArc (80,180,20,20,0,180);//rear wheel well page.drawArc (150,180,20,20,0,180);//front wheel well page.drawLine (100+offset,190,150+offset,190); page.setColor (Color.black); page.drawOval (83,183,14,14);//rear wheel page.drawOval (153,183,14,14); //front wheel
//Update the x coordinates. for (int scan=0; scan < NUM_POINTS; scan++) car_x_update[scan] = car_x[scan] + offset;
}//method draw_car
} //class Car
|
|