Beginning Java 2- JDK 1.3 Version
Images and Animation
Displaying an Image
You display an image in
essentially the same way as you display anything else – by calling a method for
a Graphics
or Graphics2D
object. The drawImage() method will
draw an image in a graphics context, so from this you can immediately deduce
that you can draw images on any components you like. Let's go straight away to
trying this out in a working example using an ImageIcon object in the
first instance.
Try It Out – Displaying an Image
We will implement an applet that creates an ImageIcon
from a file in the same directory as the applet's .class file. The
code here will refer to an image file wrox_logo.gif. If you
want to use this file, you can download it from the Wrox web site.
Alternatively you can use your own .gif or .jpg
file. Here's the code for the applet:
import java.awt.*;
import javax.swing.*;
import java.net.*;
public class DisplayImage extends JApplet
{
public void init()
{
ImageIcon icon = null;
try
{
icon = new ImageIcon(new URL(getCodeBase(),"Images/wrox_logo.gif"));
}
catch(MalformedURLException e)
{
System.out.println("Failed to create URL:\n" + e);
return;
}
int imageWidth = icon.getIconWidth(); // Get icon width
int imageHeight = icon.getIconHeight(); // and its height
resize(imageWidth,imageHeight); // Set applet size to fit the image
// Create panel a showing the image
ImagePanel imagePanel = new ImagePanel(icon.getImage());
getContentPane().add(imagePanel); // Add the panel to the content pane
}
// 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
}
}
You will need an HTML file to
run the applet. You could call it DisplayImage.html, and make the contents:
<applet
code="DisplayImage.class" width=50 height=50></applet>
This just sets the width and height parameters for the
applet to arbitrary values. The applet will resize itself to accommodate the
image. When I ran this with appletviewer,
it displayed the window below. (Although you may prefer to refer back to
Chapter 1, which explains how to run the applet in a browser using the
plug-in.)
How It Works
We retrieve the icon from the file wrox_logo.gif in the Images
directory relative to the URL that holds the code for the applet. The URL
class constructor can throw an exception, so we must arrange to catch
exceptions of type MalformedURLException
here in order to get the code to compile. The ImageIcon class
constructor does everything necessary to obtain the data from the file, and
uses it to create the image. The constructor will only return when this has
been completed.
The getIconWidth() and getIconHeight() methods
for the ImageIcon
object return the width and height of the image respectively, and we use these
as arguments to the resize() method for the JApplet
object, to adjust the size of the applet to accommodate the image.
We have to remember that an applet based on the JApplet class is similar
to a window based on the JFrame
class in that anything we want to display in an applet must be added to the
content pane for the applet object. For this reason we define an inner class, ImagePanel,
that will draw the image for the ImageIcon object, and we can add an object of
this class to the content pane for the applet. The ImagePanel class is quite
simple. The constructor saves the reference to the Image object that is
passed to it in a data member of the class, and the paint() method calls drawImage()
to display the image.
Note that no cast to Graphics2D is necessary
in the paint()
method since the drawImage()
method that we are using is defined in the Graphics class. There are
several other overloaded versions of this method, including two that are
defined in the Graphics2D
class, so when you want to use these, a cast will be necessary. The arguments
to the version of drawImage() that we use
here are:
- A reference to the Image object to be drawn
- The coordinates of the position where the image is to be drawn
- A reference to an ImageObserver object
An image is defined by its top-left corner point, so in our
applet we place the top-left corner of the image at the origin of the user
coordinate system for our panel. An ImageObserver object is
an object of a class that implements the ImageObserver interface,
and since the Component
class implements this interface, any component, including objects of our inner
class ImagePanel,
are ImageObserver
objects. The ImageObserver
interface handles the process of accessing image data from Internet sources
where there may be a considerable time delay in loading the image. This
interface provides the means to allow you to determine whether an image has
been loaded or not, and we will come back to this a little later in this
chapter.
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.