| | |
| | | package zhuye; // 包声明 |
| | | |
| | | import java.awt.Color; // 颜色类 |
| | | import java.awt.Font; // 字体类 |
| | | import java.awt.FontMetrics; // 字体度量 |
| | | import java.awt.Graphics2D; // 绘图上下文 |
| | | import java.awt.geom.Ellipse2D; // 圆形绘制 |
| | | import java.awt.geom.Point2D; // 二维坐标点 |
| | | import java.util.List; // 列表接口 |
| | | |
| | | /** |
| | | * Utility for rendering boundary vertices and numeric labels. // 工具类说明 |
| | | * Utility for rendering boundary vertices. // 工具类说明 |
| | | */ |
| | | public final class pointandnumber { // 工具类定义 |
| | | private pointandnumber() { // 私有构造防止实例化 |
| | | } |
| | | |
| | | /** |
| | | * Draw numbered boundary points with consistent styling. |
| | | * Draw boundary points with consistent styling. |
| | | */ |
| | | public static void drawBoundaryPoints(Graphics2D g2d, // 绘制方法入口 |
| | | List<Point2D.Double> boundary, // 边界点集合 |
| | | double scale, // 缩放比例 |
| | | double mergeThreshold, // 合并阈值 |
| | | Color pointColor, // 点颜色 |
| | | Color labelColor) { // 序号颜色 |
| | | double diameterScale) { // 直径缩放因子 |
| | | if (boundary == null || boundary.size() < 2) { // 判断数据是否有效 |
| | | return; // 无效直接返回 |
| | | } |
| | |
| | | return; // 无效返回 |
| | | } |
| | | |
| | | double scaleFactor = Math.max(0.5, scale); // 防止过小缩放 |
| | | double markerDiameter = Math.max(1.0, (10.0 / scaleFactor) * 0.2); // 描点直径 |
| | | double scaleFactor = Math.max(0.5, scale); // 防止过小缩放 |
| | | // 边界点直径与边界线宽度一致:3 / Math.max(0.5, scale) |
| | | double markerDiameter = 3.0 / scaleFactor; // 描点直径(与边界线宽度一致) |
| | | double markerRadius = markerDiameter / 2.0; // 半径 |
| | | int fontSize =2; // 字体大小 |
| | | |
| | | Font originalFont = g2d.getFont(); // 保存原字体 |
| | | g2d.setFont(new Font("Times New Roman ", Font.CENTER_BASELINE, fontSize)); // 切换字体 |
| | | FontMetrics metrics = g2d.getFontMetrics(); // 字体度量 |
| | | |
| | | for (int i = 0; i < effectiveCount; i++) { // 遍历有效点 |
| | | Point2D.Double point = boundary.get(i); // 当前点 |
| | |
| | | g2d.setColor(pointColor); // 设置点颜色 |
| | | Ellipse2D marker = new Ellipse2D.Double(x - markerRadius, y - markerRadius, markerDiameter, markerDiameter); // 创建圆 |
| | | g2d.fill(marker); // 绘制圆 |
| | | |
| | | String label = String.valueOf(i + 1); // 序号文本 |
| | | int textWidth = metrics.stringWidth(label); // 文本宽度 |
| | | g2d.setColor(labelColor); // 设置文本颜色 |
| | | float baselineX = (float) (x - textWidth / 2.0); // 计算X基线 |
| | | float baselineY = (float) (y + (metrics.getAscent() - metrics.getDescent()) / 2.0); // 计算Y基线 |
| | | g2d.drawString(label, baselineX, baselineY); // 绘制文本 |
| | | } |
| | | |
| | | g2d.setFont(originalFont); // 还原字体 |
| | | } |
| | | |
| | | private static boolean arePointsClose(Point2D.Double a, Point2D.Double b, double threshold) { // 点距离判断 |