Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Painting and erasing over image

 
Lukas Hnatuk
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)))
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now i see a did the wrong way)) thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic