Reviews : Java Books :
Beginning Java 2- JDK 1.3 Version : Images and Animation

Buy this book
Title: Beginning Java 2- JDK 1.3 Version
ISBN: 1861003668
US Price: $ 49.99
Canadian Price:
C$ 74.95
UK Price: £ 35.99
Publication Date: March 2000
Pages: 1230
© Wrox Press Limited, US and UK.

Beginning Java 2- JDK 1.3 Version
Images and Animation

Creating an Image

A sprite is a small graphical image that you can draw over a static image to create an animation. To create the animation effect, you just draw the sprite in different positions and orientations over time, and of course transformations of the coordinate system can be a great help in making this easier. Games often use sprites – they can make the animation take much less processor time because you only need to draw the sprite against a static background. Our interest in using BufferedImage objects means we won't get into the best techniques for minimizing processor time. We will instead concentrate on understanding how we can create and use images internally in a program.

Our BufferedImage object is going to look as shown below.

image7-1

The image is a square with sides of length spriteSize. Dimensions of other parts of the image are relative to this. There are really only two geometric entities here, a line and a circle, each repeated in different positions and orientations, so if we create a Line2D.Double object for the line, and an Ellipse2D.Double object for the circle, we should be able to draw the whole thing by moving the user coordinate system around and drawing one or other of these two objects.

A true object-oriented approach would define a class representing a sprite, possibly as a subclass of BufferedImage, but since we are exploring the mechanics of using a BufferedImage object, it will suit our purpose better to develop a method, createSprite(), that draws the sprite on a BufferedImage object. The method will just be a member of our applet class so we will add data members to the applet to store any data required. You can plug the data members we will be using into the applet class outline now:

double totalAngle;  // Current angular position of sprite
double spriteAngle; // Rotation angle of sprite about its center
ImagePanel imagePanel;  // Panel to display animation

BufferedImage sprite;  // Stores reference to the sprite
int spriteSize = 100;  // Diameter of the sprite
Ellipse2D.Double circle; // A circle - part of the sprite
Line2D.Double line;  // A line - part of the sprite

// Colors used in sprite
Color[] colors = {Color.red , Color.yellow, Color.green  , Color.blue,
Color.cyan, Color.pink  , Color.magenta, Color.orange};

java.util.Timer timer; // Timer for the animation
long interval = 50; // Time interval msec between repaints

The general use of these members should be clear from the comments. We will see how they are used as we develop the code.

The first thing the createSprite() method needs to do is to create the BufferedImage object, sprite, and we will need a Graphics2D object to use to draw on the sprite image. The code to do this is as follows:

BufferedImage createSprite(int spriteSize)

{
  
  // Create image with RGB and alpha channel

  BufferedImage sprite = new BufferedImage(spriteSize, spriteSize,
                  BufferedImage.TYPE_INT_ARGB);


  Graphics2D g2D = sprite.createGraphics(); // Context for buffered image

  // plus the rest of the method...

}

The sprite object has a width and height of spriteSize, and the image is of the type TYPE_INT_ARGB, so the alpha and color components for each pixel will be stored as a single int value, and the color will be stored as 8 bit red, green and blue components. This means that our sprite image will occupy 40,000 bytes, which is a small indication of how browsing a web page can gobble up memory. This doesn't affect the download time for the page – this memory is allocated in the local machine when the applet is executed. Apart from the contents of the HTML file that is the page, the download time is affected by the size of the .class file for the applet, plus any image or other files that it downloads when it executes.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.