Presentation is loading. Please wait.

Presentation is loading. Please wait.

如何建立一個 swing 元件 -- 以 ImageComponent 為例 井民全. Step 1: 繼承 javax.swing.JComponent 要將你的元件秀在 frame 中. 元件必須是 Jcomponent 的一種. javax.swing.JComponent 你的元件 class.

Similar presentations


Presentation on theme: "如何建立一個 swing 元件 -- 以 ImageComponent 為例 井民全. Step 1: 繼承 javax.swing.JComponent 要將你的元件秀在 frame 中. 元件必須是 Jcomponent 的一種. javax.swing.JComponent 你的元件 class."— Presentation transcript:

1 如何建立一個 swing 元件 -- 以 ImageComponent 為例 井民全

2 Step 1: 繼承 javax.swing.JComponent 要將你的元件秀在 frame 中. 元件必須是 Jcomponent 的一種. javax.swing.JComponent 你的元件 class ImageComponent extends JComponent { // … } 繼承 UML 表示圖

3 Step 2: 加入兩個必要的 methods 為了要讓 layout 管理器得知你物件的大小 public Dimension getPreferredSize() 顯示你的物件外觀 public void paint(Graphics g) javax.swing.JComponent 你的元件 getPreferedSize() Paint(Graphics g) 類別圖

4 實作 methods class ImageComponent extends JComponent { public Dimension getPreferredSize(){ return new Dimension(Width,Height) } java.awt.Dimension public void paint(Graphics g) { // 畫出 Image ( 現在先以簡單的圖代替 ) g.drawRect(10,10,100,100); }

5 Step 3: 如何把元件加入容器 建立 JFrame File -> new -> JFrame 取出容器 getContentPane() 把元件加到容器中 getContentPane().add( 你的元件 ) javax.swing.JFrame MyFrame 類別圖

6 MyFrame 實作 class MyFrame extends javax.swing.JFrame { public void Assign(ImageComponent Image) { this.getContentPane().add(Image); pack(); this.show(); } 把元件加入容器中 依據元件的大小自動設定 Frame 的 size 注意 : 如果你不加上 this.show() 圖形將不會秀出

7 測試你的元件 void main(String args[]) { MyFrame frame1 = new MyFrame (); ImageComponent image=new ImageComponent(); MyFrame.Assign(image); } 程式範例 : Step1 MyFrame Image depend ImageComponent getPreferedSize() Paint(Graphics g)

8 測試你的元件 – 加入一堆物件 ( 新增 ) ImageComponent1 ImageComponent2 MyFrame 你需要一個物件幫你管理物件如何排列 !! 如何設定管理物件 ? VerticalFlowLayout Layout=new VerticalFlowLayout() this->getContentPane().setLayout( Layout );

9 測試你的元件 – 加入一堆物件 ( 新增 ) class MyFrame extends javax.swing.JFrame { VerticalFlowLayout Layout=new VerticalFlowLayout(); public MyFrame() { this->getContentPane().setLayout( Layout ); } public void Assign(ImageComponent Image) { this.getContentPane().add(Image); pack(); this.show(); }

10 測試你的元件 – 加入一堆物件 ( 新增 ) void main(String args[]) { MyFrame frame1 = new MyFrame (); ImageComponent image=new ImageComponent(); MyFrame.Assign(image); ImageComponent image2=new ImageComponent(); MyFrame.Assign(image2); }

11 利用 interface -- 處理必要的 methods 進階的內容

12 好了, 如何加入 Image 從檔案讀取 Image 利用 Toolkit 取得 image 物件 建立 MediaTracker 等待 image 下載 當 image 準備好後,  利用 PixelGrabber 把資料讀進來 java.awt.image image; image=Toolkit.getDefaultToolkit().getImage(filename); java.awt.Toolkit

13 建立 MediaTracker -- 等待 image 下載 // 等待 Image 載入 MediaTracker mt=new MediaTracker(this); mt.addImage(image,0); try{ mt.waitForAll(); } catch(InterruptedException e) { throw new Exception(" 載入圖形錯誤 "); } java.awt.MediaTracker 圖形 id

14 利用 PixelGrabber 把資料讀進來 // 取得 image 的圖素 Height=image.getHeight(null); width=image.getWidth(null); int [] pixels=new int[Height*Width]; PixelGrabber pg=new PixelGrabber(image,0,0,Width,Height,pixels,0,Width); try{ pg.grabPixels(); }catch(InterruptedException e) {} java.awt.image.*; 指定讀取方框

15 ARGB Color Model Alpha Red Green Blue 3124231615870 Int type 32 bits Red Value = 0xf & (p >>16); Green Value = 0xff & (p>>8); Blue Value = 0xff & p

16 整合在一起 See Project “ Step2 ” : LoadFromFile( … ) method


Download ppt "如何建立一個 swing 元件 -- 以 ImageComponent 為例 井民全. Step 1: 繼承 javax.swing.JComponent 要將你的元件秀在 frame 中. 元件必須是 Jcomponent 的一種. javax.swing.JComponent 你的元件 class."

Similar presentations


Ads by Google