Results 1 to 3 of 3

Thread: Drawing a car

  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Drawing a car

    I have this:


    Java Code:
    package car;
    
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Line2D;
    import java.awt.geom.Point2D;
    import javax.swing.JFrame;
    
    public class Car {
        private int xLeft, yTop;
        
        /**
         * Constructs a car with a given top left corner 
         * @param x the x coordinate of the top left corner
         * @param y the y coordinate of the top left corner
         */ 
        public void Car(int x, int y) {
            xLeft = x;
            yTop = y;
        }
        
        /**
         * Draws the car
         * @param g2 the graphics component 
         */
        public void draw(Graphics2D g2) {
            Rectangle body = new Rectangle(xLeft, yTop - 10, 60, 10);
            Ellipse2D.Double frontTire = new Ellipse2D.Double(xLeft + 10, yTop + 10,
                                                              10, 10);
            Ellipse2D.Double rearTire = new Ellipse2D.Double(xLeft + 40, yTop + 20,
                                                              10, 10);
            // The bottom of the front windsheild
            Point2D.Double p1 = new Point2D.Double(xLeft + 10, yTop + 10);
            // The Front of the roof
            Point2D.Double p2 = new Point2D.Double(xLeft + 20, yTop);
            Point2D.Double p3 = new Point2D.Double(xLeft + 40, yTop);
            Point2D.Double p4 = new Point2D.Double(xLeft + 50, yTop + 10);
            
            Line2D.Double frontWindshield = new Line2D.Double(p1, p2);
            Line2D.Double roofTop = new Line2D.Double(p2, p3);
            Line2D.Double rearWindshield = new Line2D.Double(p3, p4);
            
            g2.draw(body);
            g2.draw(frontTire);
            g2.draw(rearTire);
            g2.draw(frontWindshield);
            g2.draw(roofTop);
            g2.draw(rearWindshield);
                    
        }
    }

    And I have this:

    Java Code:
    package car;
    
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;
    
    public class CarComponent extends JComponent{
        
        public void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            Car car1 = new Car(0, 0);
            int x = getWidth() - 60;
            int y = getHeight() - 30;
            Car car2 = new Car(x, y);
            car1.draw(g2);
            car2.draw(g2);
        }
        
         public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setSize(300, 400);
            frame.setTitle("Two Cars");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);     
            CarComponent component = new CarComponent();
            frame.add(component);
        }
        
    }
    My only problems are on the lines where car1 and car2 are in the paintComponent() method there is an error that says actual and formal argument lists differ in length.

  2. #2
    kuriozal is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Drawing a car

    Quote Originally Posted by jwl View Post
    Java Code:
        /**
         * Constructs a car with a given top left corner 
         * @param x the x coordinate of the top left corner
         * @param y the y coordinate of the top left corner
         */ 
        public void Car(int x, int y) {
            xLeft = x;
            yTop = y;
        }
    It's the normal method (with the name of constructor). Constructors can't return any type. So remove "void" and it should work.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Drawing a car

    You should avoid constructing objects in a painting method override, as painting methods are called multiple times. Construct the Cars elsewhere and call repaint() when done. If needed, you can set an existing Car object's coordinates before drawing it, or you can create a method draw(Graphics2D, x, y).

    Also, your painting method override is lacking a call to the super implementation.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. help in drawing tex
    By Rampletero in forum Advanced Java
    Replies: 8
    Last Post: 03-10-2012, 05:17 PM
  2. need help for drawing...
    By nice7 in forum New To Java
    Replies: 4
    Last Post: 12-06-2011, 03:34 PM
  3. Drawing an arc
    By berkeleybross in forum Java 2D
    Replies: 10
    Last Post: 12-09-2010, 01:32 AM
  4. Drawing a map
    By Karp in forum AWT / Swing
    Replies: 4
    Last Post: 11-07-2008, 12:26 PM
  5. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 02:23 AM

Posting Permissions

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