Don't stop reading, just because I'm going to say something about a paint program!!! ;D
I'm trying to figure out how to make a 'stamp' for a paint program.
I want the user to click on a small icon, say a picture of a tree. Where ever the mouse is moved in the drawing area, the tree stamp will follow. When the mouse is clicked it will stamp the tree down.
Right now I have a custom JPanel which tracks where the mouse is and lets you draw. Since I'm drawing directly to the Graphics context, I don't 'know' whats actually been drawn.
So, if I'm moving the stamp around the screen, it shouldn't destroy what is already there. I have 2 thoghts on how to do this -
1 - Figure out where the mouse is. Copy the image data at that location into a temporary image. Blit my tree where the mouse is. When the mouse moves. blit the temporary image to where the mouse was, then start at the beginning again.
2 - Draw to the panel like now, but also draw to a back buffer. When the mouse moves, blit the area of the stamp from the back buffer onto the current graphics context.
Do these sound like they will work? What about flicker issues?
This is another approach that uses two JPanels in an OverlayLayout. The lower panel(DrawingPanel) is for drawing. The upper panel (TopPanel) has the mouse listeners registered on it. When you mousePress on the image (JLabel in the north section) the image is transferred to TopPanel where it follows the mouse until the next mousePress when it is transfered to (imageList) DrawingPanel.
A mousePress on the panel, vis–a–vis the image label, initiates dragging and the drawing of a new Rectangle that is added to the DrawingPanel state (shapeList) after mouseRelease.
The ImageStore class is a DataStore to keep track of images and their location for rendering in DrawingPanel.