• Post Reply Bookmark Topic Watch Topic
  • New Topic

Saving drawing on simple paint application problem

 
Ehsan Rahman
Ranch Hand
Posts: 59
Chrome Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to create a simple paint application.

A JPanel is extended and the drawing is repainted as the user moves the mouse around.

I'm trying to Save the drawing by saving the extended JPanel class (Panel) but am not having much luck. When opening two saved files (one blank drawing and one with some drawing) I don't see much difference in terms of data.

I think the reason this isn't working is because the Graphics object used in JPanel is not serializable though I haven't confirmed this.

What suggestions could you please provide in implementing this so that drawings can be saved and reloaded, much like MS Paint? Would using images be a better implementation?

The source code is below. I separated the Panel class when attempting to save it (before it was an inner class and hence could not be serialized).

Many Thanks,
Ehsan







 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, serializing a Graphics object is not the right way to go. You'll need to create an actual image file. Something like the following should do the trick:
 
Ehsan Rahman
Ranch Hand
Posts: 59
Chrome Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion Ulf, early indications shows images are saving. The program needs redesigning in terms of saving what the user has drawn.
 
Ehsan Rahman
Ranch Hand
Posts: 59
Chrome Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I'm having trouble in saving the graphics object from the subclassed JPanel to the BufferedImage.

myPanel.paint(bimage.getGraphics()); doesn't quite work: the background is black and it only saves the last square dot that was painted onto the panel.

The paint() method isn't recommended to be called directly apparently, so I tried doing what has been suggested on this link:
http://stackoverflow.com/questions/113897/how-do-i-get-the-image-paint-paintcomponent-generates

So I wrote the following in the Panel class (this is only a snippet):




... note lines 50 - 54. This is the method used in the link provided.

This is how the calling class is attempting to save the BufferedImage ...



But the problem is that the bufImg being returned is null, even though super.paint(bImg.getGraphics()); has been called.

I'm not quite understanding how the graphics object can be captured. Please could you help prove some hint?

Thanks in advance.
 
Ehsan Rahman
Ranch Hand
Posts: 59
Chrome Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Backtrack: actually calling label.paint() was fine, there was just an issue with the way the graphics object of the panel was being handled using the switch statement. Now a BufferedImage is used to cache the display paint() should draw. This means when resizing/min-maximizing/moving the frame the image drawn stays intact.

This thread is helpful.
http://www.coderanch.com/t/346020/Swing-AWT-SWT-JFace/java/store-what-JPanel-draw#1518730

Updated code:



 
Twinkle Thakkar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ahesan,

I am a newbee in this world of Swing Programming. I have been trying to solve a perticular problem in which i am suppose to provide drawing with mouse functionality and capture the resultant graphics in a jpg / png file.

I have googled a lot and found an example in which allows me to draw with mouse on a panel.
I also added the code to capture the perticular panel on which i am drawing.

Now the problems are:

1. If the screen losts focus, it looses the drawing made on the panel.
2. While capturing the component, only panel gets captured with out the drawing.

Can you please let me know what is acting as a show stopper?

Here is the code:


Many Thanks,
Twinkle
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. If the screen losts focus, it looses the drawing made on the panel.
2. While capturing the component, only panel gets captured with out the drawing.

Both these have the same cause - there's no paintComponent method in which the drawing is done. Instead of performing the drawing in the myMouseDragPaint method, you need to capture the points or line segments in some data structure, and then use that to do the drawing in the paintComponent method. That's the method Swing will call whenever it needs to paint something; see Performing Custom Painting for details.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!