Beginning Java 2- JDK 1.3 Version
Images and Animation
Implementing the imageUpdate() Method
Let's try out the whole applet in its revised guise:
import java.awt.*;
import javax.swing.*;
import java.net.*;
public class DisplayImage extends JApplet
{
public void init()
{
Image image = null;
try
{
// Image from a file specified by a URL
image = getImage(new URL(getCodeBase(),"Images/wrox_logo.gif"));
}
catch (MalformedURLException e)
{
System.out.println("Failed to create URL:\n" + e);
return;
}
int imageWidth = image.getWidth(this); // Get its width
int imageHeight = image.getHeight(this); // and its height
if(imageWidth != -1 && imageHeight != -1)   // If they are available
resize(imageWidth,imageHeight); // set applet size to fit
ImagePanel imagePanel = new ImagePanel(image); // Create panel showing the image
getContentPane().add(imagePanel); // Add the panel to the content pane
}
public boolean imageUpdate(Image img, // Reference to the image
int flags, // Flags identifying what is available
int x, // x coordinate
int y, // y coordinate
int width, // Image width
int height) // Image height
{
if((flags & WIDTH) > 0 && (flags & HEIGHT) > 0)
{
resize(width,height); // Set applet size to fit the image
repaint(); // Repaint the applet in its new size
return false;
}
else
return true; // More info required
}
// Class representing a panel displaying an image
class ImagePanel extends JPanel
{
public ImagePanel(Image image)
{
this.image = image;
}
public void paint(Graphics g)
{
g.drawImage(image, 0, 0, this); // Display the image
}
Image image; // The image
}
}
How It Works
The effect of the applet will appear much the same as before
because the image file is local so everything will happen quite quickly. You can
verify that the image data is not available in the init()
method for the applet by commenting out the if
that checks for a value of . 1 for the width
or the height
before calling resize().
Without this check, the resize()
method will be called with either or both arguments as . 1 so the size of the
applet will briefly become miniscule and the applet viewer window along with it.
A further experiment will show that our imageUpdate()
method in the DisplayImage
class is only called if we have previously requested image data that is not
available. Modify the code in the init()method that calls the methods to obtain
the image size like this:
int imageWidth = 30, imageHeight = 30; // Added...
// imageWidth = image.getWidth(this); // Get its width
// imageHeight = image.getHeight(this); // and its height
// if(imageWidth != -1 && imageHeight != -1
// resize(imageWidth,imageHeight); // Set applet size to fit the image
Now the applet will not be resized at all and the image will
not be displayed. If you uncomment either or both of the statements calling
imageWidth() and imageHeight() and recompile, everything will work as it should. To see when imageUpdate()
is called, you could add a call to System.out.println() at the beginning of the method.
The default imageUpdate() method is called automatically for the ImagePanel
object because the paint() method calls drawImage() and identifies the object as the image observer. The default version calls repaint() as new data becomes available, so over a slow link our image will be drawn incrementally.
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.