aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Confused about painting in JPanels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Confused about painting in JPanels" Watch "Confused about painting in JPanels" New topic
Author

Confused about painting in JPanels

Lavanya Halliwell
Ranch Hand

Joined: Apr 09, 2010
Posts: 57
I am trying to learn a little bit about Swing.
How do you paint shapes on a JPanel? Most of my code is autogenerated when I drag and drop GUI components on the JFrame.




the problem is nothing appears in the JPanel, I do see the window with the menus.
if I add a paint method, it over writes the entire application menus and all and draws what ever the paint method has, but I want it to draw in the JPanel so I took the paint method out. How do you draw in ONLY the JPanel?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
How do you paint shapes on a JPanel?


You're not going to want to extract the Graphics object like that, but instead will want to draw in a paintComponent override method within a class that extends JPanel (or JComponent). For more details on painting in Swing, please have a look here:

Performing Custom Painting

Also you'll want to check out these articles too:
2D Graphics
Painting in AWT and Swing

Much luck!
R Johnson
Greenhorn

Joined: Jan 05, 2010
Posts: 28

I too have a problem with paint.. I can get the component to let me draw on it however if somethings obscures/damages the view or is minimized and then restored all the graphics are lost. I tried createImage(ImageProducer) but the ImageProducer confuses me since I'm calling createImage on the object I want to produce the image. I Also tried createImage(width,height) and get null values. I was trying to store a temp image of the canvas and then redraw it in the paint method. I also tried getGraphics but it returns a graphics how do you draw one graphics onto another? Or what would be a better solution?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
What didn't you understand about the example code given in the Performing Custom Painting tutorial?
R Johnson
Greenhorn

Joined: Jan 05, 2010
Posts: 28

I believe I understood the tutorial but that would mean I would have to create a vector of pixels that I've colored so I could minimize or view other windows in order to repaint what I've drawn when the frame is repaired. This seems a bit redundant because there is a vector of pixels being colored already and I would assume stored when I adjust the graphics of a component. If creating a vector or instead drawing on the same sized bufferedImage and then just having to paint the image on the frame in the paintComponent method is the only way then I'm sad but will accept it... I was just wondering if there was another solution.

For that matter bufferedImage is a great example of being able to draw on it using the createGraphics and what is drawn is stored on the Object...... It seems strange that the behavior would be different for a panel.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
R Johnson wrote:For that matter bufferedImage is a great example of being able to draw on it using the createGraphics and what is drawn is stored on the Object...... It seems strange that the behavior would be different for a panel.


But a JPanel and a BufferedImage are two completely different animals and should not be expected to behave the same. The Graphics object obtained from a JPanel is only temporary and will be replaced any time the JPanel is repainted including repainting that is not under your control such as when the operating system requests a repaint of a portion of your app. So using this knowledge you will expect a JComponent's Graphics object will not to persist and adjust your code accordingly. Nothing strange here as long as you understand what's going on.

A great link that explains a lot of the inner workings of Swing painting is this one: Painting in Swing and AWT

You may want to have a look.
R Johnson
Greenhorn

Joined: Jan 05, 2010
Posts: 28

But a JPanel and a BufferedImage are two completely different animals and should not be expected to behave the same. The Graphics object obtained from a JPanel is only temporary and will be replaced any time the JPanel is repainted including repainting that is not under your control such as when the operating system requests a repaint of a portion of your app. So using this knowledge you will expect a JComponent's Graphics object will not to persist and adjust your code accordingly. Nothing strange here as long as you understand what's going on.

A great link that explains a lot of the inner workings of Swing painting is this one: Painting in Swing and AWT


I'm not disagreeing and I understand the behavior my only confusion is that all a JPanel or any component for that matter is is colored pixels on a computer screen. If tell it to draw on it's graphics it seems a waste to have a method that allows it without saving the drawing/colored pixels to it's vector of colored pixels. I understand I can create a bufferedImage and draw this any time the component asks for paint()/paintComponent()/updateUI/etc etc. The problem I'm having is I'm trying to implement a feature at the moment that should be simple. If I draw on a bufferedimage and resize it to grow or shrink unlike in MSPaint the drawing does not scale correctly and stay in it's correct place. I created a grid with JPanels and first tried just drawing on a big image that overlapped all the JPanels and as I said the bufferedImage does not scale correctly with the JPanels to remain in it's placement on the grid. So I tried making individual images for each panel and allowing to draw on that I believe I'll still have an zoom loss problem but at least whatever remains will be in the correct cell. As I'm writing this I'm thinking of just drawing the grid and everything on one image and getting rid of the JPanels. I know I'll still have the zoom problem but it will remain in the correct cell and then will only have the one problem.

Perhaps an example and thanks again for all the replies and help.
In the example I'm just drawing circles if I make the frame smaller the circles become non-circles and I know I'm copying the image with non-circles and making them larger so all I have now is larger non-circles. I also understand I could just programmically draw things larger and bigger according to the images size but since the user is drawing the images and could draw at different zoom levels I would have to take snapshots anytime a person drew on the image and then just shrink or grow the original snapshot, if I didn't take a snapshot and just stored it as an image then my only other guess would be to capture the array of colored pixels and adjust it as it grows or shrink,but both of these seem overly complicated and I was just wondering if their was an easier solution.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused about painting in JPanels