Results 1 to 17 of 17
  1. #1
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default Help with project

    I am working on another school project. I have completed most of the code but I am having a little problem. I am sure that you will be able to tell me right away what it is.

    This is the code:

    Java Code:
    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.JComponent;
    
    /**
       A car shape that can be positioned anywhere on the screen.
    */
    public class Car extends JComponent //extended so i can use Repaint
    {
       /**
          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 + 10, 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 roof
          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);      
       }
    
    	public void moveBy(int dx, int dy)
    	{
    		Car.translate(dx, dy); // I can use car but them I have to declare it a constant. 
    		repaint();
    	}
    	
       private int xLeft;
       private int yTop;
    }
    The error that keeps giving me is:

    Car.java:67: cannot find symbol
    symbol : method translate(int,int)
    location: class Car
    Car.translate(dx, dy);
    ^
    1 error

    Any hints?

    Thanks,
    Last edited by camaro01; 03-29-2011 at 11:51 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Where is the code for the translate method? I don't see it anywhere.

  3. #3
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    is there a static translate method somewhere?

  4. #4
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    According to the book, this is what I am using:
    Java Code:
    public void moveBy(int dx, int dy)
    	{
    		Car.translate(dx, dy); // I can use car but them I have to declare it a constant. 
    		repaint();
    	}

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You don't show the static method translate in your code. If it exists we need to see it. If it doesn't you need to design it.

  6. #6
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    I am also using a timer event so I can create the movement. Is that what you guys are asking?

    I have to write two other classes but the translate method is suppose to go in this class.

  7. #7
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    Sunde887,

    would that be the reason why the compiler is giving this error?

  8. #8
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    Car.translate(dx, dy); //this is a call to a static method in the car class which means that a method with this signature must exist within the car class

    a method with this signature would looking something like this.

    public static void translate(int x, int y){
    //create code that does what you want here
    }

    Also a static method call in the context of your code makes very little sense to me

  9. #9
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    I tried changing the code but it returns this error:

    Java Code:
    Car.java:68: non-static method repaint() cannot be referenced from a static context
    		repaint();
    And I have to use the repaint method.

  10. #10
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    I am not sure you understand what I am saying.

    Are you familiar with how to create a method in java?
    do you know how to call a method in java?
    do you know the difference between an object and a class method?

  11. #11
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    This is my first java class and sometimes things don't make much sense to me. I tried to use examples in the book and I modify what I see and make it work.

    This is the instructions I have for this class:

    Specific Requirements for the Car Class:
    1. The Car class is very similar to the car class used in the example in Chapter 3.
    2. The instance fields are:
    1. xLeft - the x coordinate starting point for drawing a car.
    2. yTop - the y coordinate starting point for drawing a car.
    3. The constructor takes in two integers that are used to set the xLeft and yTop instance fields.
    4. The methods are:
    1. A draw methods that takes in a Graphics2D object, then builds and draws the car. Using the code from chapter 3 car class is allowed however, you may experiment with shape and style of the car.
    2. A translate method that takes in two integers that represent moving the car's starting points. (xLeft and yTop). For example, if the car is drawn starting at position (0, 10) and you call translate (1,0) the car will move one pixel on the X axis and zero pixels on the Y axis.

    Yo pense que:
    Java Code:
    public void moveBy(int dx, int dy)
    	{
    		Car.translate(dx, dy); // I can use car but them I have to declare it a constant. 
    		repaint();
    	}
    era un method.

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You need to have a method called translate in your car class in order to make calls on it. If it's meant to be static, thats fine, but when working with statics you should know why it's static.

    Right now in the car class you have a method called translate(which apparently appears to be static) which is called in another non static method of the car class. The problem is, you don't have this translate method in the car class, so what is the compiler going to call?

    The question never asks for you to create a method called moveBy, it asks for a method called translate.

    The method should look something like this
    Java Code:
    public void translate(deltaX, deltaY){
      //do something to change the x and y coords of the car
    }
    Last edited by sunde887; 03-28-2011 at 05:58 AM.

  13. #13
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    Sunde,

    I understand what you are telling me now. But I copied this code from another program that has it the same way and there is nowhere in that code a method translate in the code nor in the tester class. And the code uses this way:

    Java Code:
    public void moveBy(int dx, int dy)
    {
       box.translate(dx, dy)
       repaint();
    }
    and nowhere in that code is the word translate again. It appears only one time and that was the reason I copied like that.

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    in that copied code, what is box? Is that the class name, or an instance variable of the class?

    I am not especially familiar with graphics programming, but apparently, there is a method called translate.
    Graphics (Java 2 Platform SE v1.4.2)

    it is not, however; a static method, so it must be called on a graphics object.

  15. #15
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    box is a new rectangle.

    The class is RectangleComponent extends JComponent and then box, I think is an object.

    The next line is box = new Rectangle(...)

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I do not have a lot of practice programming with graphics, so my advice may not be the greatest. That being said, I will try to help you out while waiting for someone with more experience.

    It appears that the call to translate should be called on the car object. Perhaps you could make the method take a car object as well?

    Java Code:
    public void moveBy(Car car, int deltaX, int deltaY){
      car.translate(deltaX, deltaY);
      repaint();
    }
    This is just a first thought.

  17. #17
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    Sunde,

    thanks for your help. I was looking for animation in the Java tutorials but I did not find it.

    I tried your idea and this is what I got:

    Java Code:
     ----jGRASP exec: javac -g Car.java
    
    Car.java:74: cannot find symbol
    symbol  : variable car
    location: class Car
    		car.translate(dx, dy); // I can use car but them I have to declare it a constant. 
    		^
    1 error
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.
    Java Code:
     ----jGRASP exec: javac -g Car.java
    
    Car.java:74: cannot find symbol
    symbol  : method translate(int,int)
    location: class Car
    		car.translate(dx, dy); // I can use car but them I have to declare it a constant. 
    		   ^
    1 error
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.
    The first error is when I run it the same way I have it. The second error when I added the "Car car" to the parameter.

Similar Threads

  1. Adding a project to an existing project
    By Seijuro in forum NetBeans
    Replies: 4
    Last Post: 08-08-2010, 11:15 AM
  2. Project..
    By lohithbl in forum Advanced Java
    Replies: 5
    Last Post: 04-01-2010, 10:31 AM
  3. Project Help
    By XxHEXSORxX in forum AWT / Swing
    Replies: 4
    Last Post: 01-28-2009, 11:01 AM
  4. open existing project project ..
    By itaipee in forum Eclipse
    Replies: 1
    Last Post: 12-28-2008, 09:12 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
  •