aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes painting custom components without extending jcomponent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "painting custom components without extending jcomponent" Watch "painting custom components without extending jcomponent" New topic
Author

painting custom components without extending jcomponent

Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Hi, I am not understanding how to draw my custom components onto a panel. My teacher is asking for an abstract class, Shape, which cannot extend or implement anything. Then I have two subclasses, Circle and Square, that extend Shape (so they cannot extend anything else like JComponent). Finally I have a class, Draw, that creates a main window and adds three buttons. One button adds Circle objects, one button adds Square objects, and the other button changes the colors of the objects. Currently, when I click "Add Circle," I get runtime exceptions. Can anyone help me understand why? Here's my source:
Shape.java

Circle.java

Draw.java


Sorry there's so much code... I have a feeling it has something to do with the way I'm handling the event in the actionPerformed method (right at the bottom of Draw.java). Thanks for any help!


Studying for SCJP 6
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

It would have helped if you included the stack trace.

The problem is with private Shape[] shape; - you never initialize it. Where is your line that says "shap = new Shape[...]"?
If you don't want to limit the number of shapes to 5, you may want to consider using a List:

It has its own counter for the number of elements to (shapes.size()), so numOfShapes becomes obsolete.


Edit: after adding that line your code still does not work. That's because you're not overriding paintComponent in Draw - JFrame has no such method. You need paint instead; also don't forget to call super.paint(g); first.

Also, don't make x and y static. That way all your shapes will have the same coordinates.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bartek Myszkowski
Ranch Hand

Joined: Feb 03, 2009
Posts: 44
@up: sorry for repeating what You've posted Rob but I didn't noticed You've edited Your post

...and You also override paintComponent of JFrame but... there's no such method to override

Use @Override annotation just before method You are overrideing (paintComponent) and You'll see what I mean!

You should override "paintComponents" method if You want draw on JFrame but I suggest to add JPanel to JFrame and draw on that panel.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4541
    
    5

Bartek Myszkowski wrote:
You should override "paintComponent[size=14]s
" method if You want draw on JFrame

No, never. The method paintComponents has an entirely different role.

The correct way to do custom painting in Swing is to override paintComponent of a JComponent (or any subclass of it).

db


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Darryl Burke wrote:The correct way to do custom painting in Swing is to override paintComponents of a JComponent (or any subclass of it).

Shouldn't that be paintComponent?

Bartek is right though, the best way is to use a JPanel subclass and extend its paintComponent method.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4541
    
    5

Rob Prime wrote:
Darryl Burke wrote:The correct way to do custom painting in Swing is to override paintComponents of a JComponent (or any subclass of it).

Shouldn't that be paintComponent?

Bartek is right though, the best way is to use a JPanel subclass and extend its paintComponent method.


Yup, I had a typo in the worst possible place
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Well this would make a lot more sense if my teacher would allow me to extend JComponent in the Shapes class. The Circle and Square class already extend a class, and multiple inheritance is not allowed, so what else can I do to make this work without "disobeying" specs? Is there an interface I can implement that makes my class a JComponent?
Thanks for the info on the shapes array, that's a dumb mistake on my part. Personally I'd rather program encryption algorithms thank program Swing components. But I know it's useful and I need to know it.

[EDIT]
I'm sorry Rob, after reading your post again more carefully, it clicked in my head to change my paintComponents to paint(). I got something showing up on the screen, I think I can take it from here. Thanks a lot everyone!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: painting custom components without extending jcomponent