张世豪
2025-12-12 4dd7a3a44f8c3d0bc658b8e3ac4ab84378551a55
src/zhuye/MapRenderer.java
@@ -28,6 +28,7 @@
import zhangaiwu.Obstacledraw;
import zhangaiwu.Obstacledge;
import zhangaiwu.yulanzhangaiwu;
import yaokong.Control03;
/**
 * 地图渲染器 - 负责坐标系绘制、视图变换等功能
@@ -38,7 +39,9 @@
    private double translateX = 0.0;
    private double translateY = 0.0;
    private Point lastDragPoint;
    private Point mousePoint;
    private static final double MIN_SCALE = 0.05d;
    private static final double MAX_SCALE = 50.0d;
    private static final double SCALE_EPSILON = 1e-6d;
    
    // 主题颜色
    private final Color THEME_COLOR = new Color(46, 139, 87);
@@ -50,6 +53,7 @@
    private static final double CIRCLE_SAMPLE_SIZE = 0.54d;
    private static final double BOUNDARY_POINT_MERGE_THRESHOLD = 0.05;
    private static final double BOUNDARY_CONTAINS_TOLERANCE = 0.05;
    private static final double PREVIEW_BOUNDARY_MARKER_SCALE = 0.25d;
    
    // 组件引用
    private JPanel visualizationPanel;
@@ -64,6 +68,8 @@
    private String currentObstacleLandNumber;
    private String boundaryName;
    private boolean boundaryPointsVisible;
    private double boundaryPointSizeScale = 1.0d;
    private boolean previewSizingEnabled;
    private String currentBoundaryLandNumber;
    private boolean dragInProgress;
    private final Gecaoji mower;
@@ -74,6 +80,7 @@
    private final List<Point2D.Double> realtimeMowingTrack = new ArrayList<>();
    private final Deque<tuowei.TrailSample> idleMowerTrail = new ArrayDeque<>();
    private final List<Point2D.Double> handheldBoundaryPreview = new ArrayList<>();
    private double boundaryPreviewMarkerScale = 1.0d;
    private boolean realtimeTrackRecording;
    private String realtimeTrackLandNumber;
    private double mowerEffectiveWidthMeters;
@@ -96,6 +103,7 @@
    public static final int DEFAULT_IDLE_TRAIL_DURATION_SECONDS = 60;
    private static final double IDLE_TRAIL_SAMPLE_DISTANCE_METERS = 0.05d;
    private long idleTrailDurationMs = DEFAULT_IDLE_TRAIL_DURATION_SECONDS * 1_000L;
    private static final double ZOOM_STEP_FACTOR = 1.2d;
    
    public MapRenderer(JPanel visualizationPanel) {
        this.visualizationPanel = visualizationPanel;
@@ -111,10 +119,10 @@
    private void setupMouseListeners() {
        // 鼠标滚轮缩放
        visualizationPanel.addMouseWheelListener(e -> {
            mousePoint = e.getPoint();
            Point referencePoint = e.getPoint();
            int notches = e.getWheelRotation();
            double zoomFactor = notches < 0 ? 1.2 : 1/1.2;
            zoomAtPoint(zoomFactor);
            double zoomFactor = notches < 0 ? ZOOM_STEP_FACTOR : 1 / ZOOM_STEP_FACTOR;
            zoomAtPoint(referencePoint, zoomFactor);
        });
        
        // 鼠标拖拽移动
@@ -164,12 +172,6 @@
                    visualizationPanel.repaint();
                }
            }
            public void mouseMoved(MouseEvent e) {
                // 更新鼠标位置用于显示坐标
                mousePoint = e.getPoint();
                visualizationPanel.repaint();
            }
        });
    }
@@ -193,26 +195,59 @@
    /**
     * 基于鼠标位置的缩放
     */
    private void zoomAtPoint(double zoomFactor) {
        if (mousePoint == null) return;
        // 计算鼠标位置在世界坐标系中的坐标
        double worldX = (mousePoint.x - visualizationPanel.getWidth()/2) / scale - translateX;
        double worldY = (mousePoint.y - visualizationPanel.getHeight()/2) / scale - translateY;
    private void zoomAtPoint(Point referencePoint, double zoomFactor) {
        if (visualizationPanel == null) {
            return;
        }
        if (referencePoint == null) {
            referencePoint = new Point(visualizationPanel.getWidth() / 2, visualizationPanel.getHeight() / 2);
        }
        scale *= zoomFactor;
        scale = Math.max(0.05, Math.min(scale, 50.0)); // 限制缩放范围,允许最多缩小至原始的1/20
        // 计算缩放后同一鼠标位置在世界坐标系中的新坐标
        double newWorldX = (mousePoint.x - visualizationPanel.getWidth()/2) / scale - translateX;
        double newWorldY = (mousePoint.y - visualizationPanel.getHeight()/2) / scale - translateY;
        // 调整平移量,使鼠标指向的世界坐标保持不变
        double panelCenterX = visualizationPanel.getWidth() / 2.0;
        double panelCenterY = visualizationPanel.getHeight() / 2.0;
        double worldX = (referencePoint.x - panelCenterX) / scale - translateX;
        double worldY = (referencePoint.y - panelCenterY) / scale - translateY;
    scale *= zoomFactor;
    scale = Math.max(MIN_SCALE, Math.min(scale, MAX_SCALE)); // 限制缩放范围,允许最多缩小至原始的1/20
        double newWorldX = (referencePoint.x - panelCenterX) / scale - translateX;
        double newWorldY = (referencePoint.y - panelCenterY) / scale - translateY;
        translateX += (newWorldX - worldX);
        translateY += (newWorldY - worldY);
        visualizationPanel.repaint();
    }
    public void zoomInFromCenter() {
        zoomAtPoint(null, ZOOM_STEP_FACTOR);
    }
    public void zoomOutFromCenter() {
        zoomAtPoint(null, 1 / ZOOM_STEP_FACTOR);
    }
    public boolean canZoomIn() {
        return scale < MAX_SCALE - SCALE_EPSILON;
    }
    public boolean canZoomOut() {
        return scale > MIN_SCALE + SCALE_EPSILON;
    }
    public double getScale() {
        return scale;
    }
    public double getMaxScale() {
        return MAX_SCALE;
    }
    public double getMinScale() {
        return MIN_SCALE;
    }
    
    /**
     * 重置视图
@@ -264,19 +299,21 @@
            drawCircleCaptureOverlay(g2d, circleCaptureOverlay, scale);
        }
        adddikuaiyulan.drawPreview(g2d, handheldBoundaryPreview, scale, handheldBoundaryPreviewActive);
    adddikuaiyulan.drawPreview(g2d, handheldBoundaryPreview, scale, handheldBoundaryPreviewActive, boundaryPreviewMarkerScale);
        if (hasPlannedPath) {
            drawCurrentPlannedPath(g2d);
        }
        if (boundaryPointsVisible && hasBoundary) {
            double markerScale = boundaryPointSizeScale * (previewSizingEnabled ? PREVIEW_BOUNDARY_MARKER_SCALE : 1.0d);
            pointandnumber.drawBoundaryPoints(
                g2d,
                currentBoundary,
                scale,
                BOUNDARY_POINT_MERGE_THRESHOLD,
                BOUNDARY_POINT_COLOR
                BOUNDARY_POINT_COLOR,
                markerScale
            );
        }
@@ -648,6 +685,10 @@
        visualizationPanel.repaint();
    }
    public void clearIdleTrail() {
        clearIdleMowerTrail();
    }
    public void setIdleTrailDurationSeconds(int seconds) {
        int sanitized = seconds;
        if (sanitized < 5 || sanitized > 600) {
@@ -865,7 +906,8 @@
    }
    private void drawCurrentPlannedPath(Graphics2D g2d) {
        lujingdraw.drawPlannedPath(g2d, currentPlannedPath, scale);
        double arrowScale = previewSizingEnabled ? 0.5d : 1.0d;
        lujingdraw.drawPlannedPath(g2d, currentPlannedPath, scale, arrowScale);
    }
    private void drawCircleSampleMarkers(Graphics2D g2d, List<double[]> markers, double scale) {
@@ -1054,7 +1096,11 @@
    private double computeSelectionThresholdPixels() {
        double scaleFactor = Math.max(0.5, scale);
        double markerDiameterWorld = Math.max(1.0, (10.0 / scaleFactor) * 0.2);
        double diameterScale = boundaryPointSizeScale * (previewSizingEnabled ? PREVIEW_BOUNDARY_MARKER_SCALE : 1.0d);
        if (!Double.isFinite(diameterScale) || diameterScale <= 0.0d) {
            diameterScale = 1.0d;
        }
        double markerDiameterWorld = Math.max(1.0, (10.0 / scaleFactor) * 0.2 * diameterScale);
        double markerDiameterPixels = markerDiameterWorld * scale;
        return Math.max(8.0, markerDiameterPixels * 1.5);
    }
@@ -1245,30 +1291,34 @@
    private void drawViewInfo(Graphics2D g2d) {
        g2d.setColor(new Color(80, 80, 80));
        g2d.setFont(new Font("微软雅黑", Font.PLAIN, 11));
        // 只显示缩放比例,去掉平移信息
        String info = String.format("缩放: %.2fx", scale);
        g2d.drawString(info, 15, visualizationPanel.getHeight() - 15);
        if (mousePoint != null) {
            // 计算鼠标位置在世界坐标系中的坐标
            double worldX = (mousePoint.x - visualizationPanel.getWidth()/2) / scale - translateX;
            double worldY = (mousePoint.y - visualizationPanel.getHeight()/2) / scale - translateY;
            String mouseCoord = String.format("坐标: (%.1f, %.1f)m", worldX, worldY);
            g2d.drawString(mouseCoord, visualizationPanel.getWidth() - 150, visualizationPanel.getHeight() - 15);
        // 在地图顶部左侧显示遥控摇杆对应速度(若非零)
        try {
            int forward = Control03.getCurrentForwardSpeed();
            int steer = Control03.getCurrentSteeringSpeed();
            if (forward != 0 || steer != 0) {
                String speedInfo = String.format("行进:%d 转向:%d", forward, steer);
                // 背景半透明矩形增强可读性
                FontMetrics fm = g2d.getFontMetrics();
                int padding = 6;
                int w = fm.stringWidth(speedInfo) + padding * 2;
                int h = fm.getHeight() + padding;
                int x = 12;
                int y = 12;
                Color bg = new Color(255, 255, 255, 180);
                g2d.setColor(bg);
                g2d.fillRoundRect(x, y, w, h, 8, 8);
                g2d.setColor(new Color(120, 120, 120));
                g2d.drawString(speedInfo, x + padding, y + fm.getAscent() + (padding/2));
            }
        } catch (Throwable t) {
            // 不应阻塞渲染,静默处理任何异常
        }
        // 去掉操作提示:"滚轮缩放 | 拖拽移动 | 右键重置"
        // String tips = "滚轮缩放 | 拖拽移动 | 右键重置";
        // g2d.drawString(tips, visualizationPanel.getWidth() - 200, 30);
    }
    /**
     * 获取当前缩放比例
     */
    public double getScale() {
        return scale;
        // 保留底部的缩放比例信息
        String info = String.format("缩放: %.2fx", scale);
        g2d.setColor(new Color(80, 80, 80));
        g2d.drawString(info, 15, visualizationPanel.getHeight() - 15);
    }
    
    /**
@@ -1830,6 +1880,65 @@
        visualizationPanel.repaint();
    }
    public void setBoundaryPointSizeScale(double sizeScale) {
        double normalized = (Double.isFinite(sizeScale) && sizeScale > 0.0d) ? sizeScale : 1.0d;
        if (Math.abs(boundaryPointSizeScale - normalized) < 1e-6) {
            return;
        }
        boundaryPointSizeScale = normalized;
        if (visualizationPanel == null) {
            return;
        }
        if (SwingUtilities.isEventDispatchThread()) {
            visualizationPanel.repaint();
        } else {
            SwingUtilities.invokeLater(visualizationPanel::repaint);
        }
    }
    public void setPathPreviewSizingEnabled(boolean enabled) {
        previewSizingEnabled = enabled;
        if (visualizationPanel == null) {
            return;
        }
        if (SwingUtilities.isEventDispatchThread()) {
            visualizationPanel.repaint();
        } else {
            SwingUtilities.invokeLater(visualizationPanel::repaint);
        }
    }
    public void setBoundaryPreviewMarkerScale(double markerScale) {
        double normalized = Double.isFinite(markerScale) && markerScale > 0.0d ? markerScale : 1.0d;
        if (Math.abs(boundaryPreviewMarkerScale - normalized) < 1e-6) {
            return;
        }
        boundaryPreviewMarkerScale = normalized;
        if (visualizationPanel == null) {
            return;
        }
        if (SwingUtilities.isEventDispatchThread()) {
            visualizationPanel.repaint();
        } else {
            SwingUtilities.invokeLater(visualizationPanel::repaint);
        }
    }
    public boolean setHandheldMowerIconActive(boolean handheldActive) {
        if (mower == null) {
            return false;
        }
        boolean changed = mower.useHandheldIcon(handheldActive);
        if (changed && visualizationPanel != null) {
            if (SwingUtilities.isEventDispatchThread()) {
                visualizationPanel.repaint();
            } else {
                SwingUtilities.invokeLater(visualizationPanel::repaint);
            }
        }
        return changed;
    }
    public void beginHandheldBoundaryPreview() {
        handheldBoundaryPreviewActive = true;
        handheldBoundaryPreview.clear();
@@ -1859,6 +1968,7 @@
    public void clearHandheldBoundaryPreview() {
        handheldBoundaryPreviewActive = false;
        handheldBoundaryPreview.clear();
        boundaryPreviewMarkerScale = 1.0d;
        visualizationPanel.repaint();
    }
@@ -1914,8 +2024,10 @@
            return;
        }
        double width = Math.max(bounds.width, 1);
        double height = Math.max(bounds.height, 1);
        Rectangle2D.Double targetBounds = includeMowerInBounds(bounds);
        double width = Math.max(targetBounds.width, 1);
        double height = Math.max(targetBounds.height, 1);
        double targetWidth = width * 1.2;
        double targetHeight = height * 1.2;
@@ -1927,8 +2039,41 @@
        newScale = Math.max(0.05, Math.min(newScale, 50.0));
        this.scale = newScale;
        this.translateX = -bounds.getCenterX();
        this.translateY = -bounds.getCenterY();
        this.translateX = -targetBounds.getCenterX();
        this.translateY = -targetBounds.getCenterY();
    }
    // Keep the mower marker inside the viewport whenever the camera refits to scene bounds.
    private Rectangle2D.Double includeMowerInBounds(Rectangle2D.Double bounds) {
        Rectangle2D.Double expanded = new Rectangle2D.Double(
            bounds.x,
            bounds.y,
            Math.max(0.0, bounds.width),
            Math.max(0.0, bounds.height)
        );
        if (mower == null || !mower.hasValidPosition()) {
            return expanded;
        }
        Point2D.Double mowerPosition = mower.getPosition();
        if (mowerPosition == null
            || !Double.isFinite(mowerPosition.x)
            || !Double.isFinite(mowerPosition.y)) {
            return expanded;
        }
        double minX = Math.min(expanded.x, mowerPosition.x);
        double minY = Math.min(expanded.y, mowerPosition.y);
        double maxX = Math.max(expanded.x + expanded.width, mowerPosition.x);
        double maxY = Math.max(expanded.y + expanded.height, mowerPosition.y);
        expanded.x = minX;
        expanded.y = minY;
        expanded.width = Math.max(0.0, maxX - minX);
        expanded.height = Math.max(0.0, maxY - minY);
        return expanded;
    }
    public void dispose() {