Graphics類提供基本繪圖方法,Graphics2D類提供更強大的繪圖能力。本節講解Graphics類,下節講解Graphics2D。
Graphics類提供基本的幾何圖形繪制方法,主要有:畫線段、畫矩形、畫圓、畫帶顏色的圖形、畫橢圓、畫圓弧、畫多邊形等。
1. 畫線
在窗口畫一條線段,可以使用Graphics類的drawLine()方法:
1
|
drawLine( int x1, int y1, int x2, int y2) |
例如,以下代碼在點(3,3)與點(50,50)之間畫線段,在點(100,100)處畫一個點。
1
2
|
g.drawLine( 3 , 3 , 50 , 50 ); //畫一條線段 g.drawLine( 100 , 100 , 100 , 100 ); //畫一個點。 |
2. 畫矩形
有兩種矩形:普通型和圓角型。
(1) 畫普通矩形有兩個方法:
drawRect(int x,int y,int width,int height):畫線框圍起來的矩形。其中參數x和y指定左上角的位置,參數width和height是矩形的寬和高。
fillRect(int x,int y,int width,int height):是用預定的顏色填充一個矩形,得到一個著色的矩形塊。
以下代碼是畫矩形的例子:
1
2
|
g.drawRect( 80 , 100 , 40 , 25 ); //畫線框 g.setColor(Color.yellow);g.fillRect( 20 , 70 , 20 , 30 ); //畫著色塊 |
(2)畫圓角矩形也有兩個方法:
drawRoundRect(int x,int y,int width, int height, int arcWidth, int arcHeight):是用線圍起來的圓角矩形。其中參數x和y指定矩形左上角的位置;參數width和heigth是矩形的寬和高;arcWidth和arcHeight分別是圓角弧的橫向直徑和圓角弧的縱向直徑。
fillRoundRect(int x,int y,int width,int height,int arcWidth,int archeight):是用預定的顏色填充的圓角矩形。各參數的意義同前一個方法。
以下代碼是畫矩形的例子:
1
2
3
4
|
g.drawRoundRect( 10 , 10 , 150 , 70 , 40 , 25 ); //畫一個圓角矩形 g.setColor(Color.blue); g.fillRoundRect( 80 , 100 , 100 , 100 , 60 , 40 ); //涂一個圓角矩形塊 g.drawRoundRect( 10 , 150 , 40 , 40 , 40 , 40 ); //畫圓 g.setColor(Color.red); g.fillRoundRect( 80 , 100 , 100 , 100 , 100 , 100 ); //畫圓塊 |
可以用畫圓角矩形方法畫圓形,當矩形的寬和高相等,圓角弧的橫向直徑和圓角弧的縱向直徑也相等,并等于矩形的寬和高時,畫的就是圓形。參見上述例子中的注釋,前一個是畫圓,后一個是涂圓塊。
3. 畫三維矩形
畫三維矩形有兩個方法:
draw3DRect(int x,int y,int width,int height, boolean raised):畫一個突出顯示的矩形。其中x和y指定矩形左上角的位置,參數width和height是矩形的寬和高,參數raised是突出與否。
fill3DRect(int x,int y,int width,int height,boolean raised):用預定的顏色填充一個突出顯示的矩形。
以下代碼是畫突出矩形的例子:
1
2
|
g.draw3DRect( 80 , 100 , 40 , 25 , true ); //畫一個線框 g.setColor(Color.yellow); g.fill3DRect( 20 , 70 , 20 , 30 , true ); //畫一個著色塊 |
4.畫橢圓形
橢圓形由橢圓的橫軸和縱軸確定。畫橢圓形有兩個方法:
drawOval(int x,int y,int width,int height):是畫用線圍成的橢圓形。其中參數x和參數y指定橢圓形左上角的位置,參數width和height是橫軸和縱軸。
fillOval(int x,int y,int width,int height):是用預定的顏色填充的橢圓形,是一個著色塊。也可以用畫橢圓形方法畫圓形,當橫軸和縱軸相等時,所畫的橢圓形即為圓形。
以下代碼是畫橢圓形的例子:
1
2
3
|
g.drawOval( 10 , 10 , 60 , 120 ); //畫橢圓 g.setColor(Color.cyan);g.fillOval( 100 , 30 , 60 , 60 ); //涂圓塊 g.setColor(Color.magenta);g.fillOval( 15 , 140 , 100 , 50 ); //涂橢圓 |
5. 畫圓弧
畫圓弧有兩個方法:
drawArc(int x,int y,int width,int height,int startAngle, int arcAngle):畫橢圓一部分的圓弧線。橢圓的中心是它的外接矩形的中心,其中參數是外接矩形的左上角坐標(x,y),寬是width,高是heigh。參數startAngle的單位是 “度”,起始角度0度是指3點鐘方位.參數startAngle和arcAngle表示從startAngle角度開始,逆時針方向畫arcAngle度的弧,約定,正值度數是逆時針方向,負值度數是順時針方向,例如-90度是6點鐘方位。
fillArc(int x,int y,int width, int height, int startAngle, int arcAngle):用setColor()方法設定的顏色,畫著色橢圓的一部分。
以下代碼是畫圓弧的例子:
1
2
3
4
|
g.drawArc( 10 , 40 , 90 , 50 , 0 , 180 ); //畫圓弧線 g.drawArc( 100 , 40 , 90 , 50 , 180 , 180 ); //畫圓弧線 g.setColor(Color.yellow); g.fillArc( 10 , 100 , 40 , 40 , 0 ,- 270 ); //填充缺右上角的四分之三的橢圓 g.setColor(Color.green); g.fillArc( 60 , 110 , 110 , 60 ,- 90 ,- 270 ); //填充缺左下角的四分之三的橢圓 |
6. 畫多邊形
多邊形是用多條線段首尾連接而成的封閉平面圖。多邊形線段端點的x坐標和y坐標分別存儲在兩個數組中,畫多邊形就是按給定的坐標點順序用直線段將它們連起來。以下是畫多邊形常用的兩個方法:
drawPolygon(int xpoints[],int yPoints[],int nPoints):畫一個多邊形
fillPolygon(int xPoints[],int yPoints[],int nPoints):用方法setColor()設定的顏色著色多邊形。其中數組xPoints[]存儲x坐標點,yPoints[]存儲y坐標點,nPoints是坐標點個數。
注意,上述方法并不自動閉合多邊形,要畫一個閉合的多邊形,給出的坐標點的最后一點必須與第一點相同.以下代碼實現填充一個三角形和畫一個八邊形。
1
2
3
4
5
6
7
8
|
int px1[]={ 50 , 90 , 10 , 50 }; //首末點相重,才能畫多邊形 int py1[]={ 10 , 50 , 50 , 10 }; int px2[]={ 140 , 180 , 170 , 180 , 140 , 100 , 110 , 140 }; int py2[]={ 5 , 25 , 35 , 45 , 65 , 35 , 25 , 5 }; g.setColor(Color.blue); g.fillPolygon(px1,py1, 4 ); g.setColor(Color.red); g.drawPolygon(px2,py2, 9 ); |
也可以用多邊形對象畫多邊形。用多邊形類Polygon創建一個多邊形對象,然后用這個對象繪制多邊形。Polygon類的主要方法:
- Polygon():創建多邊形對象,暫時沒有坐標點。
- Polygon(int xPoints[],int yPoints[],int nPoints):用指定的坐標點創建多邊形對象。
- addPoint():將一個坐標點加入到Polygon對象中。
- drawPolygon(Polygon p):繪制多邊形。
- fillPolygon(Polygon p):和指定的顏色填充多邊形。
例如,以下代碼,畫一個三角形和填充一個黃色的三角形。注意,用多邊形對象畫封閉多邊形不要求首末點重合。
1
2
3
4
5
6
7
8
9
10
|
int x[]={ 140 , 180 , 170 , 180 , 140 , 100 , 110 , 100 }; int y[]={ 5 , 25 , 35 , 45 , 65 , 45 , 35 , 25 }; Polygon ponlygon1= new Polygon(); polygon1.addPoint( 50 , 10 ); polygon1.addPoint( 90 , 50 ); polygon1.addPoint( 10 , 50 ); g.drawPolygon(polygon1); g.setColor(Color.yellow); Polygon polygon2 = new Polygon(x,y, 8 ); g.fillPolygon(polygon2); |
7. 擦除矩形塊
當需要在一個著色圖形的中間有一個空缺的矩形的情況,可用背景色填充一矩形塊實現,相當于在該矩形塊上使用了 “橡皮擦”.實現的方法是:
clearRect(int x,int y, int width,int height):擦除一個由參數指定的矩形塊的著色。
例如,以下代碼實現在一個圓中擦除一個矩形塊的著色:
1
2
|
g.setColor(Color.blue); g.fillOval( 50 , 50 , 100 , 100 );g.clearRect( 70 , 70 , 40 , 55 ); |
8. 限定作圖顯示區域
用一個矩形表示圖形的顯示區域,要求圖形在指定的范圍內有效,不重新計算新的坐標值,自動實現超出部分不顯示。方法是clipRect(int x,int y,int width,int height),限制圖形在指定區域內的顯示,超出部分不顯示。多個限制區有覆蓋時,得到限制區域的交集區域。例如,代碼:
1
|
g.clipRect( 0 , 0 , 100 , 50 );g.clipRect( 50 , 25 , 100 , 50 ); |
相當于
1
|
g.clipRect( 50 , 25 , 50 , 25 ); |
9. 復制圖形
利用Graphics類的方法copyArea()可以實現圖形的復制,其使用格式是:
copyArea(int x,int y,int width,int height, int dx, int dy),dx和dy分別表示將圖形粘貼到原位置偏移的像素點數,正值為往右或往下偏移是,負值為往左或往上偏移量。位移的參考點是要復制矩形的左上角坐標。
例如,以下代碼示意圖形的復制,將一個矩形的一部分、另一個矩形的全部分別自制。
1
2
3
4
|
g.drawRect( 10 , 10 , 60 , 90 ); g.fillRect( 90 , 10 , 60 , 90 ); g.copyArea( 40 , 50 , 60 , 70 ,- 20 , 80 ); g.copyArea( 110 , 50 , 60 , 60 , 10 , 80 ); |
【例】小應用程序重寫update()方法,只清除圓塊,不清除文字,窗口顯示一個不斷移動的紅色方塊。
import java.applet.*; import java.awt.*; public class Example7_3 extends Applet{ int i=1; public void init(){ setBackground(Color.yellow); } public void paint(Graphics g){ i = i+8; if(i>160)i=1; g.setColor(Color.red);g.fillRect(i,10,20,20); g.drawString("我正學習update()方法",100,100); try{ Thread.sleep(100); } catch(InterruptedException e){} repaint(); } public void update(Graphics g){ g.clearRect(i,10,200,100);//不清除"我正在學習update()方法" paint(g); } }
一般的繪圖程序要繼承JFrame,定義一個JFrame窗口子類,還要繼承JPanel,定義一個JPanel子類。在JPanel子類 中重定義方法paintComponent(),在這個方法中調用繪圖方法,繪制各種圖形。
【例】使用XOR繪圖模式的應用程序。
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
37
38
39
40
41
|
import javax.swing.*; import java.awt.*; public class Example7_4 extends JFrame{ public static void main(String args[]){ GraphicsDemo myGraphicsFrame = new GraphicsDemo(); } } class ShapesPanel extends JPanel{ SharpesPanel(){ setBackground(Color.white); } public void paintComponent(Graphics g){ super .paintComponent(g); setBackground(Color.yellow); //背景色為黃色 g.setXORMode(Color.red); //設置XOR繪圖模式,顏色為紅色 g.setColor(Color.green); g.fillRect( 20 , 20 , 80 , 40 ); //實際顏色是green + yellow的混合色=灰色 g.setColor(Color.yellow); g.fillRect( 60 , 20 , 80 , 40 ); //后一半是yellow+yellow=read,前一半是yellow+灰色 g.setColor(Color.green); g.fillRect( 20 , 70 , 80 , 40 ); //實際顏色是green+yellow的混合色=灰色. g.fillRect( 60 , 70 , 80 , 40 ); //前一半是(green+yellow)+gray =背景色,后一半是green+yellow = gray g.setColor(Color.green); g.drawLine( 80 , 100 , 180 , 200 ); //該直線是green+yellow = gray g.drawLine( 100 , 100 , 200 , 200 ); //同上 /*再繪制部分重疊的直線.原直線中間段是灰色+灰色=背景色,延長部分是green+yellow=gray.*/ g.drawLine( 140 , 140 , 220 , 220 ); g.setColor(Color.yellow); //分析下列直線顏色變化,與早先的力有重疊 g.drawLine( 20 , 30 , 160 , 30 ); g.drawLine( 20 , 75 , 160 , 75 ); } } class GraphicsDemod extends JFrame{ public GraphicsDemo(){ this .getContentPane().add( new ShapesPanel()); setTile( "基本繪圖方法演示" ); setSize( 300 , 300 ); setVisible( true ); } } |