Help with project

• 03-28-2011, 04:03 AM
camaro01
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:

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,
• 03-28-2011, 04:07 AM
sunde887
Where is the code for the translate method? I don't see it anywhere.
• 03-28-2011, 04:08 AM
yellowledbet
is there a static translate method somewhere?
• 03-28-2011, 04:14 AM
camaro01
According to the book, this is what I am using:
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();         }```
• 03-28-2011, 04:16 AM
sunde887
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.
• 03-28-2011, 04:16 AM
camaro01
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.
• 03-28-2011, 04:18 AM
camaro01
Sunde887,

would that be the reason why the compiler is giving this error?
• 03-28-2011, 04:36 AM
yellowledbet
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
• 03-28-2011, 04:46 AM
camaro01
I tried changing the code but it returns this error:

Code:

```Car.java:68: non-static method repaint() cannot be referenced from a static context                 repaint();```
And I have to use the repaint method.
• 03-28-2011, 04:49 AM
yellowledbet
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?
• 03-28-2011, 04:54 AM
camaro01
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:
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.
• 03-28-2011, 04:55 AM
sunde887
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
Code:

```public void translate(deltaX, deltaY){   //do something to change the x and y coords of the car }```
• 03-28-2011, 05:12 AM
camaro01
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:

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.
• 03-28-2011, 05:35 AM
sunde887
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.
• 03-28-2011, 05:44 AM
camaro01
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(...)
• 03-28-2011, 05:50 AM
sunde887
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?

Code:

```public void moveBy(Car car, int deltaX, int deltaY){   car.translate(deltaX, deltaY);   repaint(); }```
This is just a first thought.
• 03-28-2011, 05:57 AM
camaro01
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:

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.```
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.