• 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

Best practise to show graphics2D in a specific panel?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am trying to find a way to display a graphical diagram in a specific JPanel, it sounds very trivial but I have not found a clean tip or solution yet.
I have found a lot of stuff but everything found is about showing graphics solely in either an applet or a whole frame not just a part of this frame.
Let us say we have three panels in one frame and one of them will show this Graphics2D object.... ?
Please give me a hint on where to find an example in this matter.
best regards
Joacim
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Welcome to JavaRanch!
The solution is indeed trivial. To create graphics inside a specific component in Java, you write a subclass of that component and override the appropriate painting method to create the graphics. For JPanel (or any Swing component) the appropriate methods is paintComponent(). Subclass JPanel, override paintComponent(), and inside your definition of this method, paint the graphics onto the Graphics object supplied as an argument (casting it to Graphics2D first.) Then use an instance of your subclass however you'd like -- i.e., as one of three JPanels inside a JFrame.
 
Joacim Ahlberg
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks a lot for your quick and precisely-what-I-needed answer
best regards
Joacim
 
reply
    Bookmark Topic Watch Topic
  • New Topic