Java 2D Graphics Finally, the initial release of
Java 2D Graphics Finally, the initial release of the 2D API may not be particularly fast. Sun was concentrating hard on getting the API design correct, which means they weren’t worrying as much about fixing bugs or making it run fast. Subsequent releases of the JDK should get better and better. And the elusive vaporware called HotSpot is supposed to improve performance dramatically some time in the Java 2 era. See http://java.sun.com/products/hotspot/ for more information. The bottom line is that it’s extremely difficult to pin down exactly what will make an application run quickly or slowly. As you read this chapter, keep your wits about you. Performance analysis and tuning is as much an art as a science. A healthy dose of common sense will take you a long way. There are tools that will help you analyze your application to find out what parts take the most time. These are called profilers. But as of this writing, profilers aren’t available for the Java 2 platform yet. 14.2 See for Yourself To see the effect of 2D operations on animation speeds, let’s develop an animation application framework. Through the rest of the chapter, I’ll develop classes that plug in to this framework to perform animation. The framework is composed of two classes, AnimationComponent and AnimationFrame. AnimationFrame serves as a container for Animation-Component and displays the current animation rate. 14.2.1 AnimationComponent The AnimationComponent class is an abstract subclass of java.awt.Component. It contains one additional method, timeStep(), that is called every time the animation should be moved ahead by a single frame. Creating a subclass of AnimationComponent is as simple as defining two methods: public void timeStep() This method is called before every frame of the animation. Your subclass should update its internal state appropriately. For a bouncing ball animation, for example, this method might update the position of the ball. public void paint(Graphics g) This is the paint() method defined in Component. This method should render the component’s current state. For the bouncing ball animation, this method should draw the ball in its current location. AnimationComponent is a Runnable, which means it can be placed in its own thread. The run() method is a tight loop that renders the current frame (by calling render()) and prepares for the next frame (by calling timeStep()). It also calculates a frame rate. I’ll explain how that works a little later. public void run() { while (mTrucking) { render(); timeStep(); page 254
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services