• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

painting custom components without extending jcomponent

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic