一、处理图形
1.画直线
void drawLine (int startx , int starty , int endx , int endy)
参数列表:直线开始的横坐标、纵坐标,直线结束的横坐标、纵坐标。
2.画矩形
1) 矩形边框:void drawRect(int top , int left , int width , int height )
2) 实心矩形 :void fillRect(int top , int left , int width , int height)
参数列表:矩形的左上角坐标(x,y),宽度和高度。
3.圆角矩形
1) 圆角矩形边框 : void drawRoundRect(int top , int left , int width , int height , int xDiam , int yDiam)
2 )实心圆角矩形void fillRoundRect(int top , int left , int width , int height , int xDiam , int yDiam)
参数列表 :圆角矩形的左上角坐标,宽度,高度,X轴上的弧度,Y轴上的弧度
4.画椭圆和圆形
1)空心圆:void drawOval(int top , int left , int width , int height)
2)实心圆:void fillOval(int top , int left , int width , int height)
参数列表 :左上角坐标,宽,高
5.画圆弧
1)空心圆弧:void drawArc(int top , int left , int width , int height , int startAngle , int sweepAngle);
2)实心圆弧: void fillArc(int top , int left , int width , int height , int startAngle , int sweepAngle);
参数列表 :在左上角坐标为(top , left)宽为width 高为 height的矩形中画圆弧 ,startAngle是与3点钟的角度 ,sweepAngel是与startAngle的角度。
6.画多边形
1)空心多边形:void drawP
2)实心多边形:
1 importjava.awt.Frame;2 importjava.awt.Panel;3 importjava.awt.Graphics;4 importjava.awt.Color;5 public classTest04{6
7 public static voidmain(String[] args){8
9 Frame f = newFrame();10 f.setSize(1000,1000);11 MyPanel mp = newMyPanel();12 f.add(mp);13 f.show();14 }15 }16 class MyPanel extendsPanel{17
18 public voidpaint(Graphics g){19
20 g.drawLine(100,50,100,100);21 g.drawString("Hello World !",120,100);22 g.setColor(Color.green);23 g.drawRect(100,100,200,100);24 g.fillRect(350,100,200,100);25 g.drawRoundRect(600,100,200,100,50,50);26 g.fillRoundRect(850,100,200,100,20,100);27 g.drawOval(100,250,100,100);28 g.fillOval(250,250,80,100);29 g.drawArc(350,250,100,100,0,90);30 g.fillArc(450,250,100,100,0,90);31 g.fillArc(550,250,100,100,10,80);32
33 }34 }
二、Graphics类的应用
1、绘制五角星
1 importjava.awt.Frame;2 importjava.awt.Color;3 importjava.awt.Graphics;4 importjava.awt.Panel;5 import java.math.*;6 public classTest027 {8 public static voidmain(String[] args)9 {10 Frame f = newFrame();11 f.setSize(1000,1000);12 f.setBackground(Color.black);13 MyPanel mp = newMyPanel();14 f.add(mp);15 f.show();16 }17 }18 class MyPanel extendsPanel19 {20 public voidpaint(Graphics g)21 {22 double xA= 2 , yA= 2 , c = 2,j36,j18,j54;23 doublexB ,yB ,xC,yC,xD ,yD,xE ,yE,xf,yf,xg,yg;24 j36 = Math.toRadians(36);//转化成角度
25 j18 = Math.toRadians(18);26 j54 = Math.toRadians(54);27 xB = xA+c*Math.cos(j36);28 yB = yA-c*Math.sin(j36);29 xC = xA+2*c*Math.cos(j36);30 yC =yA;31 xD = xA+c*Math.sin(j18);32 //System.out.println("xD ="+xD+" c*Math.sin(18)"+c*Math.sin(18) );
33 yD = yA+c*Math.cos(j18);34 xE = xC - c*Math.sin(j18);35 yE =yD;36 xf = xD+c/2;37 yf = yD-(c/2)*Math.tan(j36);38 xg = xE - c/(2*Math.sin(j54))*Math.sin(j18);39 yg = yE - c/(2*Math.sin(j54))*Math.cos(j18);40 //int xpoints[] = {(int)(xB*100),(int)(xD*100),(int)(xC*100),(int)(xA*100),(int)(xE*100),(int)(xB*100)};41 //int ypoints[] = {(int)(yB*100),(int)(yD*100),(int)(yC*100),(int)(yA*100),(int)(yE*100),(int)(yB*100)};
42 int xpoints[] = {(int)(xB*100),(int)(xD*100),(int)(xE*100),(int)(xB*100)};43 int ypoints[] = {(int)(yB*100),(int)(yD*100),(int)(yE*100),(int)(yB*100)};44 int num = 4;45 //System.out.println((int)(xA*100)+" "+(int)(yA*100)+" , "+(int)(xD*100)+" "+(int)(yD*100));
46 g.setColor(Color.white);47 g.fillPolygon(xpoints,ypoints,num);48 int xpoints1[] = {(int)(xA*100), (int)(xC*100),(int)(xE*100),(int)(xA*100)};49 int ypoints1[] = {(int)(yA*100),(int)(yC*100),(int)(yE*100),(int)(yA*100)};50 g.fillPolygon(xpoints1,ypoints1,num);51 g.setColor(Color.black);52 int xpoints2[] = {(int)(xD*100),(int)(xf*100),(int)(xE*100),(int)(xD*100)};53 int ypoints2[] = {(int)(yD*100),(int)(yf*100),(int)(yE*100),(int)(yD*100)};54 g.fillPolygon(xpoints2,ypoints2,4);//减去多余的部分
55 int xpoint3[] = {(int)(xE*100),(int)(xg*100),(int)(xC*100),(int)(xE*100)};56 int ypoint3[] = {(int)(yE*100),(int)(yg*100),(int)(yC*100),(int)(yE*100)};57 g.fillPolygon(xpoint3,ypoint3,4);//减去多余的部分
58
59 }60 }
五角星的坐标图 运行结果图
2.随机输出星号
1 importjava.awt.Frame;2 importjava.awt.Panel;3 importjava.awt.Graphics;4 public classTest035 {6 public static voidmain(String[] args)7 {8 Frame f = newFrame();9 f.setSize(500,500);10 MyPanel mp = newMyPanel();11 f.add(mp);12 f.show();13
14 }15 }16 class MyPanel extendsPanel17 {18 public voidpaint(Graphics g)19 {20 for(int i=0 ;i<50;i++)21 {22 int x = (int)(Math.random()*500);//生成0-500的随机数
23 int y = (int)(Math.random()*500);24 g.drawString("*",x,y);25 }26 }27 }