Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Here is a code example that displays an image at its normal size in the upper left corner of the component area (0, 0):Here is a code example that displays an image scaled to be 300 pixels wide and 62 pixels tall, starting at the coordinates (90, 0):g.drawImage(image, 0, 0, this);The following snapshot shows an applet that loads a single image and displays it twice, using both code snippets that you see above. You can find the program's full code ing.drawImage(myImage, 90, 0, 300, 62, this);ImageDisplayer.java
.
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.The
Graphics
class declares the followingdrawImage
methods. They all return a boolean value, although this value is rarely used. The return value istrue
if the image has been completely loaded and thus completely painted; otherwise, it'sfalse
.The
boolean drawImage(Image img, int x, int y, ImageObserver observer)
boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)
boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)
drawImage
methods have the following arguments:The reason why components can specify
Image img
- The image to paint.
int x, int y
- The coordinates of the upper left corner of the image.
int width, int height
- The width and height (in pixels) of the image.
Color bgcolor
- The color to paint underneath the image. This can be useful if the image contains transparent pixels and you know that the image will be displayed against a solid background of the indicated color.
ImageObserver observer
- An object that implements the
ImageObserver
interface. This registers the object as the image observer so that it's notified whenever new information about the image becomes available. Most components can simply specifythis
.this
as the image observer is that theComponent
class implements theImageObserver
interface. Its implementation invokes therepaint
method as the image data is loaded, which is usually what you want to happen.The
drawImage
method returns after displaying the image data that has been loaded, so far. If you want to make sure thatdrawImage
paints only complete images, then you must track image loading. See the previous page for information on tracking image loading.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.