I'm programming in java.
I am essentially doing a paint like tool. The user can select a background image, use a mouse to draw a line, or erase a line. When erasing a line the background image should still the same.
So what I was trying to do was have a separate image for the background image and another image to track the mouse movement and draw lines. I then combine these 2 images to show the final image.
The problem that I'm unable to solve is the erase function. I tried using setXORMode which works great if I have a background image that has 1 color. But if the image has gradient color then the lines I draw over it changes color too.
Here is the snippet of the code I was working on:
private void backgroundImageOverNote()
{
Graphics2D g = image.createGraphics();
g.drawImage(backgroundImage.getImage(), 0, 0, 400, 200, null);
g.drawImage(sketchImage, 0, 0, 400, 200, null);
g.dispose();
}
public void sketch(SketchData sketchEvent)
{
if(isValidXYPosition(sketchEvent.getX(), sketchEvent.getY()))
{
Graphics2D g = sketchImage.createGraphics();
g.drawLine(oldX, oldY, sketchEvent.getX(), sketchEvent.getY());
g.dispose();
}
oldX = sketchEvent.getX();
oldY = sketchEvent.getY();
}
private BufferedImage sketchImage = new BufferedImage(400, 200, BufferedImage.TYPE_INT_ARGB);
private ImageIcon backgroundImage;