Java Applet programming to display images

Source: Internet
Author: User

[Document Introduction] Java Applet is often used to display images stored in GIF files.
Java Applet is often used to display images stored in GIF files. It is very easy to load GIF images using Java Applet. When using Image files in the Applet, You need to define the Image object. Most Java applets use image files in GIF or JPEG format. The Applet uses the getImage method to associate an Image file with an Image object.



The drawImage method of the Graphics class is used to display the Image object. To improve the image display effect, many applets use dual-buffering technology: first, the image is loaded into the memory and then displayed on the screen.

The Applet can use the imageUpdate Method to Determine the memory size of an image.

  Load an image

Java also treats images as Image objects. Therefore, when loading images, you must first define an Image object. The format is as follows:

Image picture;
Then, use the getImage method to associate the Image object with the Image file:

picture=getImage(getCodeBase(),"ImageFileName.GIF");  
The getImage method has two parameters. The first parameter is the call to the getCodeBase method. This method returns the URL of the Applet, such as www.sun.com/applet. The second parameter specifies the image file name loaded from the URL. If the graph file is located in a subdirectory under the Applet, the corresponding directory path should be included in the file name.

After the image is loaded using the getImage method, the Applet can display the image using the drawImage method of the Graphics class, as shown below:

g.drawImage(Picture,x,y,this);
The parameters of the drayImage method indicate the image to be displayed, the x and y coordinates in the upper left corner of the image, and this.

The fourth parameter is to specify an object that implements the ImageObServer interface, that is, the object that defines the imageUpdate method (this method will be discussed later ).

Show image. java)

// Source code list
Import java. awt .*;
Import java. applet .*;
Public class ShowImage extends Applet
Image picure; // a member variable defining the Image type
Public void init ()
{
Picture = getImage (getCodeBase (), "Image.gif"); // load the image
}
Public void paint (Graphics g)
{
G. drawImage (picture, this); // display the image
}
}

To this end, the Applet statements in the HTML file are as follows:

<HTML>
<TITLE> Show Image Applet </TITLE>
<APPLET
CODE = "ShowImage. class" // The class file name is ShowImage. class.
WIDTH = 600
HEIGHT = 400>
</APPLET>
</HTML>

When the Applet is run after compilation, the image is not in one breath. This is because the program does not load and display the image completely before the drawImage method returns. In contrast, the drawImage method creates a thread, Which is concurrently executed by the original execution thread of the Applet. It is loaded and displayed on one side, resulting in this discontinuous phenomenon. To improve the display effect. Many applets use the image dual-buffering technology, that is, they first load the image into the memory and then display it on the screen, so that the image can be displayed in one breath.
Dual-buffering Image

In order to improve the image display effect, double buffering technology should be used. First, load the image into the memory and then display it in the Applet window.

BackgroundImage. java)

// Source code list
Import java. awt .*;
Import java. applet .*;
Public class BackgroundImage extends Applet // inherits the Applet
{
Image picture;
Boolean ImageLoaded = false;
Public void init ()
{
Picture = getImage (getCodeBase (), "Image.gif"); // load the image
Image offScreenImage = createImage (size (). width, size (). height );
// Use createImage to create an Image object
Graphics offScreenGC = offScreenImage. getGraphics (); // obtain the Graphics object
OffScreenGC. drawImage (picture, 0, 0, this); // display non-screen images
}
Public void paint (Graphics g)
{
If (ImageLoaded)
{
G. drawImage (picture, 0, 0, null); // display the image. The fourth parameter is null, not this
ShowStatus ("Done ");
}
Else
ShowStatus ("Loading image ");
}
Public boolean imageUpdate (Image img, int infoflags, int x, int y, int w, int h)
{
If (infoflags = ALLBITS)
{
ImageLoaded = true;
Repaint ();
Return false;
}
Else
Reture true;
}
}

The init method of the Applet shows that the method first defines an Image object named offScreenImage and assigns it the return value of the createImage method, then, a Graphics object named offScreenGC is created and assigned to its graphic environment-non-screen images will be generated by it. Because the image is a non-screen image, no image is displayed in the Applet window.

Every time the Applet calls the drawImage method, drawImage creates a thread that calls the imageUpdate method. In the imageUpdate method, the Applet can determine the memory size of an image. The thread created by drawImage continuously calls the imageUpdate method until the method returns false.

The second parameter of the imageUpdate method, infoflags, enables the Applet to know how images are loaded into memory. This parameter is equivalent to setting ImageLoaded to true and calling the repaint method to redraw the Applet window. This method returns false to prevent the execution thread of drawImage from calling the imageUpdate method again.

The operations of the Applet in the paint method are controlled by the ImageLoaded variable. When this variable is set to true, the paint method calls the drawImage method to display the image. The paint method uses null as the fourth parameter when calling the drawImage method. This prevents drawImage from calling the imageUpdate method. Because the image has been loaded into the memory, the image can be displayed in one go in the Applet window.

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.