Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Extending Canvas vs JPanel for gfx  RSS feed

 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there any reasons for extending a Canvas vs a JPanel if I'm going to be doing custom drawing on the component?

Does overriding paint or updateComponent make more sense?

Regards,
Aaron R>
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Swing we use a JPanel (or sometimes JComponent) for painting and usually override the paintComponent method (rarely paint).

In the AWT we use Canvas and override the paint method. Also override the update method in certain cases (whenever it helps) to reduce/eliminate flicker:

Mixing AWT and Swing components is not recommended.
 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info!

So the 'rule' would be Swing - over ride the paintComponent method. In AWT - override the paint.

Are there any Swing components you wouldn't recommend overiding?

Regards,
Aaron R>
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aaron Roberts:

Are there any Swing components you wouldn't recommend overiding?


Depends on what you want to override them for. I mean, I wouldn't recommend subclassing JButton for a component to paint on.

Swing was designed with the idea that a developer will extend the API to suit their needs rather than trying to provide every component you could imagine.
 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True true! I won't go subclassing happy. I can't think of a parallel to the threading 'debate/issue' where people extend a thread, when they really just need to implement Runnable. If I could, I would say - "I won't sub class that component!"

Regards,
Aaron R>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!