background image

import java.awt.image.*;
import java.awt.image.ImageObserver;
import java.net.URL;

//使用 ImageObserver 接口

 

public

 

class

 MyApplet 

extends

 Applet 

implements

 ImageObserver{

Image img;
String imgPath = "";
int xpoint = 100, ypoint = 100;

public

 void init() {

setBackground(Color.white); 

//设置背景色

 
setForeground(Color.blue); 

//设置前景色

 
imgPath = "img/" + "test.gif"; 

//设置图象文件路径

 
}

public

 void paint(Graphics g) {

URL imgURL = getDocumentBase();
img = getImage(imgURL, imgPath);
g.drawImage(img,xpoint,ypoint,this);
}
}