The Artima Developer Community
Sponsored Link

Java Answers Forum
1 thing missing in car animation

1 reply on 1 page. Most recent reply: Mar 13, 2002 1:41 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

1 thing missing in car animation Posted: Mar 12, 2002 8:02 PM
Reply to this message Reply
Advertisement
Hi. I can't get my start method to invoke the drive method of the car. I can't believe it.


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_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
  		}
  		
}//method drive
 
   //Draws the car at a particular x offset.
    public void draw_car (int offset, Graphics page) {
    	
         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
	


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: 1 thing missing in car animation Posted: Mar 13, 2002 1:41 PM
Reply to this message Reply
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

Flat View: This topic has 1 reply on 1 page
Topic: Java output printing by Dot Matrix Printer Previous Topic   Next Topic Topic: scheduling jobs in iPlanet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use