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

Painting to a specific component.  RSS feed

 
merlin bar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My class MainPanel extends JPanel. At the moment, the paintComponent() method displays an image on the JPanel.
However, I have created an inner JPanel within this MainPanel. I have added it to MainPanel.
I now want the image painted to the innerPanle instead of the MainPanel. Is this possible without making an inner class and giving that a paint() method of its own?
I hope I've explained properly. Feel free to ask for clarification.
Regards,
merlin
 
Dan Harabox
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to overload the paintComponent method of your inner JPanel and place the graphics code there.
When your mainPanel draws itself, it will automatically ask all its children to draw themselves, hence invoking the draw method of your inner panel.
 
merlin bar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Daniel,
when I said "inner panel", I didn't mean inner class. But I did just that. Made an inner class (which extended JPanel) and gave it a paintComponent() method. Thanks for the idea!
Regards,
merlin
 
Dan Harabox
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Merlin, you don't need to create a new class if you don't want to.
You can simply override the paintComponent method of your new panel when you create it as so:

[ November 16, 2003: Message edited by: Daniel H. ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!