aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help with project using graphics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help with project using graphics" Watch "Help with project using graphics" New topic
Author

Help with project using graphics

Richard Innamorato
Greenhorn

Joined: Feb 19, 2011
Posts: 12
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?
Callum Ludwig
Greenhorn

Joined: Feb 20, 2011
Posts: 13
Hi Ricardo,

Your Car class extends javax.swing.JComponent and there is no where in the API for any of the classes in the hierarchy tree for javax.swing.JComponent that has the method:



You could use the following method found in the class java.awt.Component:



See how that goes. Hope it helps you out.

Remember learning to read and use the API is very important, it does look daunting as a beginner programmer, but the tool has bottomless value.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
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
Richard Innamorato
Greenhorn

Joined: Feb 19, 2011
Posts: 12
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.
Richard Innamorato
Greenhorn

Joined: Feb 19, 2011
Posts: 12
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?

I created a ArrayList of cars

"auto = new ArrayList<Car>();"

And I am trying to pass all the objects in the ArrayList through the paint method. Any suggestions?
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

You probably meant car.draw.

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.
Callum Ludwig
Greenhorn

Joined: Feb 20, 2011
Posts: 13
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.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Callum Ludwig wrote:
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.

auto is referencing an ArrayList class. car is referencing a Car class.

But you're right about his enhanced for.
Callum Ludwig
Greenhorn

Joined: Feb 20, 2011
Posts: 13
Luigi Plinge wrote:
Callum Ludwig wrote:
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.

auto is referencing an ArrayList class. car is referencing a Car class.

But you're right about his enhanced for.


Yep so right, missed it even when I picked up on the enhanced for loop...... how shameful. *Hangs head in shame*

Ok Ricardo, I hope you got some useful stuff from all this.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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.

I shall change your method to this
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with project using graphics
 
Similar Threads
AffineTransform Rectangle2D
Variable is not declared?
Wanted: Java2D example of zooming and panning with scroll bars
Can't draw on JFrame or its ContentPane.
Drawing graphs