Is there any universal way to increase the speed of an applet. Its been double buffered and the speed slowed down tremendously. Can the speed be fixed?
Live And Let Learn.
Joined: Nov 13, 2002
Excuse me if i'm stating the obvious here... But I was wondering whether it's possible to 'double buffer' only a part of your screen ? I know java should allow it, but I really don't remember the exact API...
The concept, however, is simple: Say you have a background image (sky, trees...). And there's an animated person running against that backgound... This little person would make steps of (say) 5-10 pixels each time. So there's not need to re-paint the entire background - just the little square that's a little larger than the person, and contains both his pervious position and next position...
As I said, I don't remember the API for it, but it's bound to have something to do with Component.repaint(x,y,width,height).
Joined: Jun 26, 2003
Double Buffering and Clipping
To create smoother graphics you can use two techniques: double buffering and clipping.
Double buffering is the technique of doing all your drawing into an offscreen buffer, and then copying the contents of the buffer to the screen all at once. This prevents flickering that would otherwise result from erasing and redrawing the figure. The buffer is an offscreen Image created with the createImage method from the java.awt package. To draw an image offscreen, you simply use its Graphics object, which is obtained with the getGraphics method of the java.awt.Graphics class. Once the drawing is complete in the offscreen image, the applet can draw it in its screen.
Clipping is the second important technique for smoother graphics. It often happens that only a small region of a larger area has to be erased and redrawn. However, the paint method only knows how to redraw the entire applet window, not small regions of it. We can set a small rectangular clipping region in the Graphics object we use. This would tell the system that it only needs to draw within the specified rectangle and that it can ignore any drawing outside of this rectangle. When we know the area of an applet that needs to be redrawn, we can specify a clip rectangle that surrounds the area and do a full redraw, knowing that the applet will only redraw the portion inside the clipping rectangle. This method of drawing leads to much faster and less flickering redrawing.