Java drawimage () method

Source: Internet
Author: User

Supported Image Distribution inJava. Applet, java. AWT, and Java. AWT. Image
Package. Each image is represented by a java. AWT. image object. In addition to the image class, the Java. AWT package providesOthersBasic image support, such as drawimage of the graphics class
Method, the getimage method of the Toolkit object, and the mediatracker class. In the java. Applet package, the getimage of the Applet Class
This method makes it easy to use URLs to load images. Finally, the Java. AWT. Image package provides
Interfaces and classes.

Attach Images
AWT can easily Load two formats of images: GIF and JPEG. The Applet and toolkit classes provide getimage
The two formats can be used. Use as follows:
Myimage = getimage (URL); // It can only be in the applet subclass
Or

Java code
  1. Myimage = toolkit. getdefatooltoolkit (). getimage (filenameorurl );
myImage = Toolkit.getDefaultToolkit().getImage(filenameOrURL);

Getimage
Return immediately, so you do not have to wait until the image is loaded and can perform other operations directly in your program. Although this can improve performance, effective programs require more control or more information about images. You
You can use the mediatracker class or the imageupdate method defined in the imageobserver interface to track image loading.
This section also shows you how to easily create images using the memoryimagesource class.

Show images
The graphics object passed to the paintcomponent method can easily display images. In Graphics
You can call a drawimage method on the object. For example:
G. drawimage (myimage, 0, 0,
This );

This section describes four forms of drawimage, two of which can be scaled. Like getimage, drawimage
It is also asynchronous, and returns immediately even if the image is not fully loaded or drawn.

Attach Images
This section describes how to obtain an image object corresponding to an image. As long as the image data is stored in one
If you know the file name or URL of a GIF or JPEG file, it is easy to get an image from it: you only need to use the applet or toolkit.
You can use the getimage method in. Return the getimage method immediately
Check whether image data exists. The actual image loading is usually performed only when the program draws the image for the first time.
For many programs, this invisible background LoadingWorkWork well. Otherwise, you must track the image loading process. This section explains how to use mediatracker
Class and imageobserver
Interface.

--------------------------------------------------------------------------------
Note:
The imageicon class automatically uses a mediatracker
Load your own images.
--------------------------------------------------------------------------------

Finally, this section shows you how to use a class like memoryimagesource to easily create an image.

Use the getimage Method
This section first discusses Toolkit
And then the applet's getimage method.
Toolkit describes two getimage methods:

Java code
  1. Image getimage (URL)
  2. Image getimage (string filename)
Image getImage(URL url) Image getImage(String filename)

The following is the getimage using toolkit.
Examples of methods. Although each Java program and Applet can use these methods, applets is subject to general security restrictions. Especially, untrusted applets
You cannot specify a file name for getimage because untrusted applets cannot load data from the local file system. You can
For security restrictions, find information about the restrictions on untrusted applets.

Java code
  1. Toolkit toolkit = toolkit. getdefatooltoolkit ();
  2. Image image1 = toolkit. getimage ("imagefile.gif ");
  3. Image image2 = toolkit. getimage (
  4. NewURL ("http://java.sun.com/graphics/people.gif "));
Toolkit toolkit = Toolkit.getDefaultToolkit();Image image1 = toolkit.getImage("imageFile.gif");Image image2 = toolkit.getImage(     new URL("http://java.sun.com/graphics/people.gif"));

The Applet Class provides two getimage
Method:

Image getimage (URL)
Image getimage (URL, string
Name)
Only applets can use the getimage method of applet. The getimage method of the applet is only available in the complete background.
(Appletcontext. For this reason, these methods cannot work in the constructor or in a declaration that declares an instance variable. You should start from
Call getimage in the sample method.

The following code example demonstrates how to use the code> getimage method of the applet. Reference
Use awt to create a GUI to get getcodebase and getdocumentbase
Method.

// In a method of the applet subclass:
Image image1 = getimage (getcodebase (),
"Imagefile.gif ");
Image image2 = getimage (getdocumentbase (),
"Animagefile.jpeg ");
Image image3 = getimage (
New
URL ("http://java.sun.com/graphics/people.gif "));

Request and tracking image loading: mediatracker
And imageobserver
You can track image loading in two ways: mediatracker class and imageobserver interface.
The mediatracker class is sufficient for many programs. You only need to create a mediatracker
Instance, telling it to track one or more images, and then querying mediatracker for the shape of those images if needed. In
There is an example in improving the appearance and performance of an image animation.
The animation example shows two particularly useful features of mediatracker: requesting the addition of a group of image data
And wait until they are loaded. To load a set of image data, you can use checkid and checkall with a Boolean parameter. Set the parameter to true to start loading.
Any image that has not been loaded. Alternatively, you can request to load the image and use the waitforid and waitforall methods to wait.

Imageobserver
You can run the API to perform closer tracking than mediatracker. The component class uses it, so the component re-draws the image to be displayed after it is loaded. For use
Imageobserver interface, you need to implement the imageupdate method of imageobserver and ensure that the implemented object is registered as an image observation
. Generally, this registration occurs when you specify an imageobserver for the drawimage method, as described in a later section. The imageupdate method is
Image Information is called whenever it is visible.

If you browse mediatracker API documentation, you may notice that component
Class defines two seemingly useful methods: checkimage and prepareimage. Mediatracker
Classes make these methods unnecessary to a large extent.

Use memoryimagesource to create an image
In memoryimagesource
With the help of image producers like this, you can construct images from your lucky hand graffiti. The following sample code creates a x image to indicate a gradient from black to Blue along the X axis and a gradient from black to red along the Y axis.
Int
W = 100;
Int H = 100;
Int [] pix = new int [w * H];
Int Index = 0;
For
(INT y = 0; y Int Red = (y * 255)/(h-1 );
For
(INT x = 0; x <W; X ++ ){
Int Blue = (x * 255)/(w-
1 );
PIX [index ++] = (255 <24) | (Red <16) |
Blue;
}
}
Image IMG = createimage (New memoryimagesource (W, H, pix,
0, W ));

Show images
The following code displays an image in the original size in the upper left corner (0, 0) of the component area:
G. drawimage (image, 0,
0, this );

The following code shows an image scaled to 300 pixels in width and 62 pixels in coordinates (90, 0:
G. drawimage (myimage,
90, 0,300, 62, this );

The following code snippet shows an applet loading an image and displaying it twice, using the two clips you see above. You can
Imagedisplayer. Java to find the complete code.

This image is the GUI of the applet. Click the image to run the applet. The applet is displayed in a new browser window.

Graphics
Class declares the following drawimage
Method. Both return a Boolean value, although this value is rarely used. If the image has been fully loaded and therefore completely drawn, the return value is true; otherwise, the return value is false.

Java code
  1. BooleanDrawimage (image IMG,IntX,IntY, imageobserver observer)
  2. BooleanDrawimage (image IMG,IntX,IntY,IntWidth,IntHeight, imageobserver observer)
  3. BooleanDrawimage (image IMG,IntX,IntY, color bgcolor, imageobserver observer)
  4. BooleanDrawimage (image IMG,IntX,IntY,IntWidth,IntHeight, color bgcolor, imageobserver observer)
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
The method has the following parameters:
Image img
The image to be drawn.
Int X, int y
Coordinates in the upper left corner of the image.
Int width, int
Height
The width and height (in pixels) of the image ).
Color
Bgcolor
The color below the image. This is useful if the image contains transparent pixels and will be displayed in the specified color background.
Imageobserver
Observer
One implementation of imageobserver
Interface object. It registers the object as an image observer, so it is notified when any new information of the image is visible. Most components can simply specify this.
The component can specify this as the image observer Because component
Class implements the imageobserver interface. When image data is loaded, its implementation calls the repaint method, which is usually what you expect.
Drawimage
The method is returned as long as the image data to be displayed has been loaded. If you want to ensure that drawimage only draws the complete image, you need to track the image loading.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.