Hi Everyone, I am working on a school project (I am sure it is a first) and I am running into some problems. I have to build three classes; two classes to generate the items and a ArrayList, and the last one is a test class. I started working on the first class that is suppose to create a Car and then I am also suppose to add a translate method so it will create some animation.
I took some of the book's codes and examples to create it but I am getting errors and I am not sure how to fix them. This is what I have so far:
and this is the error:
Any ideas or recommendations on how to fix this error?
You commented out your translate() method but then use it a few lines later in the moveBy() method at line 74. The error message is telling you that it doesn't know about a translate() method for the Car class.
Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Joined: Feb 19, 2011
Yes, I commented the translate method out because I was trying to make it work and I kept getting errors. I have since figure it out and I am using the following lines:
At least it is compiling now.
Joined: Feb 19, 2011
Now, I started working on my second class. In this class, I have to create a ArrayList, fill it with cars, and then draw the cars on the canvas.
By looking at this piece of code, do you see anything wrong with it?
Can't you tell where the error is by looking at the error message?
If you try an IDE that would show you your error straight away.
Joined: Feb 20, 2011
Luigi Plinge wrote:You probably meant car.draw.
Actually he did mean auto.draw() as the object auto is referencing a Car class which has a draw() method.
But the enhanced for loop is wrong.
Also you may want to check that the Graphics object you are passing into the method paintComponent(Graphics g) is of a Graphics2D type. As you pass the super class to the first method (paintComponent(Graphics g)) but pass a subclass to the auto.draw() method.
It’s a lot more dangerous to down cast than to up cast. I’m not going to give you the code because I’m sure you can research how to determine if an object is of a particular type or not. But I would wrap all the code in an ‘if statement’. If the passed object is a Graphics2D type, do the for loop. It may even get you extra marks for being type safe.
Also I don't think you have to worry about paintComponent being passed a non-Graphics2D object. paintComponent should never be called directly (you use the repaint() method in your code) and this cast is a standard from what I've seen in books.
Now we have got rid of the compiler errors, the question seems to be more about Graphics, so I shall move this thread.
Each JComponent has a paintComponent(java.awt.Graphics) method (not to be confused with paintComponents()). This method is protected, so (as Luigi Plinge has told us) you don't call it directly. You should always begin it with super.paintComponent(g); The Graphics object passed is an instance of a concrete subclass of Graphics2D, so the cast is always feasible.
I would suggest you pass the Graphics object from the paintComponent() method, to the individual Car objects. You could pass it to the constructor, but that ties the Car objects to a particular Component, so I think you are doing better to pass it to the draw() method. There is some good code in that.
How are you calling repaint()? Does that mean your Car class is an inner class in one of your Components?
If you are applying any transforms to a Graphics object, it is probably best to copy that object before passing its reference.
Note you can apply translate() to a graphics object. So you can translate the Car or you can translate the Graphics object it is using. Translation doesn't usually cause problems if you don't create a copy, but the more complex transforms do, shear() being the worst offender. You can shear() and shear() the same amount in the opposite direction, and all the other parameters of the Graphics object change mysteriously. That is caused by imprecision in the floating-point arithmetic used.