Hi,
I'm trying to program a puzzle where one can drag pieces with the mouse.
Here is were I am: www.playart.org/applet
This solution is pretty slow because I use a double buffer and draw all the
Pieces for every movement again.
So I'm trying to erase just the last moved piece and paint it at the new
position.
The big problem is: with fast mouse movements the pieces are not completely
erased and some parts stay on the screen - I guess this is because the
images are not erased and drawn completely.
How can I wait till the Image I am trying to draw with
drawImage(Graphics, Image,x,y,ImageObserver)
is completely drawn?
I already implemented the ImageObserver interface and tried to check with
imageUpdate
but the method imageUpdate is never called.
public boolean imageUpdate(Image img, int infoflags, int x, int y, int
width, int height) {
boolean done = ((infoflags & (ERROR | FRAMEBITS | ALLBITS)) != 0);
...
}
Thanks
Sven
Remi Bastide - 17 May 2005 09:34 GMT
>Hi,
>I'm trying to program a puzzle where one can drag pieces with the mouse.
[quoted text clipped - 7 lines]
>erased and some parts stay on the screen - I guess this is because the
>images are not erased and drawn completely.
Well, it's not THAT slow... I doubt that double buffering is much of
an issue here. Have you tried using the repaint(int x, int y, int
width, int height) version of repaint and use the cliprect ?
see http://mindprod.com/jgloss/repaint.html
for details.
Larry Barowski - 17 May 2005 21:41 GMT
> Hi,
> I'm trying to program a puzzle where one can drag pieces with the mouse.
[quoted text clipped - 7 lines]
> erased and some parts stay on the screen - I guess this is because the
> images are not erased and drawn completely.
Here is one strategy for moving objects:
When the user begins to drag a piece, erase the piece from your
image buffer.
When the user stops dragging a piece, paint it to your image
buffer.
When the user drags a piece, repaint() the old and new positions.
For painting, paint the image buffer (only the part for the clip
bounds of the Graphics), and the moving piece (if any) on top
of that.
But that is a general strategy. You have a better situation,
since the pieces can never overlap. So maybe:
When the user drags a piece, copy the dragged portion of
the image buffer to its new location. Erase the difference
between the old and new areas. You can use
SwingUtilities.computeDifference() to find the difference.
repaint() the old and new areas.
For painting, paint the part of the image buffer corresponding
to the clip bounds of the graphics.
hiwa - 18 May 2005 23:48 GMT
Well-written game.
I'm impressed with its simplicity and elegance.
Will you make it open-source?
We could learn a lot from it.