修飾子與參數
public abstract void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
public void drawPolygon(Polygon p)
drawPolygon() 沒有回傳值 (return value) ,有兩個參數 (paramenter) 版本,第一個需要兩個陣列物件 (object) 與一個整數型態 (type) 參數, xPoints 為圖形 x 座標的集合, yPoints 為圖形 y 座標的集合, nPoints 為點總數,第二個直接以多邊形 Polygon 物件當參數。
舉例如下
import java.awt.*;
import java.awt.event.*;
public class GraphicsDemo11 extends Canvas {
public static void main(String[] args) {
Frame frame = new Frame("AWTDemo");
frame.addWindowListener(new AdapterDemo());
frame.setSize(200, 220);
GraphicsDemo11 canvas = new GraphicsDemo11();
frame.add(canvas, BorderLayout.CENTER);
frame.setVisible(true);
}
public void paint(Graphics g) {
int x[] = {20, 56, 100, 150, 120, 87, 30};
int y[] = {25, 40, 80, 110, 140, 90, 60};
g.drawPolygon(x, y, 7);
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:GraphicsDemo11.java
功能:示範 Java 程式
作者:張凱慶
時間:西元 2012 年 3 月 */此例在 paint() 方法中畫圖,先建立兩個座標陣列,然後呼叫 Graphics 的 drawPolygon() 畫出多邊形
public void paint(Graphics g) {
int x[] = {20, 56, 100, 150, 120, 87, 30};
int y[] = {25, 40, 80, 110, 140, 90, 60};
g.drawPolygon(x, y, 7);
}編譯後執行,結果如下

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