It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes Stamping an Image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Stamping an Image" Watch "Stamping an Image" New topic

Stamping an Image

Aaron Roberts
Ranch Hand

Joined: Sep 10, 2002
Posts: 174
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?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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.
I agree. Here's the link:
subject: Stamping an Image
It's not a secret anymore!