Results 1 to 17 of 17
Thread: Help with project
- 03-28-2011, 04:03 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 52
- Rep Power
- 0
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:
The error that keeps giving me is: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; }
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 10:51 PM.
- 03-28-2011, 04:07 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Where is the code for the translate method? I don't see it anywhere.
- 03-28-2011, 04:08 AM #3
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
is there a static translate method somewhere?
- 03-28-2011, 04:14 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 52
- Rep Power
- 0
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(); }
- 03-28-2011, 04:16 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 #6
Member
- Join Date
- Jan 2011
- Posts
- 52
- Rep Power
- 0
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 #7
Member
- Join Date
- Jan 2011
- Posts
- 52
- Rep Power
- 0
Sunde887,
would that be the reason why the compiler is giving this error?
- 03-28-2011, 04:36 AM #8
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
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 #9
Member
- Join Date
- Jan 2011
- Posts
- 52
- Rep Power
- 0
I tried changing the code but it returns this error:
And I have to use the repaint method.Java Code:Car.java:68: non-static method repaint() cannot be referenced from a static context repaint();
- 03-28-2011, 04:49 AM #10
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
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 #11
Member
- Join Date
- Jan 2011
- Posts
- 52
- Rep Power
- 0
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:
era un method.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(); }
- 03-28-2011, 04:55 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 04:58 AM.
- 03-28-2011, 05:12 AM #13
Member
- Join Date
- Jan 2011
- Posts
- 52
- Rep Power
- 0
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:
and nowhere in that code is the word translate again. It appears only one time and that was the reason I copied like that.Java Code:public void moveBy(int dx, int dy) { box.translate(dx, dy) repaint(); }
- 03-28-2011, 05:35 AM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 #15
Member
- Join Date
- Jan 2011
- Posts
- 52
- Rep Power
- 0
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 #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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?
This is just a first thought.Java Code:public void moveBy(Car car, int deltaX, int deltaY){ car.translate(deltaX, deltaY); repaint(); }
- 03-28-2011, 05:57 AM #17
Member
- Join Date
- Jan 2011
- Posts
- 52
- Rep Power
- 0
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.
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.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.
Similar Threads
-
Adding a project to an existing project
By Seijuro in forum NetBeansReplies: 4Last Post: 08-08-2010, 10:15 AM -
Project..
By lohithbl in forum Advanced JavaReplies: 5Last Post: 04-01-2010, 09:31 AM -
Project Help
By XxHEXSORxX in forum AWT / SwingReplies: 4Last Post: 01-28-2009, 10:01 AM -
open existing project project ..
By itaipee in forum EclipseReplies: 1Last Post: 12-28-2008, 08:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks