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

Displaying the Image

The image is displayed by the ImagePanel object, so we need to implement the paint() method for this class to display the image normally when imageY is zero or negative, and deal with squashing the image when imageY is positive. This is going to be easy since we can use the drawImage() method we used in the previous example to draw the image normally, and use an overloaded version that is specifically intended for scaling an image on the fly to fit a particular area. The overloaded method has the following arguments:

When you call this method, the image is scaled on the fly to fit the destination area specified by the arguments. Since you specify the coordinates of the top- left of the image, as well as its width and height, it is possible to use this method to display part of an image, and fit it to the destination space that you specify. Like all the drawImage() methods, this method can draw part of an image when loading of the image is not complete. In this case the method returns false. The ImageObserver that is passed as the last argument – usually the applet itself – is notified when more of the image becomes available, with the result that the image is repainted. When the entire image has been drawn, the drawImage() method returns true.

It would also be useful to color the background with a color that contrasts with the image. With this in mind we can implement the paint() method for the ImagePanel class as:

public void paint(Graphics g)
{
  Graphics2D g2D = (Graphics2D)g;

  g2D.setPaint(Color.lightGray); // Set a background color
  g2D.fillRect(0, 0, imageWidth, imageHeight);  // paint background
  if(imageY<=0)
     g2D.drawImage(image, imageX, imageY, this); // Draw normally
  else  // or scaled...
     g2D.drawImage(image,  // The image
       imageX, imageY, imageWidth, imageHeight, // Destination
       0, 0, imageWidth, imageHeight,  // Image area
       this);  // Image observer
}

The fillRect() method fills the entire area of the applet with the color that we set in the setPaint() call, Color.lightGray. When imageY is greater than 0, we use the version of drawImage() that scales the image on the fly to fit the area available, from the imageY position to the bottom of the applet at the y coordinate, imageHeight.

It is worth noting that a JPanel object is double-buffered by default. This means that all rendering for a new image that is to be displayed is done in a buffer in memory, and only when image is complete are the pixels for the entire picture written to the screen. Since the existing image that is displayed is not altered while the new image is being created, this eliminates the flicker and flashing that can occur if your display buffer is updated incrementally while it is displayed.

If you have put together all the bits of code we have discussed, you should have a working applet.

Join us in one week for the next section!

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.