张世豪
6 天以前 9d171a3c3a57ea54454d7e9d64dec213aa885a2c
src/zhuye/MapRenderer.java
@@ -18,6 +18,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.io.File;
import set.Setsys;
import gecaoji.Device;
import gecaoji.Gecaoji;
@@ -215,6 +216,11 @@
                if (handleMowerClick(e.getPoint())) {
                    return;
                }
                // 优先处理障碍物边界点点击(如果可见)
                if (obstaclePointsVisible && handleObstaclePointClick(e.getPoint())) {
                    return;
                }
                // 然后处理地块边界点点击
                if (boundaryPointsVisible) {
                    handleBoundaryPointClick(e.getPoint());
                }
@@ -376,7 +382,11 @@
        }
        if (boundaryPointsVisible && hasBoundary) {
            double markerScale = boundaryPointSizeScale * (previewSizingEnabled ? PREVIEW_BOUNDARY_MARKER_SCALE : 1.0d);
            // 预览模式下显示序号
            if (previewSizingEnabled) {
                drawBoundaryPointsWithNumbers(g2d, currentBoundary, scale);
            } else {
                double markerScale = boundaryPointSizeScale;
            pointandnumber.drawBoundaryPoints(
                g2d,
                currentBoundary,
@@ -386,21 +396,11 @@
                markerScale
            );
        }
        // 绘制障碍物坐标点
        if (obstaclePointsVisible && hasObstacles) {
            List<Point2D.Double> obstaclePoints = Obstacledraw.getObstaclePoints(currentObstacles);
            if (obstaclePoints != null && !obstaclePoints.isEmpty()) {
                double markerScale = boundaryPointSizeScale * (previewSizingEnabled ? PREVIEW_BOUNDARY_MARKER_SCALE : 1.0d);
                pointandnumber.drawBoundaryPoints(
                    g2d,
                    obstaclePoints,
                    scale,
                    BOUNDARY_POINT_MERGE_THRESHOLD,
                    OBSTACLE_POINT_COLOR,
                    markerScale
                );
            }
        // 绘制障碍物坐标点(带序号)
        if (obstaclePointsVisible && hasObstacles) {
            drawObstaclePointsWithNumbers(g2d, currentObstacles, scale);
        }
        if (shouldRenderIdleTrail()) {
@@ -1184,6 +1184,139 @@
        }
    }
    /**
     * 处理障碍物边界点点击
     * @param screenPoint 屏幕坐标点
     * @return 如果处理了点击返回true,否则返回false
     */
    private boolean handleObstaclePointClick(Point screenPoint) {
        if (currentObstacles == null || currentObstacles.isEmpty() || currentObstacleLandNumber == null) {
            return false;
        }
        double threshold = computeSelectionThresholdPixels();
        // 遍历所有障碍物,找到被点击的点
        for (Obstacledge.Obstacle obstacle : currentObstacles) {
            if (obstacle == null) {
                continue;
            }
            List<Obstacledge.XYCoordinate> xyCoords = obstacle.getXyCoordinates();
            if (xyCoords == null || xyCoords.isEmpty()) {
                continue;
            }
            // 检查每个点
            for (int i = 0; i < xyCoords.size(); i++) {
                Obstacledge.XYCoordinate coord = xyCoords.get(i);
                Point2D.Double worldPoint = new Point2D.Double(coord.getX(), coord.getY());
                Point2D.Double screenPosition = worldToScreen(worldPoint);
                double dx = screenPosition.x - screenPoint.x;
                double dy = screenPosition.y - screenPoint.y;
                if (Math.hypot(dx, dy) <= threshold) {
                    // 找到被点击的点
                    String obstacleName = obstacle.getObstacleName();
                    String pointLabel = (i + 1) + "";
                    String message = "确定要删除障碍物 \"" + obstacleName + "\" 的第" + pointLabel + "号边界点吗?";
                    int choice = JOptionPane.showConfirmDialog(
                        visualizationPanel,
                        message,
                        "删除障碍物边界点",
                        JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.WARNING_MESSAGE
                    );
                    if (choice == JOptionPane.OK_OPTION) {
                        removeObstaclePoint(obstacle, i);
                    }
                    return true;
                }
            }
        }
        return false;
    }
    /**
     * 删除障碍物的指定边界点
     */
    private void removeObstaclePoint(Obstacledge.Obstacle obstacle, int pointIndex) {
        if (obstacle == null || currentObstacleLandNumber == null) {
            return;
        }
        List<Obstacledge.XYCoordinate> xyCoords = obstacle.getXyCoordinates();
        if (xyCoords == null || pointIndex < 0 || pointIndex >= xyCoords.size()) {
            return;
        }
        // 检查删除后是否还有足够的点
        Obstacledge.ObstacleShape shape = obstacle.getShape();
        int minPoints = (shape == Obstacledge.ObstacleShape.CIRCLE) ? 2 : 3;
        if (xyCoords.size() <= minPoints) {
            JOptionPane.showMessageDialog(
                visualizationPanel,
                "障碍物至少需要" + minPoints + "个点,无法删除",
                "提示",
                JOptionPane.INFORMATION_MESSAGE
            );
            return;
        }
        // 创建新的坐标列表(移除指定点)
        List<Obstacledge.XYCoordinate> updatedCoords = new ArrayList<>(xyCoords);
        updatedCoords.remove(pointIndex);
        // 更新障碍物坐标
        obstacle.setXyCoordinates(updatedCoords);
        // 保存到配置文件
        try {
            File configFile = new File("Obstacledge.properties");
            Obstacledge.ConfigManager manager = new Obstacledge.ConfigManager();
            if (configFile.exists()) {
                manager.loadFromFile(configFile.getAbsolutePath());
            }
            Obstacledge.Plot plot = manager.getPlotById(currentObstacleLandNumber.trim());
            if (plot != null) {
                // 移除旧障碍物并添加更新后的障碍物
                plot.removeObstacleByName(obstacle.getObstacleName());
                plot.addObstacle(obstacle);
                manager.saveToFile(configFile.getAbsolutePath());
                // 更新地块更新时间
                Dikuai.updateField(currentObstacleLandNumber.trim(), "updateTime",
                    new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));
                Dikuai.saveToProperties();
                // 重新加载障碍物数据以刷新显示
                List<Obstacledge.Obstacle> updatedObstacles = new ArrayList<>();
                for (Obstacledge.Obstacle obs : currentObstacles) {
                    if (obs.getObstacleName().equals(obstacle.getObstacleName())) {
                        updatedObstacles.add(obstacle); // 使用更新后的障碍物
                    } else {
                        updatedObstacles.add(obs); // 保持其他障碍物不变
                    }
                }
                applyObstaclesToRenderer(updatedObstacles, currentObstacleLandNumber);
                visualizationPanel.repaint();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(
                visualizationPanel,
                "保存失败: " + ex.getMessage(),
                "错误",
                JOptionPane.ERROR_MESSAGE
            );
        }
    }
    private void handleBoundaryPointClick(Point screenPoint) {
        if (currentBoundary == null || currentBoundaryLandNumber == null) {
            return;
@@ -1451,6 +1584,187 @@
    /**
     * 绘制视图信息
     */
    /**
     * 绘制障碍物坐标点(带序号)
     * 序号显示在点中心,字体大小与障碍物名称一致(11号),不随缩放变化
     */
    private void drawObstaclePointsWithNumbers(Graphics2D g2d, List<Obstacledge.Obstacle> obstacles, double scale) {
        if (obstacles == null || obstacles.isEmpty()) {
            return;
        }
        // 保存原始变换
        AffineTransform originalTransform = g2d.getTransform();
        // 设置点的大小(随缩放变化)
        double scaleFactor = Math.max(0.5, scale);
        double clampedScale = boundaryPointSizeScale * (previewSizingEnabled ? PREVIEW_BOUNDARY_MARKER_SCALE : 1.0d);
        if (!Double.isFinite(clampedScale) || clampedScale <= 0.0d) {
            clampedScale = 1.0d;
        }
        double minimumDiameter = clampedScale < 1.0 ? 0.5 : 1.0;
        double markerDiameter = Math.max(minimumDiameter, (10.0 / scaleFactor) * 0.2 * clampedScale);
        double markerRadius = markerDiameter / 2.0;
        // 设置字体(与障碍物名称一致,不随缩放变化)
        Font labelFont = new Font("微软雅黑", Font.PLAIN, 11);
        g2d.setFont(labelFont);
        FontMetrics fontMetrics = g2d.getFontMetrics(labelFont);
        // 遍历所有障碍物
        for (Obstacledge.Obstacle obstacle : obstacles) {
            if (obstacle == null || !obstacle.isValid()) {
                continue;
            }
            List<Obstacledge.XYCoordinate> xyCoords = obstacle.getXyCoordinates();
            if (xyCoords == null || xyCoords.isEmpty()) {
                continue;
            }
            // 绘制每个点及其序号
            for (int i = 0; i < xyCoords.size(); i++) {
                Obstacledge.XYCoordinate coord = xyCoords.get(i);
                double x = coord.getX();
                double y = coord.getY();
                // 绘制点(在世界坐标系中,随缩放变化)
                g2d.setColor(OBSTACLE_POINT_COLOR);
                Ellipse2D.Double marker = new Ellipse2D.Double(
                    x - markerRadius,
                    y - markerRadius,
                    markerDiameter,
                    markerDiameter
                );
                g2d.fill(marker);
                // 将世界坐标转换为屏幕坐标以绘制序号(不随缩放变化)
                Point2D.Double worldPoint = new Point2D.Double(x, y);
                Point2D.Double screenPoint = new Point2D.Double();
                originalTransform.transform(worldPoint, screenPoint);
                // 保存当前变换
                AffineTransform savedTransform = g2d.getTransform();
                // 重置变换为屏幕坐标系统
                g2d.setTransform(new AffineTransform());
                // 绘制序号(在屏幕坐标系中,不随缩放变化)
                String numberText = String.valueOf(i + 1);
                int textWidth = fontMetrics.stringWidth(numberText);
                int textHeight = fontMetrics.getHeight();
                // 在点中心绘制序号
                int textX = (int)(screenPoint.x - textWidth / 2.0);
                int textY = (int)(screenPoint.y + textHeight / 4.0);
                // 绘制序号文字(无背景)
                g2d.setColor(Color.BLACK);
                g2d.drawString(numberText, textX, textY);
                // 恢复变换
                g2d.setTransform(savedTransform);
            }
        }
        // 恢复原始变换
        g2d.setTransform(originalTransform);
    }
    /**
     * 绘制边界点(带序号)
     * 序号显示在点中心,字体大小与障碍物序号一致(11号),不随缩放变化
     */
    private void drawBoundaryPointsWithNumbers(Graphics2D g2d, List<Point2D.Double> boundary, double scale) {
        if (boundary == null || boundary.size() < 2) {
            return;
        }
        // 保存原始变换
        AffineTransform originalTransform = g2d.getTransform();
        int totalPoints = boundary.size();
        boolean closed = totalPoints > 2 && areBoundaryPointsClose(boundary.get(0), boundary.get(totalPoints - 1));
        int effectiveCount = closed ? totalPoints - 1 : totalPoints;
        if (effectiveCount <= 0) {
            return;
        }
        // 设置点的大小(随缩放变化)
        double scaleFactor = Math.max(0.5, scale);
        double clampedScale = boundaryPointSizeScale * (previewSizingEnabled ? PREVIEW_BOUNDARY_MARKER_SCALE : 1.0d);
        if (!Double.isFinite(clampedScale) || clampedScale <= 0.0d) {
            clampedScale = 1.0d;
        }
        double minimumDiameter = clampedScale < 1.0 ? 0.5 : 1.0;
        double markerDiameter = Math.max(minimumDiameter, (10.0 / scaleFactor) * 0.2 * clampedScale);
        double markerRadius = markerDiameter / 2.0;
        // 设置字体(与障碍物序号一致,不随缩放变化)
        Font labelFont = new Font("微软雅黑", Font.PLAIN, 11);
        g2d.setFont(labelFont);
        FontMetrics fontMetrics = g2d.getFontMetrics(labelFont);
        // 绘制每个点及其序号
        for (int i = 0; i < effectiveCount; i++) {
            Point2D.Double point = boundary.get(i);
            double x = point.x;
            double y = point.y;
            // 绘制点(在世界坐标系中,随缩放变化)
            g2d.setColor(BOUNDARY_POINT_COLOR);
            Ellipse2D.Double marker = new Ellipse2D.Double(
                x - markerRadius,
                y - markerRadius,
                markerDiameter,
                markerDiameter
            );
            g2d.fill(marker);
            // 将世界坐标转换为屏幕坐标以绘制序号(不随缩放变化)
            Point2D.Double worldPoint = new Point2D.Double(x, y);
            Point2D.Double screenPoint = new Point2D.Double();
            originalTransform.transform(worldPoint, screenPoint);
            // 保存当前变换
            AffineTransform savedTransform = g2d.getTransform();
            // 重置变换为屏幕坐标系统
            g2d.setTransform(new AffineTransform());
            // 绘制序号(在屏幕坐标系中,不随缩放变化)
            String numberText = String.valueOf(i + 1);
            int textWidth = fontMetrics.stringWidth(numberText);
            int textHeight = fontMetrics.getHeight();
            // 在点中心绘制序号
            int textX = (int)(screenPoint.x - textWidth / 2.0);
            int textY = (int)(screenPoint.y + textHeight / 4.0);
            // 绘制序号文字(无背景)
            g2d.setColor(Color.BLACK);
            g2d.drawString(numberText, textX, textY);
            // 恢复变换
            g2d.setTransform(savedTransform);
        }
        // 恢复原始变换
        g2d.setTransform(originalTransform);
    }
    /**
     * 检查两个边界点是否接近(用于判断边界是否闭合)
     */
    private boolean areBoundaryPointsClose(Point2D.Double a, Point2D.Double b) {
        if (a == null || b == null) {
            return false;
        }
        double dx = a.x - b.x;
        double dy = a.y - b.y;
        return Math.hypot(dx, dy) <= BOUNDARY_POINT_MERGE_THRESHOLD;
    }
    private void drawViewInfo(Graphics2D g2d) {
        g2d.setColor(new Color(80, 80, 80));
        g2d.setFont(new Font("微软雅黑", Font.PLAIN, 11));