Java Graphics2D 基础图形绘制详解
使用 Java 进行图形处理时,Graphics2D 是核心 API。以下介绍从简单到复杂图形的绘制方法及实例代码。
1. Point
Point 代表图中的一个点,用(x,y)表示。
// int Point point = new Point(0, 0);
// double Point2D point1 = new Point2D.Double(1D, 1D);
// float Point2D point2 = new Point2D.Float(1.0F, 1.0F);
2. Line
Line2D 表示坐标空间中的线段实现。
Line2D line2D = new Line2D.Double(0D, 0D, 100D, 100D);
Line2D line2D1 = new Line2D.Float(0F, 0F, 100F, 100F);
创建一条线代码实例:
BufferedImage image = loadImage();
Graphics2D graphics2D = image.createGraphics();
graphics2D.setColor(Color.RED);
// 代表 从坐标 (1,1) 到 (100,100) 坐标画一条红线
graphics2D.draw(new Line2D.Double(1, 1, 100, 100));
graphics2D.dispose();
image.flush();
ImgUtil.write(image, new File(SAVE_FILE_PATH + "line.png"));
3. 二次曲线段
QuadCurve2D 能够创建二次参数曲线段。二次曲线由两个端点和一个控制点定义。
该类实现 Shape 接口。此类表示 (x, y) 坐标空间中的二次参数曲线段。有几种 setCurve 方法用于指定曲线的两个端点和一个控制点。
BufferedImage image = loadImage();
Graphics2D graphics2D = image.createGraphics();
graphics2D.setColor(Color.RED);
QuadCurve2D q = new QuadCurve2D.Float();
double x1 = 10;
double y1 = 10;
double ctrlx = 10;
double ctrly = 60;
double x2 = 100;
double y2 = 10;
q.setCurve(x1, y1, ctrlx, ctrly, x2, y2);
graphics2D.draw(q);
graphics2D.dispose();
image.flush();
ImgUtil.write(image, new File(SAVE_FILE_PATH + "Curve.png"));
4. 三次曲线段
CubicCurve2D 实现 Shape 接口。表示 (x, y) 坐标空间中的三次参数化曲线段。
BufferedImage image = loadImage();
Graphics2D graphics2D = image.createGraphics();
graphics2D.setColor(Color.RED);
CubicCurve2D c = new CubicCurve2D.Double();
c.setCurve(10, 40, 30, 5, 60, 40, 100, 30);
graphics2D.draw(c);
graphics2D.dispose();
image.flush();
ImgUtil.write(image, new File(SAVE_FILE_PATH + "Curve2.png"));
5. 长方形
绘制长方形使用 Rectangle2D 绘制,该类扩展了类 RectangularShape。
BufferedImage image = loadImage();
Graphics2D graphics2D = image.createGraphics();
graphics2D.setColor(Color.RED);
int x = 10;
int y = 10;
int rectWidth = 100;
int rectHeight = 50;
graphics2D.draw(new Rectangle2D.Double(x, y, rectWidth, rectHeight));
graphics2D.dispose();
image.flush();
ImgUtil.write(image, new File(SAVE_FILE_PATH + "Rectangle.png"));
6. 圆角矩形
通过 RoundRectangle2D 实现圆角矩形绘制,也继承至 RectangularShape。
BufferedImage image = loadImage();
Graphics2D graphics2D = image.createGraphics();
graphics2D.setColor(Color.RED);
int x = 10;
int y = 10;
int rectWidth = 100;
int rectHeight = 50;
double arcw = 10;
double arch = 10;
graphics2D.draw(new RoundRectangle2D.Double(x, y, rectWidth, rectHeight, arcw, arch));
graphics2D.dispose();
image.flush();
ImgUtil.write(image, new File(SAVE_FILE_PATH + "RoundRectangle2D.png"));
7. 椭圆、圆
绘制椭圆、圆由 Ellipse2D 类实现。
BufferedImage image = loadImage();
Graphics2D graphics2D = image.createGraphics();
graphics2D.setColor(Color.RED);
int x = 1;
int y = 1;
int rectWidth = 100;
int rectHeight = 100;
// 绘制圆
graphics2D.draw(new Ellipse2D.Double(x, y, rectWidth, rectHeight));
// 绘制椭圆
graphics2D.draw(new Ellipse2D.Double(x + 150, y + 150, rectWidth, rectHeight / 2));
graphics2D.dispose();
image.flush();
ImgUtil.write(image, new File(SAVE_FILE_PATH + "Ellipse2D.png"));
8. 圆弧、闭合圆弧、扇形
通过 Arc2D 类绘制实现圆弧、闭合圆弧、扇形。
BufferedImage image = loadImage();
Graphics2D graphics2D = image.createGraphics();
graphics2D.setColor(Color.RED);
int x = 10;
int y = 10;
int rectWidth = 100;
int rectHeight = 100;
int start = 90;
int extent = 135;
// 圆弧
graphics2D.draw(new Arc2D.Double(x, y, rectWidth, rectHeight, 90, 135, Arc2D.OPEN));
// 圆弧闭合
graphics2D.draw(new Arc2D.Double(x + 50, y + 50, rectWidth, rectHeight, 90, 135, Arc2D.CHORD));
// 扇形
graphics2D.draw(new Arc2D.Double(x + 100, y + 100, rectWidth, rectHeight, 90, 135, Arc2D.PIE));
graphics2D.dispose();
image.flush();
ImgUtil.write(image, new File(SAVE_FILE_PATH + "Arc2D.png"));

