Stamping an image

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?

  1. Replace the cursor icon with the stamp, so that the cursor IS the stamp. Let the OS do all the work for you! (nb: last time I researched this there were still some OS-specific bugs. However, I have used it effectively in several apps…)

Other than that, I would suggest that a more generic version of 2 is your best bet since separating your rendering into multiple discrete stages in a pipeline, each overwriting the output of the next, is a more effective way to work. GPG5 (published next March) will have a gem describing a framework which (amongst other things) does this, complete with java source code (I know this because I’m writing it ;)). Although I’m trying to do my example-code rendering with OGL rather than J2D (so that it’s more immediately useful to C++ games devs).

I actually tried the cursor idea, as that was my first thought - let the OS do the work too!! The only issue is that my stamp can only be a max size, depending on which OS I’m on. Windows allowed me 32x32, but I haven’t checked Linux or Mac yet ( don’t have a Mac to check against)

I’d like to use stamps up to say 100x100.

I’m guessing plan 2 would be better, since it involves moving less memory around than the first case.

Any ideas on the flickering potential?

Would a BufferedImage or a VolatileImage be the better back buffer? My guess is that VolatileImage, but from some other posts, I haven’t see the difference I should. (http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=2D;action=display;num=1089746638 ) you can read the post at javaranch for someones test code.