This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Best practise to show graphics2D in a specific panel? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Best practise to show graphics2D in a specific panel?" Watch "Best practise to show graphics2D in a specific panel?" New topic
Author

Best practise to show graphics2D in a specific panel?

Joacim Ahlberg
Greenhorn

Joined: Jan 02, 2004
Posts: 5
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Joacim Ahlberg
Greenhorn

Joined: Jan 02, 2004
Posts: 5

Thanks a lot for your quick and precisely-what-I-needed answer
best regards
Joacim
 
 
subject: Best practise to show graphics2D in a specific panel?
 
Similar Threads
Can JPanel's paint() use different Graphics2D objects?
What's the best intensive HTML primer with examples
Stop Action - collect best moments of your life!
Setting frame minimum size
How to show Modelless Frame