|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
General Java Advanced Animation Techniques
Advanced Animation Techniques
By: Alan Williamson
Mar. 1, 1997 12:00 AM
Java is a programming language. Nothing complicated so far. And just like all other computer languages, it will not make a bad programmer into a good programmer. All the same techniques picked up while coding in, say, C or C++, generally can be applied to the same program in Java. But what happens if you're not coming from an experienced coding background. You don't have this wealth of knowledge. Fortunately or unfortunately, depending on your side of the fence, when it comes to coding multimedia applets many experienced developers are back at square one with the novices. The problem is that in pre-Web days, the majority of programmers never needed to worry about image manipulation procedures. But now, it would be rare for a Java programmer not to have coded some sort of image processing applet. Whether it is displaying a logo or animating a series of images, the problems that can occur are the same. Granted, unlike many other languages, Java makes displaying images very easy, however, without careful consideration, the resulting applet / application may look far from professional. This article looks at bridging the gap between a poorly executed flickery animation and a smooth, professional, flicker-free animation. The techniques presented here are not tied to just the animation field, but can be employed in applications where graphics are used in general.
Loading Images It is much better to transfer one big file as opposed to lots of small ones, as the throughput of the connection will increase - more time transferring data and less time transferring file headers. For this reason, we will take all thirty images, place them one under another and store them in the same file. We now have one long strip of images that can be loaded into memory as one file. The next step is to split this image into thirty smaller images. This is performed in a two stage process (see Listing 1). The first step is to load the image referenced by imgStip into an array of integers. This is performed using the java.awt.image.PixelGrabber class, a very powerful class,that allows, either, the complete image or a subset of the image to be grabbed and stored in an array. There are two different constructors for this class, but the one employed here has the following parameters:
public PixelGrabber( Image img, //- Image that holds the pixels
Pixel Representation An AA value of 0x00 indicates the pixel is transparent. Before going to the next step in creating the new images, a simple image filter can be applied to the integer array. For example, one can either lighten or darken the image by simply increasing or decreasing the value of the RGB values. The code presented in Listing 2 increases the darkness or brightness by a given percentage. Since the pixels are simply an array of integers, many filters can be applied before creating the final image. For example, a simple filter could be written to swap the RED components with the GREEN components, or to eliminate all the RED components from the image. You could even randomize this to create a completely new image, based on the original every time.
Image Creation For this example, the constructor in Table 1 is used. The next step in creating a series of images is not a big one. In order to flick through the images in a sequence, the images are best stored in an array. The code in Listing 4 first creates a placeholder for thirty images, and then runs through a simple loop to create the individual images from the array. Notice how the offset parameter is calculated so it returns the starting index of each image. Each image is 30 x 30 pixels, or 600 pixels in total. The starting index of each image is therefore 0, 600, 1200, 1800, ... etc. into the array.
Flicker Elimination The majority of developers simply override the java.awt.Component.paint(Graphics G) method by placing all of their graphic operations within. This method is called every time the component needs refreshing, whether it originates from a manual update or via a system call, for example when the window is maximized. However, by just using the paint() method, the component may suffer from flicker. This is due to the processing of the component class that ensures the area is restored to its original background colour and graphic context before any painting is performed. A very handy feature, you may think - clean canvas every time. However, if the original color is white and the proceeding paint results in a dark image, the flicker of the update will be very apparent. What you need to do is to stop the component from restoring the area to its original glory, a thank you, but no thank you approach. The job of restoration is given to the java.awt.Component.update(Graphics G) method, and by overriding this method, with a call to paint() will stop the area from being reset (see Listing 5).
Double Buffering Adding double buffering to an applet does not require many additional lines and should be employed in most instances. The first step (Listing 6) is to create an off-screen image and retrieve a graphics handle to it, to allow painting to it. The variables, imgScreen and offScreenGraphics, should remain in scope throughout the life of the class, as it is more efficient to create them once and re-use them, as opposed to re-creating every time a paint is required. Notice the use of the createImage() method again. However, this variation of the method allows the creation of a blank image of a given width and height. The next step is to get a handle to the graphics context that will enable painting directly to the image. Now, every time the paint() method is called, instead of painting into the graphics context passed in, all operations go to the newly created memory image, with the final statement transferring this image to the component (see Listing 7).
Conclusion Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||