例一:
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將自動調(diào)用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;
}
//容器大小設(shè)定。
int imageWidth = icon.getIconWidth();
int imageHeight = icon.getIconHeight();
resize(imageWidth, imageHeight);
//調(diào)用將圖像加到面板的類
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
靜兒 閱讀(1725)
評論(0) 編輯 收藏