隨筆-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)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 恩施市| 襄垣县| 华容县| 工布江达县| 东至县| 麻栗坡县| 会昌县| 苏州市| 南岸区| 若羌县| 阿拉善右旗| 平果县| 天水市| 衡水市| 丹东市| 扶余县| 铁岭县| 宁晋县| 天祝| 连山| 酒泉市| 遂宁市| 三明市| 淮安市| 保定市| 海南省| 宁波市| 靖州| 黄冈市| 股票| 汉川市| 玉树县| 宣恩县| 华坪县| 吉木乃县| 利川市| 西充县| 嘉黎县| 格尔木市| 葫芦岛市| 庆安县|