How to display images in the Applet
**************************************** *********************
****
** Author: one small step of one day's improvement by Liu Yu **
** Date: 2000-12-19 ==================== **
** Jeru@163.net enriches my every day **
****
**************************************** *********************
I. To display an image in an Applet, You need to introduce the following package and call the ImageObserver interface .:
Import java. awt. image .*;
Import java. awt. image. ImageObserver;
Import java.net. URL;
Public class MyApplet extends Applet implements ImageObserver {...
II. Define image path
URL imgURL = getDocumentBase ();
Set the path to the same as your html file.
URL imgUrl = getClassBase ();
Set the path to the same as your html file.
3. Get Image
Img = getImage (imgURL, imgName );
Of course, if you want to put your image files in a directory, you can do this.
Img = getImage (imgURL, "img" + imgName );
4. Show Image
The most basic method is to call drawImage (). This method is defined in the Graphics class. The simplest method is as follows:
G. drawImage (image, xPosition, yPosition, this );
The last parameter calls ImageObserver.
5. Final supplement
The above is the most basic. Of course, sometimes you even want to compress your image file into a jar package.
The getResourceAsStream () method is used.