|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |
This is an image:The next few pages provide the details you'll need to work with images. You'll learn how to load, display, and manipulate them.
Wait!: Although the API this section describes is valid for both JDK 1.1 and Java 2 (JDK 1.2), we recommend that you use Swing's built-in iconsupport instead. If Swing icons aren't sufficient and you're writing a program for Java 2, then consider using the Java 2D API, which is described in the 2D Graphics
trail.
Support for using images is spread across the
java.applet,java.awt, andjava.awt.imagepackages. Every image is represented by ajava.awt.Imageobject. In addition to theImageclass, thejava.awtpackage provides other basic image support, such as theGraphicsdrawImagemethods, theToolkitgetImagemethods, and theMediaTrackerclass. Injava.applet, theAppletgetImagemethods make it easy for applets to load images using URLs. Finally, thejava.awt.imagepackage provides interfaces and classes that let you create, manipulate, and observe images.
The AWT makes it easy to load images in either of two formats: GIF and JPEG. TheAppletandToolkitclasses providegetImagemethods that work for either format. You use them like this:ThemyImage = getImage(URL); //in a method in an Applet subclass only or myImage = Toolkit.getDefaultToolkit().getImage(filenameOrURL);getImagemethods return immediately, so that you don't have to wait for an image to be loaded before going on to perform other operations in your program. While this improves performance, some programs require more control or information about image loading. You can track image loading status either by using theMediaTrackerclass or by implementing theimageUpdatemethod, which is defined by theImageObserverinterface.This section will also tell you how to create images on the fly by using the
MemoryImageSourceclass.
It's easy to display an image using theGraphicsobject that's passed into yourpaintComponentmethod. You simply invoke adrawImagemethod on theGraphicsobject. For example:This section explains the four forms ofg.drawImage(myImage, 0, 0, this);drawImage, two of which scale the image. LikegetImage,drawImageis asynchronous, returning immediately even if the image hasn't been fully loaded or painted yet.
|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |