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

Printable View

• 04-28-2011, 03:44 AM
etranman1
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...

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*/```
• 04-28-2011, 04:35 AM
Fubarable
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...
• 04-30-2011, 01:38 AM
etranman1
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?
• 04-30-2011, 03:21 AM
Fubarable
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.
• 05-03-2011, 12:35 AM
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?

Code:

```        public void run() {                 while (Thread.currentThread() == animator) {                     // Display the next frame of animation.                     repaint();                                         // Delay for a while                     try {                         Thread.sleep(delay);                     } catch (InterruptedException e) {                         break;                     }                     // Advance the frame                     frame++;                 }                         }```
• 05-03-2011, 02:08 AM
Fubarable
Quote:

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?

Code:

```        public void run() {                 while (Thread.currentThread() == animator) {                     // Display the next frame of animation.                     repaint();                                         // Delay for a while                     try {                         Thread.sleep(delay);                     } 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.
• 05-05-2011, 04:50 PM
etranman1
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?
• 05-05-2011, 05:26 PM
Tolls
Generate a random number for each car for the distance (in pixels presumably) it moves during this iteration of the game loop.