This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Painting and erasing over image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Painting and erasing over image" Watch "Painting and erasing over image" New topic
Author

Painting and erasing over image

Lukas Hnatuk
Greenhorn

Joined: May 31, 2010
Posts: 10
Hello, i am trying to make an application, where i can draw to an image, but i stucked.
My problem is:
A have an image and i need to write something in to with my tablet. But I need not to modify the image itself. And sometimes, i need to clear what i have written before, but also i do not need to modify the image. My idea was to write it to the GlassPane, which was over the JPanel, where the image was displayed. The problem is, when i need to erase the text a write(draw with my tablet) before, i remove the picture at the background too.
Here is the code, where i draw to the image:
currentAction:

The funtion reaint the parent is called everyTime the component need to be repainted.
And here is the code of the windows, where i am drawing(writing)

The Rewitable GlassPane is only update JPanel like this:


Can someone please give me some guide, how to to erase the things i drew before?
Lets assume a make a pencil(currentAction) and i need a eraser)))


Sever: GlassFish v3
JSF 2.1
Mostly using NetBeans
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Lukas Hnatuk wrote:
Can someone please give me some guide, how to to erase the things i drew before?
Lets assume a make a pencil(currentAction) and i need a eraser)))


I'm not a graphics expert, but usually I'll draw my non-changing background image into a BufferedImage and display that in the JPanel via the JPanel's paintComponent method, and then I draw the more temporary images often by placing Shape objects into a collection and iterating through the collection in the same paintComponent after showing the BufferedImage.

Are you currently following a tutorial to see how to paint correctly? I ask this because I see that you're getting your JPanel's Graphics object via getGraphics which is the wrong way to do this 99% of the time. If you haven't gone through a decent Graphics tutorial (and the Oracle 2D graphics and painting in Swing tutorials should be where you'd start), I'd do this before writing any more code. Otherwise, if you are currently following a tutorial and it tells you to get the graphics this way (are you doing full-screen graphics?), then please let us know which tutorial with link.

Much luck!
Lukas Hnatuk
Greenhorn

Joined: May 31, 2010
Posts: 10
I have the code from this site: http://www.java2s.com/Code/Java/2D-Graphics-GUI/Scribble.htm
I have also read the oracle swing tutorial, but none of it suits me the way i want and this link suits me the best.
Lukas Hnatuk
Greenhorn

Joined: May 31, 2010
Posts: 10
Now i see a did the wrong way)) thank you
 
 
subject: Painting and erasing over image
 
Similar Threads
Paint Drag Preview
Using glass pane for faster animation
MouseDragg
How can i use mouse to click on an image and draw a point after i clicked ?
Can't clear glassPane