move sprite on an image

Hi

I just want to move a sprite on an image, I use BufferStrategy to redraw the whole scene, ok, it has no problem. But I know that there is a better way to only repaint the sprite rectangle in every frame loop without needing to redraw everything. But…When I use clearRect(…), the background image will also be erased? What is the correct way to do the right thing???

Don’t use clearRect().

use drawImage(), with a clip rect or a variant of drawImage that takes a source and destination rectangle as well to paint only the region of the background that is where the old positon of the sprite is.

Thanks for your advice. I solve the problem with the second method. And in addition I still have two questions:

  1. I can’ t quite understand the “drawImage() with a clip”, could you show me some code?

public abstract boolean drawImage(Image img,
int dx1,
int dy1,
int dx2,
int dy2,
int sx1,
int sy1,
int sx2,
int sy2,
ImageObserver observer)

What about the performance of this method above? I know that the copy rectangle operation will take some system time rather than directly draw the entire image to the screen.

Thank you again :stuck_out_tongue:

  1. http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics.html#setClip(int,%20int,%20int,%20int)

  2. I don’t know, it might be slower If it isn’t fast enough, profile the application. Just try the various methods and time them to see for sure what is most efficient. You may find that the origninal method that redraws the whole screen works just as well as anything else.