Java Applet is commonly used to display images stored in GIF files. It is very simple to load GIF images in Java Applet. When using image files in 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 the image file with the Image object.
The drawImage method of the Graphics class is used to display Image objects. In order to improve the display effect of images, many Applets use double buffering technology: first load the image into the memory, and then display it on the screen.
The applet can determine how much of an image is already in memory through the imageUpdate method.
Loading an image
Java treats images as Image objects, so when loading an image, you need to first define the Image object. The format is as follows:
Image picture;
and then use the getImage method to connect the Image object and the image file:
picture=getImage(getCodeBase( ),"ImageFileName.GIF");
The getImage method has two parameters. The first parameter is a call to the getCodeBase method, which returns the URL address of the Applet, such as www.sun.com/Applet . The second parameter specifies the name of the image file to load from the URL. If the image file is located in a subdirectory under the Applet, the corresponding directory path should be included in the file name.
After loading the image using the getImage method, the Applet can use the drawImage method of the Graphics class to display the image. The form is as follows:
g.drawImage(Picture,x,y,this);
The parameters of the drayImage method specify the image to be displayed. The x and y coordinates of the upper left corner of the image and this.
The purpose of the fourth parameter is to specify an object that implements the ImageObServer interface, that is, an object that defines the imageUpdate method (this method is discussed later).
Display image (ShowImage.java)
//Source program list
import java.awt.*;
import java.applet.*;
public class ShowImage extends Applet
Image picure; //Define member variables of type Image
public void init()
{
picture=getImage(getCodeBase(),"Image.gif"); //Load image
}
public void paint(Graphics g)
{
g.drawImage(picture,0,0,this); //Display image
}
}
For this reason, the statements about Applet 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 running the applet after compilation, the image is not in one go. 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 that executes concurrently with the Applet's original execution thread. It is loaded and displayed at the same time, thus creating this discontinuity. In order to improve the display effect. Many Applets use image double buffering technology, that is, the image is completely loaded into the memory and then displayed on the screen, so that the image can be displayed in one go.
Double-buffering images
In order to improve the display effect of images, double-buffering technology should be used. The image is first loaded into memory and then displayed in the Applet window.
Example of using double buffering image technology (BackgroundImage.java)
//Source program list
import java.awt.*;
import java.applet.*;
public class BackgroundImage extends Applet //Inherit Applet
{
Image picture;
Boolean ImageLoaded=false;
public void init()
{
picture=getImage(getCodeBase(),"Image.gif"); //Load image Image offScreenImage=createImage(size().width,size().height);
//Create Image object Graphics using method createImage offScreenGC=offScreenImage.getGraphics(); //Get Graphics object offScreenGC.drawImage(picture,0,0,this); //Display off-screen image}
public void paint(Graphics g)
{
if(ImageLoaded)
{
g.drawImage(picture,0,0,null); //Display 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
return true;
}
}
Analysis of 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, and then creates a Graphics object named offScreenGC and assigns it a graphics environment - a non-screen image. will be produced by it. Because the off-screen image is drawn here, there will be no image displayed in the Applet window.
Whenever the Applet calls the drawImage method, drawImage will create a thread that calls the imageUpdate method. The applet can determine how much of the image has been loaded into memory in the imageUpdate method. The thread created by drawImage keeps calling the imageUpdate method until the method returns false.
The second parameter infoflags of the imageUpdate method enables the Applet to know how the image is loaded into memory. This parameter is equal to ImageLoaded and is set to true and the repaint method is called to redraw the Applet window. This method eventually returns false to prevent the drawImage execution thread from calling the imageUpdate method again.
The operation of the Applet in the paint method is controlled by the ImageLoaded variable. When this variable becomes true, the paint method calls the drawImage method to display the image. When the paint method calls the drawImage method, it takes null as the fourth parameter, which prevents drawImage from calling the imageUpdate method. Because the image has been loaded into the memory at this time, the display of the image in the Applet window can be completed in one go.