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

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


          網站導航:
           
          主站蜘蛛池模板: 胶州市| 师宗县| 铁力市| 西宁市| 拉孜县| 武功县| 沾益县| 剑阁县| 金川县| 靖宇县| 吐鲁番市| 岱山县| 新竹市| 冕宁县| 大化| 象州县| 磴口县| 西畴县| 集安市| 城步| 辽源市| 西安市| 麟游县| 苏尼特左旗| 墨脱县| 鸡西市| 伊春市| 通许县| 随州市| 湘潭县| 翁源县| 潼南县| 兴宁市| 尤溪县| 崇仁县| 丰原市| 韶山市| 当涂县| 太保市| 寿光市| 阿勒泰市|