Beginning Java 2- JDK 1.3 Version
Images and Animation
Obtaining an Image
We have already seen one way
to obtain an image from a file using the
ImageIcon class back in
Chapter 13. The
ImageIcon class doesn't
really care about the size of the image, that is, it is not constrained to be a
typical icon size, so an image represented by an object of this class can be
anything. We only considered one ImageIcon constructor when we read icons for
our toolbars buttons from a file. That constructor happened to accept a String
argument that specified the file name. There are a number of other ImageIcon
constructors, so here's the complete set:
| Constructor Description ImageIcon() |
Creates an uninitialized object that you must
initialize with an Image object before use.
You would do this by passing a reference to an Image object to the setImage()
method for the ImageIcon object. |
| ImageIcon(String filename) |
Creates an object from the file specified by filename, which
represents either an absolute path for the file, or a path relative to
the current directory. |
| ImageIcon(String filename, String description) |
As the constructor above, but stores a description of
the image specified by the second argument, and which can be
retrieved bycalling the getDescription() method
for the ImageIcon object. |
| ImageIcon(URL location) |
Creates an object specified by the file that is
located at the source specified by the argument. The URL
class represents a uniform resource locator specification that identifies a
source on the World Wide Web. We will come back to this class a little later
in the chapter. |
| ImageIcon(URL location, String description) |
Same as the previous constructor but adds a
description of the image that can be retrieved by calling the
getDescription() method for the ImageIcon object. |
| ImageIcon(Image image) |
Creates an ImageIcon
object from the Image object supplied
as the argument. We will discuss the Image
class later in this section. |
| ImageIcon(Image image, String description) |
Creates an ImageIcon
object from the Image object supplied
as the first argument. The second argument provides a description of the
image. |
| ImageIcon(byte[] imageData) |
Creates an object from the byte
array that must contain data containing an image in a supported format –
which can be GIF, PNG, or JPEG at the present time. The data can be read from
a file or created programmatically. |
| ImageIcon(byte[] imageData, String description) |
As the previous constructor but also stores a
description of the image. |
An ImageIcon object contains
a reference to an object of type Image as a member. The Image class is an abstract
class that is a superclass of all classes that represent images in Java, so a
reference of type Image
can refer to any instance of a graphical image being used. You can obtain a
reference to the Image
member of an ImageIcon
object by calling its getImage()
method. An object of type Image can be created from
data in GIF (Graphics Interchange
Format), PNG (Portable Network Graphics), or JPEG (Joint Photographic
Experts Group) format. So, whether the image data is passed to the constructor
as an array – as is the case for two of the ImageIcon constructors,
or is obtained from an external source such as a file or a URL
object, it must be a GIF, PNG, or JPEG representation of an image.
While
the GIF format for images is very common, it has some limitations. A GIF image
can contain a maximum of 256 different colors, albeit selected from a palette
of 16 million colors. The JPEG image format has been designed to store
photographs, and is a much more sophisticated way of representing a color
image. The sophistication comes at a price though. The complexity of the format
and the data compression technique used means that it takes much longer to
process than a GIF image, but it does make it feasible to transmit good quality
photographic images over the net. The JPEG compression technique is also
'lossy', so the quality is not as good as the original image. The PNG image
format is also much more flexible than GIF images and it stores images in a
lossless form. It is designed to be a portable image storage form for
computer-originated images. You can represent grayscale, indexed-color and true
color images in PNG format, and you can also include an alpha channel that
determines the transparency of the image when it is combined with others.
The constructors that create
the ImageIcon
object by referencing a String
object specifying a file name, or by referencing a URL object defining an
Internet source, always construct the internal Image object before
returning. When you specify a URL as the source of the image data, there can
be a considerable delay before the ImageIcon object creation
is completed, depending on how long it takes to retrieve the image data from
the source. Being able to create an ImageIcon object from a URL
object is particularly relevant to applets. You can retrieve any images, or
indeed any other external data that you use in your applet from an Internet
source. Since URLs are so important, let's take a brief detour into how URLs
and the URL
class work.
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.