1 / 16

如何建立一個 swing 元件 -- 以 ImageComponent 為例

如何建立一個 swing 元件 -- 以 ImageComponent 為例. 井民全. Step 1: 繼承 javax.swing.JComponent. javax.swing.JComponent. 要將你的元件秀在 frame 中 . 元件必須是 Jcomponent 的一種. 你的元件. 繼承 UML 表示圖. class ImageComponent extends JComponent { // … }. Step 2: 加入兩個必要的 methods. 為了要讓 layout 管理器得知你 物件的大小

nyoko
Télécharger la présentation

如何建立一個 swing 元件 -- 以 ImageComponent 為例

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

  5. Step 3:如何把元件加入容器 javax.swing.JFrame • 建立 JFrame • File -> new -> JFrame • 取出容器 getContentPane() • 把元件加到容器中 getContentPane().add(你的元件) 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. 測試你的元件 MyFrame Image ImageComponent getPreferedSize() Paint(Graphics g) depend void main(String args[]) { MyFrame frame1 = new MyFrame (); ImageComponent image=new ImageComponent(); MyFrame.Assign(image); } 程式範例: Step1

  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 java.awt.Toolkit • 從檔案讀取 Image • 利用 Toolkit取得 image 物件 • 建立 MediaTracker等待 image 下載 • 當image準備好後, • 利用 PixelGrabber把資料讀進來 java.awt.image image; image=Toolkit.getDefaultToolkit().getImage(filename);

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

  14. 利用 PixelGrabber把資料讀進來 java.awt.image.*; // 取得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) {} 指定讀取方框

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

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

More Related