Results 1 to 8 of 8
- 04-28-2011, 02:44 AM #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*/
-
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, 12:38 AM #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?
-
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-02-2011, 11:35 PM #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() { 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-05-2011, 03:50 PM #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?
- 05-05-2011, 04:26 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
how to draw a dog using applets?
By germada in forum Java AppletsReplies: 0Last Post: 03-23-2011, 03:20 PM -
write line multiple times
By relith in forum New To JavaReplies: 3Last Post: 10-27-2010, 08:38 AM -
Issue with saving multiple times to a variable
By sidd0123 in forum New To JavaReplies: 3Last Post: 06-06-2010, 02:02 AM -
Why the paint() method is called two times ?
By supremo in forum New To JavaReplies: 4Last Post: 06-03-2010, 06:21 PM -
Playing an AudioInputStream multiple times
By pmgallardo in forum Advanced JavaReplies: 6Last Post: 03-09-2009, 04:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks