30
Sep
Black Art of Java Game Programming by Joel
Figure 2-3 Broadway execution path The animation thread operates independently once its start() method is called, and stops executing in response to stop(). And while the animation thread is running, it causes the rectangle to jiggle around. Why is all this code needed to create a simple animation? For example, why could you not just put the animation loop in the start() method, like this: public void start() { while (true) { repaint(); updateRectangle(); try { Thread.sleep (REFRESH_RATE); } catch (Exception exc) { }; } As an experiment, try running Broadway with this rogue start() method. It won t work, and the screen will stay blank! The reason is that the code you write in an applet works in conjunction with a lot of other code that you ve inherited. If your start() method loops forever, instructions after the start() method are never executed. The result is that the screen stays blank! Figure 2-4 makes this clear. It shows what happens when you use the infinitely looping start() method, instead of the start() method that creates another thread of execution. Figure 2-4 Broadway execution path with infinitely looping start() method This should illustrate another point of creating graphics with Java: when paint() is called, the painting doesn t occur simultaneously, but usually a short while later. If you call paint() too many times per second, Java can t keep up, and it executes the most recent paint() to catch up (ignoring the previous paint() requests). As a result, there s a limit on how fast a frame rate you can achieve; beyond that, the quality of the animation suffers. You can test the limits of your particular machine by setting the frame rate to a really high number and seeing what happens. (Try a pausing interval of less than 5 milli-seconds.) Even with the frame rate at a reasonable number, say 10 15 fps, you ll notice an annoying flicker that mars the animation. Let s find out how to get rid of it and improve animation performance by using two techniques: double-buffering and clipping. Improving Animation Quality
Note: If you are looking for inexpensive but high quality provider to host and run your jsp application check Astra jsp hosting services
