This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Graphics2D on JPanel  RSS feed

 
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I want to paint onto a JPanel and not the contentPane of my JFrame, do I need to write a class that extends JPanel and do my Graphics2D painting by over riding it's paint method, and then putting that JPanel on my JFrame? Or is there another way to paint on a JPanel without creating a seperate class?
Thanks.
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's how I do it. Don't know of any other way. Just make sure you call super.paintComponent() from the overloaded method.
Ashish H.
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put Canvas onto JPanel and override his paint() method.
 
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use Canvas... it's an AWT component, so it would hang on top of the other Swing components in your application.

Just like everyone else said... The best way to do this would be sub-class JPanel and do the painting in it's paintComponent() method...

You could also grab the graphics context of the panel using panel.getGraphics(), and then draw to that graphics context... however, you'll have to do this in a thread to keep it consistantly updating, and there is no guarantee that it's going to be drawn every time the frame repaints... so it isn't a good solution...
[ January 06, 2003: Message edited by: Nathan Pruett ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!