Results 1 to 8 of 8
  1. #1
    etranman1 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #3
    etranman1 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    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. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #5
    etranman1 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    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++;
    		}
    		
    	}

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by etranman1 View Post
    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++;
    		}
    		
    	}
    You'd likely have a boolean controlling your game loop, stopping it when the game is over.

  7. #7
    etranman1 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    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. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default

    Generate a random number for each car for the distance (in pixels presumably) it moves during this iteration of the game loop.

Similar Threads

  1. how to draw a dog using applets?
    By germada in forum Java Applets
    Replies: 0
    Last Post: 03-23-2011, 04:20 PM
  2. write line multiple times
    By relith in forum New To Java
    Replies: 3
    Last Post: 10-27-2010, 09:38 AM
  3. Issue with saving multiple times to a variable
    By sidd0123 in forum New To Java
    Replies: 3
    Last Post: 06-06-2010, 03:02 AM
  4. Why the paint() method is called two times ?
    By supremo in forum New To Java
    Replies: 4
    Last Post: 06-03-2010, 07:21 PM
  5. Playing an AudioInputStream multiple times
    By pmgallardo in forum Advanced Java
    Replies: 6
    Last Post: 03-09-2009, 05:29 PM

Posting Permissions

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