-2

I have a JPanel with an two images (one underneath the other). The goal is for the user to be able to use the mouse the "scratch off" the top image and thus expose that part of the bottom image.

It is Erase part of image on Tkinter canvas, exposing another image underneath exactly except in Java Swing instead of Python.

Is this possible? I know you can use BufferedImage.getSubimage, but, as far as I can tell, this only works to crop the image.

Community
  • 1
  • 1
Joe
  • 116
  • 1
  • 16
  • Use layered JPanels. For example: [Stack Overflow: Draw custom stuff on top of opaque components in a JPanel](http://stackoverflow.com/questions/11047847/draw-custom-stuff-on-top-of-opaque-components-in-a-jpanel) – Mr. Polywhirl Aug 15 '16 at 16:17
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/) of your best attempt (even if that stops at loading two images and recording where the mouse has been). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Aug 15 '16 at 16:24

1 Answers1

3

Assuming your Image supports alpha transparency then you can just read the Image into a BufferedImage and then use the setRGB(...) method of the BufferedImage to clear the image:

BufferedImage image =  ImageIO.read( new File( ... );
int transparent = new Color(0, 0, 0, 0).getRGB();
image.setRGB(??, ??, transparent);

So you would need to use a MouseListener to handle a MouseEvent to get the location of the pixels you want to make transparent.

camickr
  • 321,443
  • 19
  • 166
  • 288