Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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 icon support 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.image
packages. Every image is represented by ajava.awt.Image
object. In addition to theImage
class, thejava.awt
package provides other basic image support, such as theGraphics
drawImage
methods, theToolkit
getImage
methods, and theMediaTracker
class. Injava.applet
, theApplet
getImage
methods make it easy for applets to load images using URLs. Finally, thejava.awt.image
package 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. TheApplet
andToolkit
classes providegetImage
methods 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);getImage
methods 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 theMediaTracker
class or by implementing theimageUpdate
method, which is defined by theImageObserver
interface.This section will also tell you how to create images on the fly by using the
MemoryImageSource
class.
It's easy to display an image using theGraphics
object that's passed into yourpaintComponent
method. You simply invoke adrawImage
method on theGraphics
object. For example:This section explains the four forms ofg.drawImage(myImage, 0, 0, this);drawImage
, two of which scale the image. LikegetImage
,drawImage
is 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
Feedback Form |