File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 24166
    
  30

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best practise to show graphics2D in a specific panel?
 
Similar Threads
Stop Action - collect best moments of your life!
How to show Modelless Frame
What's the best intensive HTML primer with examples
Setting frame minimum size
Can JPanel's paint() use different Graphics2D objects?