例一:
package map1;
import java.awt.Graphics;
import java.awt.Image;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JPanel;
public class T4 extends JApplet {
private static final long serialVersionUID = 1L;
//程序启动时,applet将自动调用init()方法。
public void init() {
ImageIcon icon = null;
//首先取得图形对象。
try {
icon = new ImageIcon(new URL(getCodeBase(), "Images/6.gif"));//字符串为相对路径。
} catch (MalformedURLException e) {
System.out.println("Failed to create to URL." + e);
return;
}
//容器大小设定。
int imageWidth = icon.getIconWidth();
int imageHeight = icon.getIconHeight();
resize(imageWidth, imageHeight);
//调用将图像加到面板的类
ImagePanel imagePanel = new ImagePanel(icon.getImage());
//将图像添加到界面上。
getContentPane().add(imagePanel);
}
//将图像加到面板的类
class ImagePanel extends JPanel {
private static final long serialVersionUID = 1L;
private Image image;
//获得图像。
public ImagePanel(Image image) {
this.image = image;
}
//图像绘制。
public void paint(Graphics g) {
g.drawImage(this.image, 0, 0, this);
}
}
}
posted on 2007-10-18 14:48
静儿 阅读(1720)
评论(0) 编辑 收藏