File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes Increasing Speed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Increasing Speed" Watch "Increasing Speed" New topic

Increasing Speed

Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
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.
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
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).
Isuru Sampath
Ranch Hand

Joined: Jun 26, 2003
Posts: 57

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.

Quoting the
Java Applets Tute

No Winds No Waves
Isuru Sampath
Ranch Hand

Joined: Jun 26, 2003
Posts: 57
I hope this complete example would help better; from Java Examples in a Nutshell - Orielly publishing
I agree. Here's the link:
subject: Increasing Speed
It's not a secret anymore!