package zhuye; // 包声明
|
|
import java.awt.BasicStroke; // 描边类
|
import java.awt.Color; // 颜色类
|
import java.awt.Graphics2D; // 绘图上下文
|
import java.awt.geom.Path2D; // 路径类
|
import java.awt.geom.Point2D; // 坐标点类
|
import java.util.List; // 列表接口
|
|
/** // 文档说明开始
|
* Utility for rendering boundary polygons with theme-aligned colors. // 工具类说明
|
*/ // 文档说明结束
|
public final class bianjiedrwa { // 工具类定义
|
private bianjiedrwa() { // 私有构造防止实例化
|
} // 构造结束
|
|
/** // 文档说明开始
|
* Draw boundary polygon using provided stroke/fill colors. // 方法说明
|
*/ // 文档说明结束
|
public static void drawBoundary(Graphics2D g2d, List<Point2D.Double> boundary,
|
double scale, Color fillColor, Color borderColor) { // 绘制方法
|
if (boundary == null || boundary.size() < 2) { // 判空
|
return; // 无数据返回
|
} // if结束
|
|
float strokeWidth = (float) (3 / Math.max(0.5, scale)); // 计算边线宽度
|
|
// 填充区域
|
Path2D fillPath = new Path2D.Double(); // 创建填充路径
|
boolean first = true; // 首点标记
|
for (Point2D.Double point : boundary) { // 遍历点
|
if (first) { // 首个点
|
fillPath.moveTo(point.x, point.y); // 移动到开始点
|
first = false; // 更新标记
|
} else { // 其他点
|
fillPath.lineTo(point.x, point.y); // 连线到下个点
|
} // if结束
|
} // for结束
|
fillPath.closePath(); // 闭合路径
|
|
g2d.setColor(fillColor); // 设置填充色
|
g2d.fill(fillPath); // 填充区域
|
|
// 绘制边界线(包括起点到终点的连接)- 实线
|
if (boundary.size() >= 2) {
|
Path2D.Double borderPath = new Path2D.Double(); // 创建边界路径
|
Point2D.Double firstPoint = boundary.get(0);
|
borderPath.moveTo(firstPoint.x, firstPoint.y); // 移动到起点
|
for (int i = 1; i < boundary.size(); i++) { // 从第二个点到最后一个点
|
Point2D.Double point = boundary.get(i);
|
borderPath.lineTo(point.x, point.y); // 连线
|
}
|
// 如果最后一个点不是第一个点,则连接到起点形成闭合
|
Point2D.Double lastPoint = boundary.get(boundary.size() - 1);
|
if (!lastPoint.equals(firstPoint)) {
|
borderPath.lineTo(firstPoint.x, firstPoint.y); // 连接到起点形成闭合
|
}
|
|
g2d.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); // 设置实线描边
|
g2d.setColor(borderColor); // 设置边线颜色
|
g2d.draw(borderPath); // 绘制完整边界(包括起点到终点的连接)
|
}
|
} // 方法结束
|
} // 类结束
|