• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best practise to show graphics2D in a specific panel?

 
Joacim Ahlberg
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator

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