一、处理图形
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)实心多边形:
import java.awt.Frame;
import java.awt.Panel;
import java.awt.Graphics;
import java.awt.Color;
public class Test04{ public static void main(String[] args){ Frame f = new Frame();
f.setSize(1000,1000);
MyPanel mp = new MyPanel();
f.add(mp);
f.show();
}
}
class MyPanel extends Panel{ public void paint(Graphics g){ g.drawLine(100,50,100,100);
g.drawString("Hello World !",120,100);
g.setColor(Color.green);
g.drawRect(100,100,200,100);
g.fillRect(350,100,200,100);
g.drawRoundRect(600,100,200,100,50,50);
g.fillRoundRect(850,100,200,100,20,100);
g.drawOval(100,250,100,100);
g.fillOval(250,250,80,100);
g.drawArc(350,250,100,100,0,90);
g.fillArc(450,250,100,100,0,90);
g.fillArc(550,250,100,100,10,80); }
}
二、Graphics类的应用
1、绘制五角星
import java.awt.Frame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Panel;
import java.math.*;
public class Test02
{
public static void main(String[] args)
{
Frame f = new Frame();
f.setSize(1000,1000);
f.setBackground(Color.black);
MyPanel mp = new MyPanel();
f.add(mp);
f.show();
}
}
class MyPanel extends Panel
{
public void paint(Graphics g)
{
double xA= 2 , yA= 2 , c = 2,j36,j18,j54;
double xB ,yB ,xC,yC,xD ,yD,xE ,yE,xf,yf,xg,yg;
j36 = Math.toRadians(36);//转化成角度
j18 = Math.toRadians(18);
j54 = Math.toRadians(54);
xB = xA+c*Math.cos(j36);
yB = yA-c*Math.sin(j36);
xC = xA+2*c*Math.cos(j36);
yC = yA;
xD = xA+c*Math.sin(j18);
//System.out.println("xD ="+xD+" c*Math.sin(18)"+c*Math.sin(18) );
yD = yA+c*Math.cos(j18);
xE = xC - c*Math.sin(j18);
yE = yD;
xf = xD+c/2;
yf = yD-(c/2)*Math.tan(j36);
xg = xE - c/(2*Math.sin(j54))*Math.sin(j18);
yg = yE - c/(2*Math.sin(j54))*Math.cos(j18);
//int xpoints[] = {(int)(xB*100),(int)(xD*100),(int)(xC*100),(int)(xA*100),(int)(xE*100),(int)(xB*100)};
//int ypoints[] = {(int)(yB*100),(int)(yD*100),(int)(yC*100),(int)(yA*100),(int)(yE*100),(int)(yB*100)};
int xpoints[] = {(int)(xB*100),(int)(xD*100),(int)(xE*100),(int)(xB*100)};
int ypoints[] = {(int)(yB*100),(int)(yD*100),(int)(yE*100),(int)(yB*100)};
int num = 4 ;
//System.out.println((int)(xA*100)+" "+(int)(yA*100)+" , "+(int)(xD*100)+" "+(int)(yD*100));
g.setColor(Color.white);
g.fillPolygon(xpoints,ypoints,num);
int xpoints1[] = {(int)(xA*100), (int)(xC*100),(int)(xE*100),(int)(xA*100)};
int ypoints1[] = {(int)(yA*100),(int)(yC*100),(int)(yE*100),(int)(yA*100)};
g.fillPolygon(xpoints1,ypoints1,num);
g.setColor(Color.black);
int xpoints2[] = {(int)(xD*100),(int)(xf*100),(int)(xE*100),(int)(xD*100)};
int ypoints2[] = {(int)(yD*100),(int)(yf*100),(int)(yE*100),(int)(yD*100)};
g.fillPolygon(xpoints2,ypoints2,4);//减去多余的部分
int xpoint3[] = {(int)(xE*100),(int)(xg*100),(int)(xC*100),(int)(xE*100)};
int ypoint3[] = {(int)(yE*100),(int)(yg*100),(int)(yC*100),(int)(yE*100)};
g.fillPolygon(xpoint3,ypoint3,4);//减去多余的部分 }
}
五角星的坐标图 运行结果图
2.随机输出星号
import java.awt.Frame;
import java.awt.Panel;
import java.awt.Graphics;
public class Test03
{
public static void main(String[] args)
{
Frame f = new Frame();
f.setSize(500,500);
MyPanel mp = new MyPanel();
f.add(mp);
f.show(); }
}
class MyPanel extends Panel
{
public void paint(Graphics g)
{
for(int i=0 ;i<50;i++)
{
int x = (int)(Math.random()*500);//生成0-500的随机数
int y = (int)(Math.random()*500);
g.drawString("*",x,y);
}
}
}