隨筆-60  評論-117  文章-0  trackbacks-0
          例一:

          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 靜兒 閱讀(1726) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 壤塘县| 隆化县| 左权县| 遂溪县| 阜城县| 延川县| 渝北区| 沙河市| 岳池县| 黄骅市| 石门县| 呼图壁县| 西宁市| 木兰县| 岑巩县| 石景山区| 曲沃县| 北宁市| 临江市| 思茅市| 洛南县| 台南县| 贺兰县| 南郑县| 新和县| 麟游县| 华宁县| 长乐市| 十堰市| 望都县| 千阳县| 庆云县| 会同县| 根河市| 清新县| 新沂市| 株洲县| 连南| 寿阳县| 闽侯县| 布拖县|