Java applets display images stored in GIF files

Source: Internet
Author: User
Tags thread

Java applets are commonly used to display images stored in GIF files. Java applets Mount GIF images very simply, and you need to define an image object when you are using images files within an applet. Most Java applets use image files in GIF or JPEG format. The applet uses the GetImage method to connect the image files to the images object.

The DrawImage method of the graphics class is used to display the image object. In order to improve the image display, many applets use double buffering technology: first load the image into memory, and then display on the screen.

The applet can determine how much of an image has been installed in memory through the Imageupdate method.

Load a picture

Java also treats images as image objects, so you need to first define an image object when loading images, as shown in the following format:

Image picture;

Then use the GetImage method to connect the image object with the picture file:

Picture=getimage (GetCodeBase (), "imagefilename.gif");

The GetImage method has two parameters. The first parameter is a call to the GetCodeBase method that returns the URL address of the Applet, such as Www.sun.com/Applet. The second parameter specifies the image file name to mount from the URL. If the diagram file is in a subdirectory below the applet, the corresponding directory path should be included in the file name.

After loading the image with the GetImage method, the applet can display the image using the DrawImage method of the graphics class, in the form shown below:

G.drawimage (Picture,x,y,this);

The parameters of the Drayimage method indicate the image to be displayed, the x-coordinate and y-coordinate 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, the object that defines the Imageupdate method, which is discussed later.

Display Image (Showimage.java)

//源程序清单
import java.awt.*;
import java.applet.*;
public class ShowImage extends Applet
Image picure; //定义类型为Image的成员变量
public void init()
{
  picture=getImage(getCodeBase(),"Image.gif"); //装载图像
}
public void paint(Graphics g)
{
  g.drawImage(picture,0,0,this); //显示图像
}
}

To do this, the statements for the applet in the HTML file are as follows:

<HTML>
<TITLE>Show Image Applet</TITLE>
<APPLET
CODE="ShowImage.class" //class文件名为ShowImage.class
WIDTH=600
HEIGHT=400>
</APPLET>
</HTML>

When you run the applet after compiling, the image is not coherent. This is because the program is not a DrawImage method to load and display the image completely before returning it. In contrast, the DrawImage method creates a thread that is executed concurrently with the original execution thread of the applet, which is displayed on one side and thus produces this discontinuity. In order to improve the display effect. Many applets use image double buffering technology, that is, the image is fully loaded into memory and then displayed on the screen, which can make the image display coherent.

Related Article

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.