Best practise to show graphics2D in a specific panel?
Joined: Jan 02, 2004
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
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.