Java绘图类
1.Graphics类
提供了绘图常用的方法,利用这些方法可以实现直线,矩形,多边形等文本,图片的绘制其操作主要包括颜色,字体,画笔,文本,图像等
2.Graphics2D类
使用Graphics2D类可以实现更强的图像绘制功能
3.将Graphics强制转换成Graphics2D类:
public static void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
}
绘制图形
Graphics 类常用图形绘制方法
方法 | 说明 |
---|---|
draw(int x,int y,int height,int startAngle,int arcAngle) | 弧形 |
drawLine(int x1,int y1,int x2,int y2) | 直线 |
drawOval(int x,int y,int width,int height) | 椭圆 |
drawPolygon(int[] xPoints,int[] yPoints,int nPoints) | 多边形 |
drawPolyline(int[] xPoints,int[] yPoints,int nPoints) | 多边线 |
drawRect(int x,int y,int width,int height) | 矩形 |
drawRoundRect(int x,int y,int width,int height,int arcWidth,int arcAngle) | 圆角矩形 |
fillArc(int x,int y,int width,int height,int startAngle,int arcAngle) | 实心弧形 |
fillOval(int x,int y,int width,int height) | 实心椭圆 |
fillPolygon(int[] xPoints,int[] yPoints,int nPoints) | 实心多边形 |
fillRect(int x,int y,int width,int height) | 实心矩形 |
fillRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight) | 实心圆角矩形 |
当我们要绘制图形时,我们必须要进行创建并初始化图形类对象。这些图形必须时Shape接口的实现类,然后使用Graphics2D类的draw方法进行绘制,或者fill方法进行填充。
语法:
draw(Shape form)
fill(Shape form)
常用图形类:
主函数:
public static void main(String[] args) {
new G_1().setVisible(true);
}
- Arc2D类:弧
Graphics2D g2d =(Graphics2D)g;
Shape[] shapes = new Shape[3];
shapes[0] = new Arc2D.Double(50,50,185,160,10,90,Arc2D.OPEN);//坐标位置(50,50),宽85,高60,起始角是5度,终止角是90度
shapes[1] = new Arc2D.Double(150,150,185,160,10,90,Arc2D.CHORD);
shapes[2] = new Arc2D.Double(200,300,185,160,10,90,Arc2D.PIE);
for (Shape shape:shapes){
//遍历图形数组
g2d.draw(shape);//绘制图形
}
//Arc2D.OPEN、Arc2D.CHORD、Arc2D.PIE分别表示圆弧是开弧、弓弧和饼弧。
- CubicCurve2D类:立方曲线
class drawplace extends JPanel{
public void paint(Graphics g){
Graphics2D g2d =(Graphics2D)g;
Shape[] shapes = new Shape[1];
shapes[0] =new CubicCurve2D.Double(100,100,50,75,15,15,260,300);
//参数分别表示坐标起点,控制点1,控制点2,终点的坐标
for (Shape shape:shapes){
//遍历图形数组
g2d.draw(shape);//绘制图形
}
}
}
- Ellipse2D类:椭圆
class drawplace extends JPanel{
public void paint(Graphics g){
Graphics2D g2d =(Graphics2D)g;
Shape[] shapes = new Shape[1];
shapes[0] = new Ellipse2D.Double(150,150,200,100);
//参数表示起点,宽,高
for (Shape shape:shapes){
//遍历图形数组
g2d.draw(shape);//绘制图形
}
}
}
class drawplace extends JPanel{
public void paint(Graphics g){
Graphics2D g2d =(Graphics2D)g;
Shape[] shapes = new Shape[1];
shapes[0] = new Ellipse2D.Double(150,150,200,100);
//参数表示起点,宽,高
for (Shape shape:shapes){
//遍历图形数组
g2d.draw(shape);//绘制图形
}
}
}
- Line2D类:线
class drawplace extends JPanel{
public void paint(Graphics g){
Graphics2D g2d =(Graphics2D)g;
Shape[] shapes = new Shape[1];
shapes[0] = new Line2D.Double(10,150,250,150);
//参数表示起点和终点
for (Shape shape:shapes){
//遍历图形数组
g2d.draw(shape);//绘制图形
}
}
}
-
Point2D类:点
-
QuadCurve2D类:二次曲线
class drawplace extends JPanel{
public void paint(Graphics g){
Graphics2D g2d =(Graphics2D)g;
Shape[] shapes = new Shape[1];
shapes[0] = new QuadCurve2D.Double(100,100,10,150,250,300);
//参数表示起点,控制点,终点
for (Shape shape:shapes){
//遍历图形数组
g2d.draw(shape);//绘制图形
}
}
}
- Rectangle2D类:矩形
- RoundRectangle2D类:圆角矩形
例子: