Java API 分類導覽 - java.awt.Graphics2D fill()

Graphics2D 類別 (class) 的 fill() 方法 (method) 用來畫出指定形狀並且填滿顏色的圖形。



修飾子與參數
public abstract void fill(Shape s)


fill() 沒有回傳值 (return value) ,需要一個 Shape 型態 (type) 的物件 (object) 當參數 (paramenter) 。


舉例如下
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
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
 
public class Graphics2DDemo06 extends Canvas {
    public static void main(String[] args) {
        Frame frame = new Frame("AWTDemo");
        frame.addWindowListener(new AdapterDemo());
        frame.setSize(200, 200);
         
        Graphics2DDemo06 canvas = new Graphics2DDemo06();
        frame.add(canvas, BorderLayout.CENTER);
         
        frame.setVisible(true);
    }
     
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.fill(new Ellipse2D.Double(10, 10, 180, 160));
    }   
}
 
class AdapterDemo extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}
 
/* 《程式語言教學誌》的範例程式
    檔名:Graphics2DDemo06.java
    功能:示範 Java 程式
    作者:張凱慶
    時間:西元 2012 年 3 月 */


此例在 paint() 方法中畫圖,先將 Graphics 型態的參數 g 強制轉換成 Graphics2D 型態,然後呼叫 fill() 畫出圓形
17
18
19
20
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.fill(new Ellipse2D.Double(10, 10, 180, 160));
}


編譯後執行,結果如下



中英文術語對照
類別class
方法method
回傳值return value
型態type
物件object
參數parameter


您可以繼續參考
繪圖


相關目錄
Java API 分類導覽
Java 教材
首頁


參考資料
http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics2D.html

沒有留言: