# Thread: New to Applets..How do you draw the figure in the paint method multiple times?

1. Member
Join Date
Apr 2011
Posts
4
Rep Power
0

## New to Applets..How do you draw the figure in the paint method multiple times?

My code draws one car, but I wanted to have six cars....I know I can copy this 6 times, but is there a more efficient way?

I want to be able to set the position of each car as well...

Java Code:
```public class DrawCar extends Applet
{

public void paint(Graphics g)
{  Graphics2D g2 = (Graphics2D)g;       // create the car body
Rectangle body = new Rectangle(10, 110, 60, 10);
// create the car tires
Ellipse2D.Double frontTire = new Ellipse2D.Double(20, 120, 10, 10);
Ellipse2D.Double rearTire   = new Ellipse2D.Double(50, 120, 10, 10);  // create the 4 points connecting the windshields and roof
Point2D.Double r1 = new Point2D.Double(20, 110);
Point2D.Double r2 = new Point2D.Double(30, 100);
Point2D.Double r3 = new Point2D.Double(50, 100);
Point2D.Double r4 = new Point2D.Double(60, 110);  // create the windshields and roof of the car
Line2D.Double frontWindshield  = new Line2D.Double(r1, r2);
Line2D.Double roofTop = new Line2D.Double(r2, r3);
Line2D.Double rearWindshield  = new Line2D.Double(r3, r4);          // draw  all of the car parts on the screen
g2.draw(body);
g2.draw(frontTire);
g2.draw(rearTire);
g2.draw(frontWindshield);
g2.draw(roofTop);
g2.draw(rearWindshield);

} // end of paint
}
// end of CarDrawer*/```

2. Possibilities:
• Create a method that accepts a Graphics object and a Point (for a base point) that draws your cars from within the paint method
• Create a Car class that draws itself say in a public draw(Graphics2D g2) method and have your applet hold an ArrayList of these objects, then in paint iterate through the list telling each Car to draw and passing in the current Graphics object
• Many more possible solutions...

3. Member
Join Date
Apr 2011
Posts
4
Rep Power
0
I figured out to create multiple cars, now I want to move them horizontally right across the screen...

How would I do that if I drew each part of the car separately? I would need to move the rectangle, tires, windows, etc. at the same time?

4. If you create a class to represent a Car object and be responsible for knowing its own position and drawing itself or method to draw a car based on a single position (probably a Point), then you'd be all set.

5. Member
Join Date
Apr 2011
Posts
4
Rep Power
0
Thanks...I'm able to race the cars across the screen....but I would like to stop the applet and display the winner whenever a car crosses a certain point...

I am unsure about where to put the if statement to check whether it crossed the line..Should i put it in my run method since that is constantly being executed?

Java Code:
```	public void run() {
// Display the next frame of animation.
repaint();

// Delay for a while
try {
} catch (InterruptedException e) {
break;
}

// Advance the frame
frame++;
}

}```

6. Originally Posted by etranman1
Thanks...I'm able to race the cars across the screen....but I would like to stop the applet and display the winner whenever a car crosses a certain point...

I am unsure about where to put the if statement to check whether it crossed the line..Should i put it in my run method since that is constantly being executed?

Java Code:
```	public void run() {
// Display the next frame of animation.
repaint();

// Delay for a while
try {
} catch (InterruptedException e) {
break;
}

// Advance the frame
frame++;
}

}```
You'd likely have a boolean controlling your game loop, stopping it when the game is over.

7. Member
Join Date
Apr 2011
Posts
4
Rep Power
0
So I think I implemented it wrong. I pretty have much the whole screen repainted each time so the cars move at a constant speed and it comes out the same winner each time.

How would I make each car run on its own at random speeds?

8. Moderator
Join Date
Apr 2009
Posts
13,540
Rep Power
24
Generate a random number for each car for the distance (in pixels presumably) it moves during this iteration of the game loop.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•