簡單圖形繪製

簡單圖形繪製

Graphics是java.awt提供的一個繪畫類。構造方法只有一個Graphics():

Graphics 是一個抽象類,所以應用程序不能直接調用此構造方法。圖形上下文從其他圖形上下文獲取,或者通過在組件上調用getGraphics 來創建。

public void paint(Graphics g){...}

1.繪製直線

g.drawLine(x1, y1, x2, y2);   

x1, y1爲起點, x2, y2爲終點

2.繪製矩形

g.drawRect(x, y, width, height);

x, y爲起點,width寬,height

g.fillRect(x, y, width, height);

x, y爲起點,width寬,height,並填充矩形。

g.drawRoundRect(x, y, width, height, arcWidth, arcHeight);

x, y爲起點,width寬,height,圖形當前顏色繪製圓角矩形的邊框

g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);

x, y爲起點,width寬,height,圖形當前顏色繪製圓角矩形的邊框和當前顏色填充圓角矩形。

g.draw3DRect(x, y, width, height, raised);

g.fill3DRect(x, y, width, height, raised);

 繪製一個用當前顏色填充的 3-D 高亮顯示矩形。true表示上凸,false表示下凹。

3.繪製圓形和橢圓

 繪製橢圓的邊框,當width等於 height的時候爲圓。

g.drawOval(x, y, width, height);

x, y爲圓心,width爲水平方向的寬,height爲y軸方向的高

g.fillOval(x, y, width, height));

x, y爲圓心,width爲水平方向的寬,height爲y軸方向的高,並用當前顏色填充橢圓。

4.繪製圓弧和扇形

width等於 height的時候爲圓

g.drawArc(x, y, width, height, startAngle, arcAngle);

x, y爲圓心,width爲水平方向的寬,height爲y軸方向的高。起始角度和結束角度。

g.fillArc(x, y, width, height, startAngle, arcAngle);

同上,並填充當前顏色。

5.繪製多邊形

g.drawPolygon(xPoints, yPoints, nPoints);

<span style="font-size:14px;">int[] xpoint={50,60,90,120,150};
int[] ypoint={130,40,95,120,50};
g.drawPolyline(xpoint, ypoint, 5);</span>

nPoints表示幾個點。定義的是一系列連接線

g.drawPolygon(xPoints, yPoints, nPoints);

定義的閉合多邊形。

g.drawPolygon(p);

繪製由指定的 Polygon 對象定義的多邊形邊框

6.設置當前顏色

<span style="font-size:14px;">Color c=g.getColor();//獲取系統顏色
g.setColor(c.cyan);//設置當前顏色
</span>

7.圖形文字顯示

img=getImage(URL url,String name);//加載image對象

img=getImage(URL url);//加載image對象

g.drawImage(img, x, y, observer);//顯示圖片,不修改大小

具體看幫助文檔


import java.applet.Applet;
import java.awt.*;

public class DrawImg extends Applet{
	int flag=0;
	Panel p=new Panel();
	Button line=new Button("直線");
	Button oval=new Button("畫圓");
	Button rect=new Button("矩形");
	Button polygon=new Button("多邊形");
	Button arc=new Button("扇形");
	int[] xpoint={50,60,90,120,150};
	int[] ypoint={130,40,95,120,50};
	
	public void init(){
		p.add(line);p.add(oval);p.add(rect);p.add(polygon);p.add(arc);
		add(p);
		p.setSize(400, 300);
		p.setVisible(true);
	}
	public boolean action(Event e,Object o){
		if(e.target==line) flag=1;
		if(e.target==oval) flag=2;
		if(e.target==rect) flag=3;
		if(e.target==polygon) flag=4;
		if(e.target==arc) flag=5;
		repaint();
		return true;
	}
	public void paint(Graphics g){
		Color c=g.getColor();
		switch(flag){
		case 1:
			g.drawLine(50, 50, 100, 100);
			break;
		case 2:
			g.setColor(c.blue);
			g.fillOval(50, 50, 50, 30);
			g.drawOval(50, 50, 60, 40);
			break;
		case 3:
			g.drawRect(20, 80, 40, 30);
			g.fillRect(80, 80, 40, 60);
			g.drawRoundRect(150, 80, 80, 60, 10, 20);
			g.fillRoundRect(170, 90, 40, 40, 10, 20);
			g.setColor(c.cyan);
			break;
		case 4:
			g.drawPolyline(xpoint, ypoint, 5);
			break;
		case 5:
			g.drawArc(30, 50, 100, 120, 0, 45);
			g.fillArc(120, 80, 150, 100, 45, 90);
			break;
		}
	}
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章