Java Reference
In-Depth Information
On the Web
The program VGraphics.java is located in the Chapter 27 folder at
www.crcpress.com .
Raster Graphics
Raster or bitmap graphics is not based on geometrical figures but on images
stored dot-by-dot. Raster images are a rectangle of individually colored
dots. Raster images can be stored in GIF or JPEG format and displayed in
Java programs. This technology is suitable for displaying photographs or
other images defined in a pattern of individual dots.
The image object
In order to display an image stored in a Java-compatible file the application
must first create an Image object. This is accomplished by first retrieving
the default Java AWT toolkit and then using the getImage() method of the
Toolkit class. For example, if you wish to display an image stored in a file
named “stars.gif” you start by creating the image object, as follows:
Image hstImage =
Toolkit.getDefaultToolkit().getImage(“stars.gif”);
Displaying the bitmap
You can display an image called HST_1(of the class Image) using the
drawImage() method of the Graphics class, for example:
g.drawImage(HST_1, 120, 350, 200, 200, this);
————- ———— ———— ——
| | | |___ image observer
| | |___________ scaling rectangle
| |_____________________ x/y location
|_______________________________ image object
The fifth parameter in the call to drawImage() is an ImageObserver in-
terface. When the call to drawImage() takes place Java starts a new pro-
gram thread to load the requested image. The image observer is notified
by the thread when the image data is acquired. Since the Component
class implements the ImageObserver interface, we can use the “this” op-
erator as an image observer in the call to drawImage().
The following program, named RGraphics.java, demonstrates the dis-
play of a bitmap file in Java.
Search WWH ::




Custom Search