Beginning Java 2- JDK 1.3 Version Images and Animation
Synthesizing Images
You don't have to read all your images in from files – you can create your
own. The most flexible way of doing this involves using a
BufferedImage object. This is a subclass of the Image class
that has the image data stored in a buffer that you can access. It also
supports a variety of ways of storing the pixel data: with or without an
alpha channel, different types of color model and with various precisions
for color components. The ColorModel class provides a flexible
way to define various kinds of color models for use with a
BufferedImage object but to understand the basics of how this
works we will only use one color model, the default where color components
are RGB values, and one buffer type – storing 8 bit RGB color values plus
an alpha channel. This type is specified in the BufferedImage
class by the constant TYPE_INT_ARGB, which implies an int
value is used for each pixel. The value for each pixel stores an alpha
component plus the RGB color components as 8 bit bytes. We can create a
BufferedImage object of this type with a given width and
height with statements such as:
int width = 200;
int height = 300;
BufferedImage image = new
BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
This creates a BufferedImage object that represents an image
200 pixels wide and 300 pixels high. To draw on the image, we need a
graphics context, and the createGraphics() method for the
BufferedImage object returns a Graphics2D object that relates
to the image:
Graphics2D g2D = image.createGraphics();
Operations using the g2D object modify pixels in the
BufferedImage object, image. With this object available you
now have the full capability to draw on the BufferedImage
object – you can draw shapes, images, GeneralPath objects or
whatever, and you can set the alpha compositing object for the graphics
context as we discussed in the previous section. And you also have all the
affine transform capability that comes with a Graphics2D
object.
You can retrieve an individual pixel from a BufferedImage
object by calling its getRGB() method and supplying the x,y
coordinates of the pixel as arguments of type int. The pixel is returned as
type int in the TYPE_INT_ARGB format, which consists of four
8-bit values for the alpha and RGB color components packed into the 32-bit
word. There is also an overloaded version of getRGB() that returns an array
of pixels from a portion of the image data. You can set an individual pixel
value by calling the setRGB() method. The first two arguments
are the coordinates of the pixel, and the third argument is the value that
is to be set, of type int. There is also a version of this
method that will set the values of an array of pixels. We won't be going
into pixel operations further, but we will venture drawing on a
BufferedImage object with a working example.
We will put together an applet that uses a BufferedImage
object. The applet will create an animation of the buffered image object
that is created against a background of the Wrox logo. This will also
demonstrate how you can make part of an image transparent. The applet will
be broadly similar to applets we have produced earlier in this chapter; the
basic contents of the source file will look like this:
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
public class ImageDrawDemo extends JApplet
{
// The init() method to initialize everything...
// The start() method to start the animation...
// The stop() method to stop the animation...
// The ImagePanel class defining the panel displaying the animation...
// Data members for the applet...
}
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.
|