Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Trying to make a Car program and I can't seem to get it to compile

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Wink Trying to make a Car program and I can't seem to get it to compile

    Here is what i have thus far:

    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Line2D;
    import java.awt.geom.Point2D;

    /**
    * A car shape that can be positioned anywhere on the screen.
    * */
    public class Car
    {
    private int xLeft;
    private int 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 Car(int x, int y)
    {
    xLeft = x;
    yTop = y;
    }

    /**
    * Draws the car.
    * @param g2 the graphics context
    * */
    public void draw(Graphics2D g2)
    {
    Rectangle body = new Rectangle(xLeft, yTop + 10, 60, 10);
    Ellipse2D.Double frontTire = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10);
    Ellipse2D.Double rearTire = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10);

    //The bottom of the front windshield
    Point2D.Double r1 = new Point2D.Double(xLeft + 10, yTop + 10);
    //The front of the roof
    Point2D.Double r2 = new Point2D.Double(xLeft + 20, yTop);
    //The rear of the roog
    Point2D.Double r3 = new Point2D.Double(xLeft + 40, yTop);
    //The bottom of the rear windshield
    Point2D.Double r4 = new Point2D.Double(xLeft + 50, yTop + 10);

    Line2D.Double frontWindshield = new Line2D.Double(r1, r2);
    Line2D.Double roofTop = new Line2D.Double(r2, r3);
    Line2D.Double rearWindshield = new Line2D.Double(r3, r4);

    g2.draw(body);
    g2.draw(frontTire);
    g2.draw(rearTire);
    g2.draw(frontWindshield);
    g2.draw(roofTop);
    g2.draw(rearWindshield);
    }
    }



    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JComponent;

    /**
    * This component draws two car shapes.
    **/
    public class CarComponent extends JComponent
    {
    public void paintComponent(Graphics 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);
    }
    }


    import javax.swing.JFrame;
    * This program allows students to see graphics shapes entered in
    * a class ShapeComponents*/
    public class CarViewer
    {
    public static void main(String[]args)
    {
    //Construct a new object, called window, in which we will draw
    JFrame window = new JFrame();

    //Set the frame size to 400 pixels by 400 pixels
    //DO NOT CHANGE THE SIZE OF THE WINDOW FOR THIS ASSIGNMENT
    window.setSize(400,400);
    window.setTitle("Drawing Window");

    /* Invoke the operator that stops the program if/when the frame is
    * closed */
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);

    //Add the desired shape to the frame
    CarComponent component = new CarComponent();
    window.add(component);

    //Make the frame visible
    window.setVisible(true);
    }
    }

    The two errors are is that "cannot find symbol g2." Help would be greatly appreciated
    Last edited by Necroshade; October 25th, 2011 at 07:15 PM.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make a Car program and I can't seem to get it to compile

    This is just coming from a novice but I don't think "g2" was defined in your script. You have to initialize it first which I don't see anywhere in there.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to make a Car program and I can't seem to get it to compile

    yeah, I saw the mistake, when I looked over it i saw that i forgot to put in the line "Graphics2D g2 = (Graphics2D) g;" you were correct

Similar Threads

  1. How do you make JOptions compile?
    By GirlCoder in forum AWT / Java Swing
    Replies: 4
    Last Post: July 19th, 2011, 04:19 PM
  2. [SOLVED] Compile a set of java files for a sudoku program
    By kanishk.dudeja in forum Java Theory & Questions
    Replies: 7
    Last Post: June 16th, 2011, 10:54 AM
  3. anagram program wont compile please help
    By Rusak in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 26th, 2011, 07:23 PM
  4. Anagram program wont compile
    By Rusak in forum Member Introductions
    Replies: 0
    Last Post: March 25th, 2011, 03:38 PM
  5. how do i make my program to....
    By andys in forum Object Oriented Programming
    Replies: 2
    Last Post: November 26th, 2010, 10:31 AM