修飾子與參數
public abstract Graphics create()
public Graphics create(int x, int y, int width, int height)
create() 的回傳值 (return value) 為 Graphics 型態 (type) 的物件 (object) ,有兩個參數 (paramenter) 版本,第一個不需要參數,第二個需要四個整數型態的參數, x 為 Graphics 的起始 x 座標, y 為 y 座標, width 為寬, height 為高。
舉例如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import java.awt.*; import java.awt.event.*; public class GraphicsDemo29 extends Canvas { public static void main(String[] args) { Frame frame = new Frame( "AWTDemo" ); frame.addWindowListener( new AdapterDemo()); frame.setSize( 200 , 220 ); GraphicsDemo29 canvas = new GraphicsDemo29(); frame.add(canvas, BorderLayout.CENTER); frame.setVisible( true ); } public void paint(Graphics g) { g.setColor(Color.RED); g.fillRect( 10 , 10 , 100 , 100 ); Graphics ng = g.create( 30 , 30 , 100 , 100 ); ng.setColor(Color.GREEN); ng.fillRect( 30 , 30 , 100 , 100 ); } } class AdapterDemo extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit( 0 ); } } /* 《程式語言教學誌》的範例程式 檔名:GraphicsDemo29.java 功能:示範 Java 程式 作者:張凱慶 時間:西元 2012 年 3 月 */ |
此例在 paint() 方法中畫圖,先呼叫 Graphics 的 setColor() 設定繪圖顏色為紅色,然後呼叫 fillRect() 畫出一個紅色正方形,接著呼叫 create() 建立一個新的 Graphics 物件 ng ,最後 ng 設定繪圖顏色為綠色,並在 ng 中畫另一個綠色正方形
16 17 18 19 20 21 22 | public void paint(Graphics g) { g.setColor(Color.RED); g.fillRect( 10 , 10 , 100 , 100 ); Graphics ng = g.create( 30 , 30 , 100 , 100 ); ng.setColor(Color.GREEN); ng.fillRect( 30 , 30 , 100 , 100 ); } |
編譯後執行,結果如下

中英文術語對照 | |
---|---|
類別 | class |
方法 | method |
回傳值 | return value |
型態 | type |
物件 | object |
參數 | parameter |
您可以繼續參考
繪圖
相關目錄
Java API 分類導覽
Java 教材
首頁
參考資料
http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics.html
沒有留言:
張貼留言