| | |
| | | import java.awt.geom.Point2D; |
| | | import java.awt.geom.Rectangle2D; |
| | | import java.math.BigDecimal; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayDeque; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.Date; |
| | | import java.util.Deque; |
| | | import java.util.LinkedHashSet; |
| | | 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; |
| | | import gecaoji.GecaojiMeg; |
| | | import gecaoji.gecaolunjing; |
| | | import gecaoji.lujingdraw; |
| | | import dikuai.Dikuaiguanli; |
| | | import dikuai.Dikuai; |
| | | import zhangaiwu.Obstacledraw; |
| | | import zhangaiwu.Obstacledge; |
| | | import zhangaiwu.yulanzhangaiwu; |
| | | import yaokong.Control03; |
| | | import bianjie.shudongdraw; |
| | | |
| | | /** |
| | | * 地图渲染器 - 负责坐标系绘制、视图变换等功能 |
| | | */ |
| | | public class MapRenderer { |
| | | // 视图变换参数 |
| | | private double scale = 1.0; |
| | | private static final double DEFAULT_SCALE = 20.0; // 默认缩放比例 |
| | | private double scale = DEFAULT_SCALE; |
| | | 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 static final String MAP_SCALE_PROPERTY = "mapScale"; // 属性文件中的键名 |
| | | |
| | | // 主题颜色 |
| | | private final Color THEME_COLOR = new Color(46, 139, 87); |
| | |
| | | private static final Color GRASS_FILL_COLOR = new Color(144, 238, 144, 120); |
| | | private static final Color GRASS_BORDER_COLOR = new Color(60, 179, 113); |
| | | private static final Color BOUNDARY_POINT_COLOR = new Color(128, 0, 128); |
| | | private static final Color BOUNDARY_LABEL_COLOR = Color.BLACK; |
| | | private static final Color OBSTACLE_POINT_COLOR = new Color(255, 140, 0); // 橙色,用于区分障碍物点 |
| | | private static final Color CIRCLE_SAMPLE_COLOR = new Color(220, 20, 60, 230); |
| | | private static final double CIRCLE_SAMPLE_SIZE = 0.54d; |
| | | private static final double BOUNDARY_POINT_MERGE_THRESHOLD = 0.05; |
| | | private static final SimpleDateFormat TIMESTAMP_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | private static final Color HANDHELD_BOUNDARY_FILL = new Color(51, 153, 255, 60); |
| | | private static final Color HANDHELD_BOUNDARY_BORDER = new Color(51, 102, 204, 220); |
| | | private static final Color HANDHELD_BOUNDARY_POINT = new Color(51, 102, 204); |
| | | private static final Color HANDHELD_BOUNDARY_LABEL = new Color(22, 62, 138); |
| | | private static final double BOUNDARY_CONTAINS_TOLERANCE = 0.05; |
| | | private static final double PREVIEW_BOUNDARY_MARKER_SCALE = 0.25d; |
| | | |
| | | // 组件引用 |
| | | private JPanel visualizationPanel; |
| | | private List<Point2D.Double> currentBoundary; |
| | | private Rectangle2D.Double boundaryBounds; |
| | | private Path2D.Double currentBoundaryPath; |
| | | private List<Point2D.Double> currentPlannedPath; |
| | | private Rectangle2D.Double plannedPathBounds; |
| | | private List<Obstacledge.Obstacle> currentObstacles; |
| | |
| | | private String currentObstacleLandNumber; |
| | | private String boundaryName; |
| | | private boolean boundaryPointsVisible; |
| | | private boolean obstaclePointsVisible; |
| | | private boolean boundaryLengthVisible = false; // 是否显示边界距离,默认关闭 |
| | | private double boundaryPointSizeScale = 1.0d; |
| | | private boolean previewSizingEnabled; |
| | | private String currentBoundaryLandNumber; |
| | | private boolean dragInProgress; |
| | | private final Gecaoji mower; |
| | | private final Timer mowerUpdateTimer; |
| | | private JDialog mowerInfoDialog; |
| | | private Timer mowerInfoRefreshTimer; |
| | | private JLabel mowerNumberValueLabel; |
| | | private JLabel realtimeXValueLabel; |
| | | private JLabel realtimeYValueLabel; |
| | | private JLabel positioningStatusValueLabel; |
| | | private JLabel satelliteCountValueLabel; |
| | | private JLabel realtimeSpeedValueLabel; |
| | | private JLabel headingValueLabel; |
| | | private JLabel updateTimeValueLabel; |
| | | private final GecaojiMeg mowerInfoManager; |
| | | private CircleCaptureOverlay circleCaptureOverlay; |
| | | private final List<double[]> circleSampleMarkers = new ArrayList<>(); |
| | | private final List<Point2D.Double> realtimeMowingTrack = new ArrayList<>(); |
| | | private final List<Point2D.Double> navigationPreviewTrack = 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; |
| | |
| | | private double mowingCompletionRatio; |
| | | private long lastTrackPersistTimeMillis; |
| | | private boolean trackDirty; |
| | | private boolean measurementModeActive = false; // 测量模式是否激活 |
| | | private boolean handheldBoundaryPreviewActive; |
| | | private boolean pendingTrackBreak = true; |
| | | private bianjie.shudongdraw manualBoundaryDrawer = new bianjie.shudongdraw(); // 手动绘制边界绘制器 |
| | | private boolean idleTrailSuppressed; |
| | | private Path2D.Double realtimeBoundaryPathCache; |
| | | private String realtimeBoundaryPathLand; |
| | | private WangfanDraw returnPathDrawer; // 往返路径绘制管理器 |
| | | private List<Point2D.Double> currentReturnPath; // 当前地块的往返路径(用于显示) |
| | | private List<Point2D.Double> previewReturnPath; // 预览的往返路径 |
| | | |
| | | private static final double TRACK_SAMPLE_MIN_DISTANCE_METERS = 0.1d; |
| | | private static final double TRACK_SAMPLE_MIN_DISTANCE_METERS = 0.2d; |
| | | private static final double TRACK_DUPLICATE_TOLERANCE_METERS = 1e-3d; |
| | | private static final long TRACK_PERSIST_INTERVAL_MS = 5_000L; |
| | | 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; |
| | | this.mower = new Gecaoji(); |
| | | this.mowerUpdateTimer = createMowerTimer(); |
| | | this.mowerInfoManager = new GecaojiMeg(visualizationPanel, mower); |
| | | setupMouseListeners(); |
| | | // 从配置文件读取上次保存的缩放比例和视图中心坐标 |
| | | loadViewSettingsFromProperties(); |
| | | } |
| | | |
| | | /** |
| | | * 从配置文件读取缩放比例和视图中心坐标 |
| | | */ |
| | | private void loadViewSettingsFromProperties() { |
| | | // 加载缩放比例 |
| | | String scaleValue = Setsys.getPropertyValue(MAP_SCALE_PROPERTY); |
| | | if (scaleValue != null && !scaleValue.trim().isEmpty()) { |
| | | try { |
| | | double savedScale = Double.parseDouble(scaleValue.trim()); |
| | | // 验证缩放比例是否在有效范围内 |
| | | if (savedScale >= MIN_SCALE && savedScale <= MAX_SCALE) { |
| | | scale = savedScale; |
| | | } else { |
| | | scale = DEFAULT_SCALE; |
| | | } |
| | | } catch (NumberFormatException e) { |
| | | // 如果解析失败,使用默认值 |
| | | scale = DEFAULT_SCALE; |
| | | } |
| | | } else { |
| | | // 如果没有保存的值,使用默认值 |
| | | scale = DEFAULT_SCALE; |
| | | } |
| | | |
| | | // 加载视图中心坐标 |
| | | String viewCenterXValue = Setsys.getPropertyValue("viewCenterX"); |
| | | String viewCenterYValue = Setsys.getPropertyValue("viewCenterY"); |
| | | if (viewCenterXValue != null && !viewCenterXValue.trim().isEmpty()) { |
| | | try { |
| | | translateX = Double.parseDouble(viewCenterXValue.trim()); |
| | | } catch (NumberFormatException e) { |
| | | translateX = 0.0; |
| | | } |
| | | } else { |
| | | translateX = 0.0; |
| | | } |
| | | if (viewCenterYValue != null && !viewCenterYValue.trim().isEmpty()) { |
| | | try { |
| | | translateY = Double.parseDouble(viewCenterYValue.trim()); |
| | | } catch (NumberFormatException e) { |
| | | translateY = 0.0; |
| | | } |
| | | } else { |
| | | translateY = 0.0; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 保存缩放比例到配置文件 |
| | | */ |
| | | private void saveScaleToProperties() { |
| | | Setsys setsys = new Setsys(); |
| | | // 保留2位小数 |
| | | setsys.updateProperty(MAP_SCALE_PROPERTY, String.format("%.2f", scale)); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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); |
| | | }); |
| | | |
| | | // 鼠标拖拽移动 |
| | |
| | | lastDragPoint = null; |
| | | dragInProgress = false; |
| | | } |
| | | |
| | | public void mouseExited(MouseEvent e) { |
| | | // 鼠标离开面板时,清除鼠标位置显示 |
| | | if (manualBoundaryDrawer.isManualBoundaryDrawingMode()) { |
| | | manualBoundaryDrawer.clearMousePosition(); |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | public void mouseClicked(MouseEvent e) { |
| | | if (dragInProgress) { |
| | |
| | | if (!SwingUtilities.isLeftMouseButton(e) || e.getClickCount() != 1) { |
| | | return; |
| | | } |
| | | // 优先处理手动绘制边界模式点击 |
| | | if (manualBoundaryDrawer.isManualBoundaryDrawingMode()) { |
| | | Point2D.Double worldPoint = screenToWorld(e.getPoint()); |
| | | if (manualBoundaryDrawer.handleClick(worldPoint)) { |
| | | visualizationPanel.repaint(); |
| | | return; |
| | | } |
| | | } |
| | | // 优先处理测量模式点击 |
| | | if (measurementModeActive && handleMeasurementClick(e.getPoint())) { |
| | | return; |
| | | } |
| | | if (handleMowerClick(e.getPoint())) { |
| | | return; |
| | | } |
| | | // 优先处理障碍物边界点点击(如果可见) |
| | | if (obstaclePointsVisible && handleObstaclePointClick(e.getPoint())) { |
| | | return; |
| | | } |
| | | // 然后处理地块边界点点击 |
| | | if (boundaryPointsVisible) { |
| | | handleBoundaryPointClick(e.getPoint()); |
| | | } |
| | |
| | | } |
| | | |
| | | public void mouseMoved(MouseEvent e) { |
| | | // 更新鼠标位置用于显示坐标 |
| | | mousePoint = e.getPoint(); |
| | | visualizationPanel.repaint(); |
| | | // 在手动绘制边界模式时,更新鼠标位置 |
| | | if (manualBoundaryDrawer.isManualBoundaryDrawingMode()) { |
| | | Point2D.Double worldPoint = screenToWorld(e.getPoint()); |
| | | manualBoundaryDrawer.updateMousePosition(worldPoint); |
| | | visualizationPanel.repaint(); |
| | | } else { |
| | | manualBoundaryDrawer.clearMousePosition(); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | |
| | | private Timer createMowerTimer() { |
| | | Timer timer = new Timer(300, e -> { |
| | | mower.refreshFromDevice(); |
| | | updateIdleMowerTrail(); |
| | | if (realtimeTrackRecording) { |
| | | captureRealtimeTrackPoint(); |
| | | } |
| | |
| | | /** |
| | | * 基于鼠标位置的缩放 |
| | | */ |
| | | 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); |
| | | |
| | | |
| | | // 保存缩放比例到配置文件 |
| | | saveScaleToProperties(); |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 重置视图 |
| | | */ |
| | | public void resetView() { |
| | | scale = 1.0; |
| | | scale = DEFAULT_SCALE; |
| | | translateX = 0.0; |
| | | translateY = 0.0; |
| | | // 保存缩放比例到配置文件 |
| | | saveScaleToProperties(); |
| | | visualizationPanel.repaint(); |
| | | } |
| | | |
| | |
| | | boolean hasPlannedPath = currentPlannedPath != null && currentPlannedPath.size() >= 2; |
| | | boolean hasObstacles = currentObstacles != null && !currentObstacles.isEmpty(); |
| | | |
| | | // 绘制地块边界(底层) |
| | | if (hasBoundary) { |
| | | drawCurrentBoundary(g2d); |
| | | } |
| | | |
| | | if (hasObstacles) { |
| | | Obstacledraw.drawObstacles(g2d, currentObstacles, scale, selectedObstacleName); |
| | | } |
| | | |
| | | yulanzhangaiwu.renderPreview(g2d, scale); |
| | | |
| | | if (!circleSampleMarkers.isEmpty()) { |
| | |
| | | drawCircleCaptureOverlay(g2d, circleCaptureOverlay, scale); |
| | | } |
| | | |
| | | if (handheldBoundaryPreviewActive) { |
| | | drawHandheldBoundaryPreview(g2d); |
| | | } |
| | | adddikuaiyulan.drawPreview(g2d, handheldBoundaryPreview, scale, handheldBoundaryPreviewActive, boundaryPreviewMarkerScale); |
| | | |
| | | // 绘制手动绘制的边界 |
| | | manualBoundaryDrawer.drawBoundary(g2d, scale); |
| | | |
| | | // 绘制鼠标实时位置(手动绘制边界模式时) |
| | | manualBoundaryDrawer.drawMousePosition(g2d, scale); |
| | | |
| | | // 绘制导航路径(中层) |
| | | if (hasPlannedPath) { |
| | | drawCurrentPlannedPath(g2d); |
| | | } |
| | | |
| | | if (boundaryPointsVisible && hasBoundary) { |
| | | pointandnumber.drawBoundaryPoints( |
| | | g2d, |
| | | currentBoundary, |
| | | scale, |
| | | BOUNDARY_POINT_MERGE_THRESHOLD, |
| | | BOUNDARY_POINT_COLOR, |
| | | BOUNDARY_LABEL_COLOR |
| | | ); |
| | | // 绘制障碍物(顶层,显示在地块和导航路径上方) |
| | | if (hasObstacles) { |
| | | Obstacledraw.drawObstacles(g2d, currentObstacles, scale, selectedObstacleName); |
| | | } |
| | | |
| | | // 显示边界点(如果边界点可见,或者边界距离可见) |
| | | if ((boundaryPointsVisible || boundaryLengthVisible) && hasBoundary) { |
| | | // 预览模式下显示序号 |
| | | if (previewSizingEnabled) { |
| | | drawBoundaryPointsWithNumbers(g2d, currentBoundary, scale); |
| | | } else { |
| | | double markerScale = boundaryPointSizeScale; |
| | | pointandnumber.drawBoundaryPoints( |
| | | g2d, |
| | | currentBoundary, |
| | | scale, |
| | | BOUNDARY_POINT_MERGE_THRESHOLD, |
| | | BOUNDARY_POINT_COLOR, |
| | | markerScale |
| | | ); |
| | | } |
| | | } |
| | | |
| | | // 绘制障碍物坐标点(带序号) |
| | | if (obstaclePointsVisible && hasObstacles) { |
| | | drawObstaclePointsWithNumbers(g2d, currentObstacles, scale); |
| | | } |
| | | |
| | | if (shouldRenderIdleTrail()) { |
| | | tuowei.draw(g2d, idleMowerTrail, scale); |
| | | } |
| | | |
| | | if (!realtimeMowingTrack.isEmpty()) { |
| | | drawRealtimeMowingCoverage(g2d); |
| | | } |
| | | |
| | | // 绘制导航预览已割区域 |
| | | if (!navigationPreviewTrack.isEmpty()) { |
| | | drawNavigationPreviewCoverage(g2d); |
| | | } |
| | | |
| | | // 先画往返路径(线+点),保证割草机图标在其上方 |
| | | if (returnPathDrawer != null && returnPathDrawer.isActive()) { |
| | | returnPathDrawer.draw(g2d, scale); |
| | | } else if (previewReturnPath != null && !previewReturnPath.isEmpty()) { |
| | | // 绘制预览的往返路径(铁线路图风格) |
| | | WangfanDraw.drawRailwayPath(g2d, previewReturnPath, scale); |
| | | } else if (currentReturnPath != null && !currentReturnPath.isEmpty()) { |
| | | // 绘制保存的往返路径(铁线路图风格) |
| | | WangfanDraw.drawRailwayPath(g2d, currentReturnPath, scale); |
| | | } |
| | | |
| | | drawMower(g2d); |
| | | |
| | | // 绘制导航预览速度(如果正在导航预览) |
| | | if (navigationPreviewSpeed > 0 && mower != null && mower.hasValidPosition()) { |
| | | drawNavigationPreviewSpeed(g2d, scale); |
| | | } |
| | | |
| | | // 绘制测量模式(如果激活) |
| | | if (measurementModeActive) { |
| | | drawMeasurementMode(g2d, scale); |
| | | } |
| | | |
| | | // 保存当前变换(包含视图变换)用于坐标转换 |
| | | AffineTransform currentTransformForLength = g2d.getTransform(); |
| | | |
| | | // 恢复原始变换 |
| | | g2d.setTransform(originalTransform); |
| | | |
| | | // 绘制边界长度(如果启用)- 在恢复原始变换后绘制 |
| | | if (boundaryLengthVisible && hasBoundary) { |
| | | bianjie.BoundaryLengthDrawer.drawBoundaryLengths(g2d, currentBoundary, scale, |
| | | visualizationPanel.getWidth(), visualizationPanel.getHeight(), translateX, translateY); |
| | | } |
| | | |
| | | // 绘制视图信息 |
| | | drawViewInfo(g2d); |
| | | } |
| | |
| | | private void drawCoordinateSystem(Graphics2D g2d) { |
| | | // 绘制原点 - 红色实心小圆圈 |
| | | g2d.setColor(Color.RED); |
| | | g2d.fillOval(-1, -1, 2, 2); |
| | | g2d.fill(new Ellipse2D.Double(-0.5d, -0.5d, 1d, 1d)); |
| | | } |
| | | |
| | | |
| | |
| | | private void drawMower(Graphics2D g2d) { |
| | | mower.draw(g2d, scale); |
| | | } |
| | | |
| | | private void drawRealtimeMowingCoverage(Graphics2D g2d) { |
| | | if (realtimeMowingTrack.size() < 2) { |
| | | |
| | | /** |
| | | * 绘制导航预览速度(在割草机图标上方) |
| | | */ |
| | | private void drawNavigationPreviewSpeed(Graphics2D g2d, double scale) { |
| | | if (mower == null || !mower.hasValidPosition()) { |
| | | return; |
| | | } |
| | | |
| | | Path2D.Double path = new Path2D.Double(); |
| | | boolean started = false; |
| | | for (Point2D.Double point : realtimeMowingTrack) { |
| | | if (point == null || !Double.isFinite(point.x) || !Double.isFinite(point.y)) { |
| | | continue; |
| | | } |
| | | if (!started) { |
| | | path.moveTo(point.x, point.y); |
| | | started = true; |
| | | } else { |
| | | path.lineTo(point.x, point.y); |
| | | } |
| | | } |
| | | |
| | | if (!started) { |
| | | |
| | | Point2D.Double mowerPos = mower.getPosition(); |
| | | if (mowerPos == null) { |
| | | return; |
| | | } |
| | | |
| | | Stroke originalStroke = g2d.getStroke(); |
| | | Color originalColor = g2d.getColor(); |
| | | |
| | | double effectiveWidth = getEffectiveMowerWidthMeters(); |
| | | if (effectiveWidth > 0) { |
| | | float coverageWidth = (float) Math.max(effectiveWidth, 0.1d); |
| | | g2d.setStroke(new BasicStroke(coverageWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); |
| | | g2d.setColor(new Color(46, 139, 87, 80)); |
| | | g2d.draw(path); |
| | | } |
| | | |
| | | float spineWidth = (float) (effectiveWidth > 0 ? Math.max(effectiveWidth / 4.0, 0.18d) : 0.3d); |
| | | g2d.setStroke(new BasicStroke(spineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); |
| | | g2d.setColor(new Color(34, 139, 34, 200)); |
| | | g2d.draw(path); |
| | | |
| | | g2d.setStroke(originalStroke); |
| | | g2d.setColor(originalColor); |
| | | |
| | | // 将速度从米/秒转换为KM/h |
| | | double speedKmh = navigationPreviewSpeed * 3.6; |
| | | String speedText = String.format("%.1f km/h", speedKmh); |
| | | |
| | | // 保存原始变换 |
| | | AffineTransform originalTransform = g2d.getTransform(); |
| | | |
| | | // 将世界坐标转换为屏幕坐标 |
| | | Point2D.Double screenPos = worldToScreen(mowerPos); |
| | | |
| | | // 恢复原始变换以绘制文字(固定大小,不随缩放变化) |
| | | g2d.setTransform(new AffineTransform()); |
| | | |
| | | // 设置字体(与缩放文字大小一致,11号字体) |
| | | Font labelFont = new Font("微软雅黑", Font.PLAIN, 11); |
| | | g2d.setFont(labelFont); |
| | | FontMetrics metrics = g2d.getFontMetrics(labelFont); |
| | | |
| | | // 计算文字位置(在割草机图标上方) |
| | | int textWidth = metrics.stringWidth(speedText); |
| | | int textHeight = metrics.getHeight(); |
| | | int textX = (int)Math.round(screenPos.x - textWidth / 2.0); |
| | | // 在割草机图标上方,留出一定间距 |
| | | // 图标在世界坐标系中的大小约为 48 * 0.8 / scale 米 |
| | | // 转换为屏幕像素:图标高度(像素)= (48 * 0.8 / scale) * scale = 48 * 0.8 = 38.4 像素 |
| | | double iconSizePixels = 48.0 * 0.8; // 图标在屏幕上的大小(像素) |
| | | int spacing = 8; // 间距(像素) |
| | | int textY = (int)Math.round(screenPos.y - iconSizePixels / 2.0 - spacing - textHeight); |
| | | |
| | | // 绘制文字背景(半透明白色,增强可读性) |
| | | g2d.setColor(new Color(255, 255, 255, 200)); |
| | | g2d.fillRoundRect(textX - 4, textY - metrics.getAscent() - 2, textWidth + 8, textHeight + 4, 4, 4); |
| | | |
| | | // 绘制文字 |
| | | g2d.setColor(new Color(46, 139, 87)); // 使用主题绿色 |
| | | g2d.drawString(speedText, textX, textY); |
| | | |
| | | // 恢复变换 |
| | | g2d.setTransform(originalTransform); |
| | | } |
| | | |
| | | private void drawHandheldBoundaryPreview(Graphics2D g2d) { |
| | | if (!handheldBoundaryPreviewActive || handheldBoundaryPreview.isEmpty()) { |
| | | private void drawRealtimeMowingCoverage(Graphics2D g2d) { |
| | | if (realtimeMowingTrack == null || realtimeMowingTrack.size() < 2) { |
| | | return; |
| | | } |
| | | |
| | | Path2D.Double path = new Path2D.Double(); |
| | | boolean started = false; |
| | | for (Point2D.Double point : handheldBoundaryPreview) { |
| | | if (point == null || !Double.isFinite(point.x) || !Double.isFinite(point.y)) { |
| | | continue; |
| | | } |
| | | if (!started) { |
| | | path.moveTo(point.x, point.y); |
| | | started = true; |
| | | } else { |
| | | path.lineTo(point.x, point.y); |
| | | } |
| | | } |
| | | |
| | | if (!started) { |
| | | Path2D.Double boundaryPath = getRealtimeBoundaryPath(); |
| | | double effectiveWidth = getEffectiveMowerWidthMeters(); |
| | | gecaolunjing.draw(g2d, realtimeMowingTrack, effectiveWidth, boundaryPath); |
| | | } |
| | | |
| | | /** |
| | | * 绘制导航预览已割区域 |
| | | */ |
| | | private void drawNavigationPreviewCoverage(Graphics2D g2d) { |
| | | if (navigationPreviewTrack == null || navigationPreviewTrack.size() < 2) { |
| | | return; |
| | | } |
| | | |
| | | Stroke originalStroke = g2d.getStroke(); |
| | | Color originalColor = g2d.getColor(); |
| | | |
| | | if (handheldBoundaryPreview.size() >= 3) { |
| | | path.closePath(); |
| | | g2d.setColor(HANDHELD_BOUNDARY_FILL); |
| | | g2d.fill(path); |
| | | |
| | | Path2D.Double boundaryPath = currentBoundaryPath; |
| | | // 获取导航预览的割草宽度(从daohangyulan获取) |
| | | double previewWidth = getNavigationPreviewWidth(); |
| | | if (previewWidth <= 0) { |
| | | previewWidth = 0.5; // 默认50厘米 |
| | | } |
| | | |
| | | float outlineWidth = (float) Math.max(1.5f / scale, 0.2f); |
| | | g2d.setStroke(new BasicStroke(outlineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); |
| | | g2d.setColor(HANDHELD_BOUNDARY_BORDER); |
| | | g2d.draw(path); |
| | | |
| | | double markerSize = Math.max(0.5d, 3.0d / scale); |
| | | double markerRadius = markerSize / 2.0d; |
| | | Font originalFont = g2d.getFont(); |
| | | float labelFontSize = (float) Math.max(6.0f, 16.0f / Math.max(scale, 0.2)); |
| | | Font labelFont = originalFont.deriveFont(Font.BOLD, labelFontSize); |
| | | FontMetrics metrics = g2d.getFontMetrics(labelFont); |
| | | |
| | | for (Point2D.Double point : handheldBoundaryPreview) { |
| | | if (point == null || !Double.isFinite(point.x) || !Double.isFinite(point.y)) { |
| | | continue; |
| | | gecaolunjing.draw(g2d, navigationPreviewTrack, previewWidth, boundaryPath); |
| | | } |
| | | |
| | | /** |
| | | * 设置导航预览轨迹 |
| | | */ |
| | | public void setNavigationPreviewTrack(List<Point2D.Double> track) { |
| | | if (track == null) { |
| | | navigationPreviewTrack.clear(); |
| | | } else { |
| | | navigationPreviewTrack.clear(); |
| | | navigationPreviewTrack.addAll(track); |
| | | } |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 添加导航预览轨迹点 |
| | | */ |
| | | public void addNavigationPreviewTrackPoint(Point2D.Double point) { |
| | | if (point != null && Double.isFinite(point.x) && Double.isFinite(point.y)) { |
| | | navigationPreviewTrack.add(new Point2D.Double(point.x, point.y)); |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | Shape marker = new Ellipse2D.Double(point.x - markerRadius, point.y - markerRadius, markerSize, markerSize); |
| | | g2d.setColor(HANDHELD_BOUNDARY_POINT); |
| | | g2d.fill(marker); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 清除导航预览轨迹 |
| | | */ |
| | | public void clearNavigationPreviewTrack() { |
| | | navigationPreviewTrack.clear(); |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | private double navigationPreviewWidth = 0.5; // 导航预览的割草宽度(米) |
| | | private double navigationPreviewSpeed = 0.0; // 导航预览的割草机速度(米/秒) |
| | | |
| | | /** |
| | | * 设置导航预览的割草宽度 |
| | | */ |
| | | public void setNavigationPreviewWidth(double widthMeters) { |
| | | navigationPreviewWidth = widthMeters > 0 ? widthMeters : 0.5; |
| | | } |
| | | |
| | | /** |
| | | * 获取导航预览的割草宽度 |
| | | */ |
| | | private double getNavigationPreviewWidth() { |
| | | return navigationPreviewWidth; |
| | | } |
| | | |
| | | /** |
| | | * 设置导航预览的割草机速度(米/秒) |
| | | */ |
| | | public void setNavigationPreviewSpeed(double speedMetersPerSecond) { |
| | | navigationPreviewSpeed = speedMetersPerSecond >= 0 ? speedMetersPerSecond : 0.0; |
| | | } |
| | | |
| | | /** |
| | | * 获取导航预览的割草机速度(米/秒) |
| | | */ |
| | | private double getNavigationPreviewSpeed() { |
| | | return navigationPreviewSpeed; |
| | | } |
| | | |
| | | private Path2D.Double getRealtimeBoundaryPath() { |
| | | if (realtimeTrackLandNumber == null) { |
| | | return null; |
| | | } |
| | | |
| | | g2d.setFont(labelFont); |
| | | g2d.setColor(HANDHELD_BOUNDARY_LABEL); |
| | | int index = 1; |
| | | for (Point2D.Double point : handheldBoundaryPreview) { |
| | | if (point == null || !Double.isFinite(point.x) || !Double.isFinite(point.y)) { |
| | | index++; |
| | | continue; |
| | | if (currentBoundaryLandNumber != null && realtimeTrackLandNumber.equals(currentBoundaryLandNumber)) { |
| | | if (currentBoundaryPath == null) { |
| | | currentBoundaryPath = buildBoundaryPath(currentBoundary); |
| | | } |
| | | String label = String.valueOf(index++); |
| | | int textWidth = metrics.stringWidth(label); |
| | | int ascent = metrics.getAscent(); |
| | | int descent = metrics.getDescent(); |
| | | float textX = (float) (point.x - textWidth / 2.0); |
| | | float textY = (float) (point.y + (ascent - descent) / 2.0); |
| | | g2d.drawString(label, textX, textY); |
| | | return currentBoundaryPath; |
| | | } |
| | | |
| | | g2d.setStroke(originalStroke); |
| | | g2d.setFont(originalFont); |
| | | g2d.setColor(originalColor); |
| | | if (realtimeBoundaryPathCache != null && realtimeTrackLandNumber.equals(realtimeBoundaryPathLand)) { |
| | | return realtimeBoundaryPathCache; |
| | | } |
| | | |
| | | Dikuai dikuai = Dikuai.getDikuai(realtimeTrackLandNumber); |
| | | if (dikuai == null) { |
| | | realtimeBoundaryPathCache = null; |
| | | realtimeBoundaryPathLand = null; |
| | | return null; |
| | | } |
| | | |
| | | String normalized = normalizeValue(dikuai.getBoundaryCoordinates()); |
| | | if (normalized == null) { |
| | | realtimeBoundaryPathCache = null; |
| | | realtimeBoundaryPathLand = null; |
| | | return null; |
| | | } |
| | | |
| | | List<Point2D.Double> parsed = parseBoundary(normalized); |
| | | if (parsed.size() < 3) { |
| | | realtimeBoundaryPathCache = null; |
| | | realtimeBoundaryPathLand = null; |
| | | return null; |
| | | } |
| | | |
| | | realtimeBoundaryPathCache = buildBoundaryPath(parsed); |
| | | realtimeBoundaryPathLand = realtimeTrackLandNumber; |
| | | return realtimeBoundaryPathCache; |
| | | } |
| | | |
| | | private boolean shouldRenderIdleTrail() { |
| | | return !idleTrailSuppressed |
| | | && !realtimeTrackRecording |
| | | && !handheldBoundaryPreviewActive |
| | | && idleMowerTrail.size() >= 2; |
| | | } |
| | | |
| | | private void captureRealtimeTrackPoint() { |
| | | if (!realtimeTrackRecording) { |
| | | return; |
| | | } |
| | | if (realtimeTrackLandNumber == null || visualizationPanel == null) { |
| | | pendingTrackBreak = true; |
| | | return; |
| | | } |
| | | Device device = Device.getGecaoji(); |
| | | if (device == null) { |
| | | pendingTrackBreak = true; |
| | | return; |
| | | } |
| | | |
| | | String fixQuality = device.getPositioningStatus(); |
| | | if (!isHighPrecisionFix(fixQuality)) { |
| | | pendingTrackBreak = true; |
| | | return; |
| | | } |
| | | Point2D.Double position = mower.getPosition(); |
| | | if (position == null || !Double.isFinite(position.x) || !Double.isFinite(position.y)) { |
| | | pendingTrackBreak = true; |
| | | return; |
| | | } |
| | | |
| | | if (!isPointInsideActiveBoundary(position)) { |
| | | pendingTrackBreak = true; |
| | | return; |
| | | } |
| | | |
| | | Point2D.Double candidate = new Point2D.Double(position.x, position.y); |
| | | Point2D.Double lastPoint = realtimeMowingTrack.isEmpty() ? null : realtimeMowingTrack.get(realtimeMowingTrack.size() - 1); |
| | | double distance = 0.0; |
| | | double distance = Double.NaN; |
| | | if (lastPoint != null) { |
| | | double dx = position.x - lastPoint.x; |
| | | double dy = position.y - lastPoint.y; |
| | | double dx = candidate.x - lastPoint.x; |
| | | double dy = candidate.y - lastPoint.y; |
| | | distance = Math.hypot(dx, dy); |
| | | if (distance <= TRACK_DUPLICATE_TOLERANCE_METERS) { |
| | | return; |
| | | } |
| | | if (distance < TRACK_SAMPLE_MIN_DISTANCE_METERS) { |
| | | return; |
| | | } |
| | | } |
| | | |
| | | realtimeMowingTrack.add(new Point2D.Double(position.x, position.y)); |
| | | if (distance > 0.0) { |
| | | realtimeMowingTrack.add(candidate); |
| | | if (!pendingTrackBreak && lastPoint != null && Double.isFinite(distance)) { |
| | | trackLengthMeters += distance; |
| | | } |
| | | |
| | | updateCompletionMetrics(); |
| | | trackDirty = true; |
| | | maybePersistRealtimeTrack(false); |
| | | pendingTrackBreak = false; |
| | | } |
| | | |
| | | private void updateIdleMowerTrail() { |
| | | long now = System.currentTimeMillis(); |
| | | pruneIdleMowerTrail(now); |
| | | |
| | | if (idleTrailSuppressed || realtimeTrackRecording) { |
| | | if (!idleMowerTrail.isEmpty()) { |
| | | clearIdleMowerTrail(); |
| | | } |
| | | return; |
| | | } |
| | | |
| | | Device device = Device.getGecaoji(); |
| | | if (device == null) { |
| | | return; |
| | | } |
| | | // 使用更宽松的定位状态判断,允许状态1和4显示拖尾 |
| | | if (!isValidFixForTrail(device.getPositioningStatus())) { |
| | | return; |
| | | } |
| | | |
| | | Point2D.Double position = mower.getPosition(); |
| | | if (position == null || !Double.isFinite(position.x) || !Double.isFinite(position.y)) { |
| | | return; |
| | | } |
| | | |
| | | tuowei.TrailSample lastSample = idleMowerTrail.peekLast(); |
| | | if (lastSample != null) { |
| | | Point2D.Double lastPoint = lastSample.getPoint(); |
| | | double dx = position.x - lastPoint.x; |
| | | double dy = position.y - lastPoint.y; |
| | | if (Math.hypot(dx, dy) < IDLE_TRAIL_SAMPLE_DISTANCE_METERS) { |
| | | return; |
| | | } |
| | | } |
| | | |
| | | idleMowerTrail.addLast(new tuowei.TrailSample(now, new Point2D.Double(position.x, position.y))); |
| | | pruneIdleMowerTrail(now); |
| | | } |
| | | |
| | | /** |
| | | * 强制更新拖尾(用于收到$GNGGA数据时立即更新) |
| | | * 这个方法会刷新mower位置并立即添加到拖尾 |
| | | */ |
| | | public void forceUpdateIdleMowerTrail() { |
| | | long now = System.currentTimeMillis(); |
| | | pruneIdleMowerTrail(now); |
| | | |
| | | if (idleTrailSuppressed || realtimeTrackRecording) { |
| | | if (!idleMowerTrail.isEmpty()) { |
| | | clearIdleMowerTrail(); |
| | | } |
| | | return; |
| | | } |
| | | |
| | | Device device = Device.getGecaoji(); |
| | | if (device == null) { |
| | | return; |
| | | } |
| | | // 使用更宽松的定位状态判断,允许状态1和4显示拖尾 |
| | | if (!isValidFixForTrail(device.getPositioningStatus())) { |
| | | return; |
| | | } |
| | | |
| | | // 刷新mower位置,使用最新的Device数据 |
| | | mower.refreshFromDevice(); |
| | | Point2D.Double position = mower.getPosition(); |
| | | if (position == null || !Double.isFinite(position.x) || !Double.isFinite(position.y)) { |
| | | return; |
| | | } |
| | | |
| | | tuowei.TrailSample lastSample = idleMowerTrail.peekLast(); |
| | | if (lastSample != null) { |
| | | Point2D.Double lastPoint = lastSample.getPoint(); |
| | | double dx = position.x - lastPoint.x; |
| | | double dy = position.y - lastPoint.y; |
| | | if (Math.hypot(dx, dy) < IDLE_TRAIL_SAMPLE_DISTANCE_METERS) { |
| | | return; |
| | | } |
| | | } |
| | | |
| | | idleMowerTrail.addLast(new tuowei.TrailSample(now, new Point2D.Double(position.x, position.y))); |
| | | pruneIdleMowerTrail(now); |
| | | |
| | | // 立即重绘,确保拖尾及时显示 |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | private void pruneIdleMowerTrail(long now) { |
| | | if (idleMowerTrail.isEmpty()) { |
| | | return; |
| | | } |
| | | long cutoff = now - idleTrailDurationMs; |
| | | boolean modified = false; |
| | | while (!idleMowerTrail.isEmpty() && idleMowerTrail.peekFirst().getTimestamp() < cutoff) { |
| | | idleMowerTrail.removeFirst(); |
| | | modified = true; |
| | | } |
| | | if (modified && visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | private void clearIdleMowerTrail() { |
| | | if (idleMowerTrail.isEmpty()) { |
| | | return; |
| | | } |
| | | idleMowerTrail.clear(); |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | private void updateCompletionMetrics() { |
| | |
| | | mowerEffectiveWidthMeters = defaultMowerWidthMeters; |
| | | } |
| | | |
| | | // 加载往返路径 |
| | | String returnPathStr = dikuai != null ? dikuai.getReturnPathCoordinates() : null; |
| | | if (returnPathStr != null && !returnPathStr.isEmpty() && !"-1".equals(returnPathStr)) { |
| | | currentReturnPath = lujingdraw.parsePlannedPath(returnPathStr); |
| | | } else { |
| | | currentReturnPath = null; |
| | | } |
| | | |
| | | loadRealtimeTrack(landNumber, dikuai != null ? dikuai.getMowingTrack() : null); |
| | | visualizationPanel.repaint(); |
| | | } |
| | |
| | | mowerEffectiveWidthMeters = defaultMowerWidthMeters; |
| | | } |
| | | |
| | | idleTrailSuppressed = true; |
| | | clearIdleMowerTrail(); |
| | | |
| | | realtimeTrackLandNumber = normalizedLand; |
| | | realtimeTrackRecording = true; |
| | | pendingTrackBreak = true; |
| | | captureRealtimeTrackPoint(); |
| | | } |
| | | |
| | | public void pauseRealtimeTrackRecording() { |
| | | realtimeTrackRecording = false; |
| | | pendingTrackBreak = true; |
| | | idleTrailSuppressed = false; |
| | | maybePersistRealtimeTrack(true); |
| | | } |
| | | |
| | | public void stopRealtimeTrackRecording() { |
| | | realtimeTrackRecording = false; |
| | | pendingTrackBreak = true; |
| | | idleTrailSuppressed = false; |
| | | maybePersistRealtimeTrack(true); |
| | | } |
| | | |
| | |
| | | completedMowingAreaSqMeters = 0.0; |
| | | mowingCompletionRatio = 0.0; |
| | | trackDirty = true; |
| | | pendingTrackBreak = true; |
| | | idleTrailSuppressed = false; |
| | | maybePersistRealtimeTrack(true); |
| | | visualizationPanel.repaint(); |
| | | } |
| | | |
| | | public void clearIdleTrail() { |
| | | clearIdleMowerTrail(); |
| | | } |
| | | |
| | | public void setIdleTrailDurationSeconds(int seconds) { |
| | | int sanitized = seconds; |
| | | if (sanitized < 5 || sanitized > 600) { |
| | | sanitized = DEFAULT_IDLE_TRAIL_DURATION_SECONDS; |
| | | } |
| | | idleTrailDurationMs = sanitized * 1_000L; |
| | | pruneIdleMowerTrail(System.currentTimeMillis()); |
| | | } |
| | | |
| | | public int getIdleTrailDurationSeconds() { |
| | | long seconds = idleTrailDurationMs / 1_000L; |
| | | if (seconds <= 0L) { |
| | | return DEFAULT_IDLE_TRAIL_DURATION_SECONDS; |
| | | } |
| | | if (seconds > Integer.MAX_VALUE) { |
| | | return DEFAULT_IDLE_TRAIL_DURATION_SECONDS; |
| | | } |
| | | return (int) seconds; |
| | | } |
| | | |
| | | public double getMowingCompletionRatio() { |
| | | if (!isMowerInsideSelectedBoundary()) { |
| | | return 0.0; |
| | | } |
| | | return mowingCompletionRatio; |
| | | } |
| | | |
| | | public double getCompletedMowingAreaSqMeters() { |
| | | if (!isMowerInsideSelectedBoundary()) { |
| | | return 0.0; |
| | | } |
| | | return completedMowingAreaSqMeters; |
| | | } |
| | | |
| | |
| | | return trackLengthMeters; |
| | | } |
| | | |
| | | private boolean isMowerInsideSelectedBoundary() { |
| | | Point2D.Double position = mower.getPosition(); |
| | | if (position == null) { |
| | | return false; |
| | | } |
| | | return isPointInsideActiveBoundary(position); |
| | | } |
| | | |
| | | public void flushRealtimeTrack() { |
| | | maybePersistRealtimeTrack(true); |
| | | } |
| | |
| | | mowingCompletionRatio = 0.0; |
| | | trackDirty = false; |
| | | lastTrackPersistTimeMillis = 0L; |
| | | pendingTrackBreak = true; |
| | | realtimeBoundaryPathCache = null; |
| | | realtimeBoundaryPathLand = null; |
| | | |
| | | String trimmed = normalizeValue(trackData); |
| | | if (trimmed == null || trimmed.isEmpty()) { |
| | |
| | | } |
| | | |
| | | String[] segments = trimmed.split(";"); |
| | | Path2D.Double boundaryPath = getRealtimeBoundaryPath(); |
| | | Point2D.Double lastPoint = null; |
| | | for (String segment : segments) { |
| | | if (segment == null || segment.trim().isEmpty()) { |
| | |
| | | try { |
| | | double x = Double.parseDouble(parts[0].trim()); |
| | | double y = Double.parseDouble(parts[1].trim()); |
| | | Point2D.Double current = new Point2D.Double(x, y); |
| | | realtimeMowingTrack.add(current); |
| | | if (lastPoint != null) { |
| | | trackLengthMeters += Math.hypot(current.x - lastPoint.x, current.y - lastPoint.y); |
| | | if (!Double.isFinite(x) || !Double.isFinite(y)) { |
| | | continue; |
| | | } |
| | | Point2D.Double current = new Point2D.Double(x, y); |
| | | if (boundaryPath != null && !isPointInsideBoundary(current, boundaryPath)) { |
| | | continue; |
| | | } |
| | | if (lastPoint != null) { |
| | | double dx = current.x - lastPoint.x; |
| | | double dy = current.y - lastPoint.y; |
| | | double distance = Math.hypot(dx, dy); |
| | | if (distance <= TRACK_DUPLICATE_TOLERANCE_METERS) { |
| | | continue; |
| | | } |
| | | if (distance < TRACK_SAMPLE_MIN_DISTANCE_METERS) { |
| | | continue; |
| | | } |
| | | trackLengthMeters += distance; |
| | | } |
| | | realtimeMowingTrack.add(current); |
| | | lastPoint = current; |
| | | } catch (NumberFormatException ignored) { |
| | | // 跳过异常条目 |
| | |
| | | double worldY = (screenPoint.y - visualizationPanel.getHeight() / 2.0) / scale - translateY; |
| | | return new Point2D.Double(worldX, worldY); |
| | | } |
| | | |
| | | /** |
| | | * 处理测量模式点击 |
| | | */ |
| | | private boolean handleMeasurementClick(Point screenPoint) { |
| | | if (!measurementModeActive) { |
| | | return false; |
| | | } |
| | | Point2D.Double worldPoint = screenToWorld(screenPoint); |
| | | celiangmoshi.addPoint(worldPoint); |
| | | visualizationPanel.repaint(); |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 设置手动绘制边界模式 |
| | | */ |
| | | public void setManualBoundaryDrawingMode(boolean active) { |
| | | manualBoundaryDrawer.setManualBoundaryDrawingMode(active); |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取手动绘制的边界点列表 |
| | | */ |
| | | public List<Point2D.Double> getManualBoundaryPoints() { |
| | | return manualBoundaryDrawer.getManualBoundaryPoints(); |
| | | } |
| | | |
| | | /** |
| | | * 清空手动绘制的边界点 |
| | | */ |
| | | public void clearManualBoundaryPoints() { |
| | | manualBoundaryDrawer.clearManualBoundaryPoints(); |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置测量模式 |
| | | */ |
| | | public void setMeasurementMode(boolean active) { |
| | | measurementModeActive = active; |
| | | if (!active) { |
| | | celiangmoshi.clear(); |
| | | } |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 触发地图重绘 |
| | | */ |
| | | public void repaint() { |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 绘制测量模式 |
| | | */ |
| | | private void drawMeasurementMode(Graphics2D g2d, double scale) { |
| | | List<Point2D.Double> points = celiangmoshi.getPoints(); |
| | | if (points.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | // 保存原始变换 |
| | | AffineTransform originalTransform = g2d.getTransform(); |
| | | |
| | | // 设置测量模式颜色 |
| | | Color lineColor = new Color(255, 0, 0, 200); // 红色半透明 |
| | | Color pointColor = new Color(255, 0, 0, 255); // 红色 |
| | | Color textColor = new Color(33, 37, 41, 220); // 深灰色文字 |
| | | |
| | | // 设置线宽和点的大小(确保点和线连接) |
| | | float lineWidth = (float)(2.0 / scale); |
| | | // 点的大小(在世界坐标系中,米),确保点足够大以覆盖线的端点 |
| | | double pointSizeInWorld = 0.15d; // 点的大小(米) |
| | | double halfSize = pointSizeInWorld / 2.0; |
| | | |
| | | // 先绘制所有测量点(作为基础层) |
| | | g2d.setColor(pointColor); |
| | | for (Point2D.Double point : points) { |
| | | // 点的中心在 point.x, point.y |
| | | Ellipse2D.Double pointShape = new Ellipse2D.Double( |
| | | point.x - halfSize, |
| | | point.y - halfSize, |
| | | pointSizeInWorld, |
| | | pointSizeInWorld |
| | | ); |
| | | g2d.fill(pointShape); |
| | | } |
| | | |
| | | // 然后绘制连线,确保线从点的中心开始和结束,点和线连接在一起 |
| | | g2d.setColor(lineColor); |
| | | g2d.setStroke(new BasicStroke(lineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); |
| | | |
| | | // 绘制连线和距离文字(显示两个相邻点连线的长度) |
| | | for (int i = 0; i < points.size() - 1; i++) { |
| | | Point2D.Double p1 = points.get(i); |
| | | Point2D.Double p2 = points.get(i + 1); |
| | | |
| | | // 绘制连线(从第一个点的中心到第二个点的中心) |
| | | // 使用 Path2D 确保精确绘制,保持浮点精度 |
| | | Path2D.Double linePath = new Path2D.Double(); |
| | | linePath.moveTo(p1.x, p1.y); |
| | | linePath.lineTo(p2.x, p2.y); |
| | | g2d.draw(linePath); |
| | | |
| | | // 计算距离(两个相邻点连线的长度) |
| | | double distance = celiangmoshi.calculateDistance(p1, p2); |
| | | String distanceText = celiangmoshi.formatDistance(distance); |
| | | |
| | | // 计算中点位置(用于显示文字) |
| | | double midX = (p1.x + p2.x) / 2.0; |
| | | double midY = (p1.y + p2.y) / 2.0; |
| | | |
| | | // 将世界坐标转换为屏幕坐标(用于文字显示) |
| | | Point2D.Double worldMid = new Point2D.Double(midX, midY); |
| | | Point2D.Double screenMid = worldToScreen(worldMid); |
| | | |
| | | // 恢复原始变换以绘制文字(固定大小,不随缩放变化) |
| | | g2d.setTransform(new AffineTransform()); |
| | | |
| | | // 设置字体(与缩放文字大小一致,11号字体) |
| | | Font labelFont = new Font("微软雅黑", Font.PLAIN, 11); |
| | | g2d.setFont(labelFont); |
| | | FontMetrics metrics = g2d.getFontMetrics(labelFont); |
| | | |
| | | // 计算文字位置(居中显示) |
| | | int textWidth = metrics.stringWidth(distanceText); |
| | | int textHeight = metrics.getHeight(); |
| | | int textX = (int)Math.round(screenMid.x - textWidth / 2.0); |
| | | int textY = (int)Math.round(screenMid.y - textHeight / 2.0) + metrics.getAscent(); |
| | | |
| | | // 绘制文字背景(可选,用于提高可读性) |
| | | g2d.setColor(new Color(255, 255, 255, 200)); |
| | | g2d.fillRoundRect(textX - 2, textY - metrics.getAscent() - 2, textWidth + 4, textHeight + 4, 4, 4); |
| | | |
| | | // 绘制文字 |
| | | g2d.setColor(textColor); |
| | | g2d.drawString(distanceText, textX, textY); |
| | | |
| | | // 恢复变换 |
| | | g2d.setTransform(originalTransform); |
| | | } |
| | | |
| | | // 最后再次绘制测量点(在连线之上,确保点覆盖在线的端点上,点和线连接在一起) |
| | | g2d.setColor(pointColor); |
| | | for (Point2D.Double point : points) { |
| | | // 点的中心在 point.x, point.y,正好是线的端点位置 |
| | | Ellipse2D.Double pointShape = new Ellipse2D.Double( |
| | | point.x - halfSize, |
| | | point.y - halfSize, |
| | | pointSizeInWorld, |
| | | pointSizeInWorld |
| | | ); |
| | | g2d.fill(pointShape); |
| | | } |
| | | } |
| | | |
| | | private void drawCurrentBoundary(Graphics2D g2d) { |
| | | bianjiedrwa.drawBoundary(g2d, currentBoundary, scale, GRASS_FILL_COLOR, GRASS_BORDER_COLOR); |
| | | } |
| | | |
| | | 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) { |
| | | if (markers == null || markers.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | // 保存原始变换 |
| | | AffineTransform originalTransform = g2d.getTransform(); |
| | | |
| | | Shape markerShape; |
| | | double half = CIRCLE_SAMPLE_SIZE / 2.0; |
| | | g2d.setColor(CIRCLE_SAMPLE_COLOR); |
| | | g2d.setStroke(new BasicStroke((float) (1.2f / scale), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); |
| | | Font originalFont = g2d.getFont(); |
| | | float baseSize = (float) Math.max(12f / scale, 9f); |
| | | float reducedSize = Math.max(baseSize / 3f, 3f); |
| | | Font labelFont = originalFont.deriveFont(reducedSize); |
| | | |
| | | // 设置字体(与缩放文字大小一致,11号字体,不随缩放变化) |
| | | Font labelFont = new Font("微软雅黑", Font.PLAIN, 11); |
| | | g2d.setFont(labelFont); |
| | | FontMetrics metrics = g2d.getFontMetrics(); |
| | | FontMetrics metrics = g2d.getFontMetrics(labelFont); |
| | | |
| | | for (double[] pt : markers) { |
| | | if (pt == null || pt.length < 2 || !Double.isFinite(pt[0]) || !Double.isFinite(pt[1])) { |
| | | continue; |
| | | } |
| | | double x = pt[0]; |
| | | double y = pt[1]; |
| | | |
| | | // 绘制点(在世界坐标系中,随缩放变化) |
| | | markerShape = new Ellipse2D.Double(x - half, y - half, CIRCLE_SAMPLE_SIZE, CIRCLE_SAMPLE_SIZE); |
| | | g2d.fill(markerShape); |
| | | |
| | | // 将世界坐标转换为屏幕坐标以绘制文字(不随缩放变化) |
| | | Point2D.Double worldPoint = new Point2D.Double(x, y); |
| | | Point2D.Double screenPoint = new Point2D.Double(); |
| | | originalTransform.transform(worldPoint, screenPoint); |
| | | |
| | | // 恢复原始变换以使用屏幕坐标绘制文字 |
| | | g2d.setTransform(new AffineTransform()); |
| | | |
| | | String label = String.format(Locale.US, "%.2f,%.2f", x, y); |
| | | int textWidth = metrics.stringWidth(label); |
| | | float textX = (float) (x - textWidth / 2.0); |
| | | float textY = (float) (y - half - 0.2d) - metrics.getDescent(); |
| | | int textHeight = metrics.getHeight(); |
| | | |
| | | // 在屏幕坐标系中绘制文字(不随缩放变化) |
| | | int textX = (int)(screenPoint.x - textWidth / 2.0); |
| | | int textY = (int)(screenPoint.y - half - 0.2d) - metrics.getDescent(); |
| | | g2d.setColor(new Color(33, 37, 41, 220)); |
| | | g2d.drawString(label, textX, textY); |
| | | |
| | | // 恢复原始变换 |
| | | g2d.setTransform(originalTransform); |
| | | g2d.setColor(CIRCLE_SAMPLE_COLOR); |
| | | } |
| | | g2d.setFont(originalFont); |
| | | } |
| | | |
| | | private void drawCircleCaptureOverlay(Graphics2D g2d, CircleCaptureOverlay overlay, double scale) { |
| | |
| | | } |
| | | } |
| | | |
| | | private void showMowerInfo() { |
| | | Device device = Device.getGecaoji(); |
| | | if (device == null) { |
| | | public void showMowerInfo() { |
| | | if (mowerInfoManager != null) { |
| | | mowerInfoManager.showMowerInfo(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 处理障碍物边界点点击 |
| | | * @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; |
| | | } |
| | | |
| | | ensureMowerInfoDialog(); |
| | | updateMowerInfoLabels(); |
| | | positionMowerDialog(); |
| | | if (!mowerInfoDialog.isVisible()) { |
| | | mowerInfoDialog.setVisible(true); |
| | | } else { |
| | | mowerInfoDialog.toFront(); |
| | | } |
| | | startMowerInfoTimer(); |
| | | } |
| | | |
| | | private void ensureMowerInfoDialog() { |
| | | if (mowerInfoDialog != null) { |
| | | return; |
| | | } |
| | | |
| | | Window owner = SwingUtilities.getWindowAncestor(visualizationPanel); |
| | | JDialog dialog = new JDialog(owner, "割草机信息", Dialog.ModalityType.MODELESS); |
| | | dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
| | | |
| | | JPanel content = new JPanel(new BorderLayout(0, 12)); |
| | | content.setBorder(BorderFactory.createEmptyBorder(16, 20, 16, 20)); |
| | | |
| | | JPanel grid = new JPanel(new GridLayout(0, 2, 12, 8)); |
| | | |
| | | grid.add(new JLabel("设备编号:")); |
| | | mowerNumberValueLabel = createValueLabel(); |
| | | grid.add(mowerNumberValueLabel); |
| | | |
| | | grid.add(new JLabel("实时X坐标:")); |
| | | realtimeXValueLabel = createValueLabel(); |
| | | grid.add(realtimeXValueLabel); |
| | | |
| | | grid.add(new JLabel("实时Y坐标:")); |
| | | realtimeYValueLabel = createValueLabel(); |
| | | grid.add(realtimeYValueLabel); |
| | | |
| | | grid.add(new JLabel("定位质量:")); |
| | | positioningStatusValueLabel = createValueLabel(); |
| | | grid.add(positioningStatusValueLabel); |
| | | |
| | | grid.add(new JLabel("卫星颗数:")); |
| | | satelliteCountValueLabel = createValueLabel(); |
| | | grid.add(satelliteCountValueLabel); |
| | | |
| | | grid.add(new JLabel("行驶速度:")); |
| | | realtimeSpeedValueLabel = createValueLabel(); |
| | | grid.add(realtimeSpeedValueLabel); |
| | | |
| | | grid.add(new JLabel("运动航向:")); |
| | | headingValueLabel = createValueLabel(); |
| | | grid.add(headingValueLabel); |
| | | |
| | | grid.add(new JLabel("更新时间:")); |
| | | updateTimeValueLabel = createValueLabel(); |
| | | grid.add(updateTimeValueLabel); |
| | | |
| | | content.add(grid, BorderLayout.CENTER); |
| | | |
| | | JButton closeButton = new JButton("关闭"); |
| | | closeButton.addActionListener(e -> dialog.dispose()); |
| | | JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
| | | buttonPanel.add(closeButton); |
| | | content.add(buttonPanel, BorderLayout.SOUTH); |
| | | |
| | | dialog.setContentPane(content); |
| | | dialog.pack(); |
| | | dialog.setResizable(false); |
| | | dialog.addWindowListener(new WindowAdapter() { |
| | | @Override |
| | | public void windowClosed(WindowEvent e) { |
| | | stopMowerInfoTimer(); |
| | | resetMowerInfoLabels(); |
| | | mowerInfoDialog = null; |
| | | |
| | | // 创建新的坐标列表(移除指定点) |
| | | 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()); |
| | | } |
| | | }); |
| | | |
| | | mowerInfoDialog = dialog; |
| | | } |
| | | |
| | | private void positionMowerDialog() { |
| | | if (mowerInfoDialog == null || visualizationPanel == null) { |
| | | return; |
| | | } |
| | | int panelWidth = visualizationPanel.getWidth(); |
| | | int panelHeight = visualizationPanel.getHeight(); |
| | | int dialogHeight = mowerInfoDialog.getHeight(); |
| | | if (dialogHeight <= 0) { |
| | | dialogHeight = mowerInfoDialog.getPreferredSize().height; |
| | | } |
| | | if (panelWidth > 0) { |
| | | mowerInfoDialog.setSize(panelWidth, dialogHeight); |
| | | } |
| | | |
| | | try { |
| | | Point panelOnScreen = visualizationPanel.getLocationOnScreen(); |
| | | int dialogWidth = mowerInfoDialog.getWidth(); |
| | | int targetX = panelOnScreen.x + (panelWidth - dialogWidth) / 2; |
| | | int targetY = panelOnScreen.y + panelHeight / 3 - dialogHeight / 2; |
| | | mowerInfoDialog.setLocation(targetX, targetY); |
| | | } catch (IllegalComponentStateException ex) { |
| | | // component not yet showing; ignore positioning |
| | | } |
| | | } |
| | | |
| | | private JLabel createValueLabel() { |
| | | JLabel label = new JLabel("--"); |
| | | label.setHorizontalAlignment(SwingConstants.LEFT); |
| | | return label; |
| | | } |
| | | |
| | | private void startMowerInfoTimer() { |
| | | if (mowerInfoRefreshTimer == null) { |
| | | mowerInfoRefreshTimer = new Timer(1000, e -> updateMowerInfoLabels()); |
| | | mowerInfoRefreshTimer.setRepeats(true); |
| | | } |
| | | if (!mowerInfoRefreshTimer.isRunning()) { |
| | | mowerInfoRefreshTimer.start(); |
| | | } |
| | | } |
| | | |
| | | private void stopMowerInfoTimer() { |
| | | if (mowerInfoRefreshTimer != null) { |
| | | mowerInfoRefreshTimer.stop(); |
| | | mowerInfoRefreshTimer = null; |
| | | } |
| | | } |
| | | |
| | | private void resetMowerInfoLabels() { |
| | | mowerNumberValueLabel = null; |
| | | realtimeXValueLabel = null; |
| | | realtimeYValueLabel = null; |
| | | positioningStatusValueLabel = null; |
| | | satelliteCountValueLabel = null; |
| | | realtimeSpeedValueLabel = null; |
| | | headingValueLabel = null; |
| | | updateTimeValueLabel = null; |
| | | } |
| | | |
| | | private void updateMowerInfoLabels() { |
| | | if (mowerNumberValueLabel == null) { |
| | | return; |
| | | } |
| | | |
| | | Device device = Device.getGecaoji(); |
| | | if (device == null) { |
| | | setMowerInfoLabels("--"); |
| | | return; |
| | | } |
| | | |
| | | mower.refreshFromDevice(); |
| | | |
| | | mowerNumberValueLabel.setText(formatDeviceValue(device.getMowerNumber())); |
| | | realtimeXValueLabel.setText(formatDeviceValue(device.getRealtimeX())); |
| | | realtimeYValueLabel.setText(formatDeviceValue(device.getRealtimeY())); |
| | | positioningStatusValueLabel.setText(formatDeviceValue(device.getPositioningStatus())); |
| | | satelliteCountValueLabel.setText(formatDeviceValue(device.getSatelliteCount())); |
| | | realtimeSpeedValueLabel.setText(formatDeviceValue(device.getRealtimeSpeed())); |
| | | headingValueLabel.setText(formatDeviceValue(device.getHeading())); |
| | | updateTimeValueLabel.setText(formatTimestamp(device.getGupdateTime())); |
| | | } |
| | | |
| | | private void setMowerInfoLabels(String value) { |
| | | if (mowerNumberValueLabel != null) mowerNumberValueLabel.setText(value); |
| | | if (realtimeXValueLabel != null) realtimeXValueLabel.setText(value); |
| | | if (realtimeYValueLabel != null) realtimeYValueLabel.setText(value); |
| | | if (positioningStatusValueLabel != null) positioningStatusValueLabel.setText(value); |
| | | if (satelliteCountValueLabel != null) satelliteCountValueLabel.setText(value); |
| | | if (realtimeSpeedValueLabel != null) realtimeSpeedValueLabel.setText(value); |
| | | if (headingValueLabel != null) headingValueLabel.setText(value); |
| | | if (updateTimeValueLabel != null) updateTimeValueLabel.setText(value); |
| | | } |
| | | |
| | | private String sanitizeDeviceValue(String value) { |
| | | if (value == null) { |
| | | return null; |
| | | } |
| | | String trimmed = value.trim(); |
| | | if (trimmed.isEmpty() || "-1".equals(trimmed)) { |
| | | return null; |
| | | } |
| | | return trimmed; |
| | | } |
| | | |
| | | private String formatDeviceValue(String value) { |
| | | String sanitized = sanitizeDeviceValue(value); |
| | | return sanitized == null ? "--" : sanitized; |
| | | } |
| | | |
| | | private String formatTimestamp(String value) { |
| | | String sanitized = sanitizeDeviceValue(value); |
| | | if (sanitized == null) { |
| | | return "--"; |
| | | } |
| | | try { |
| | | long millis = Long.parseLong(sanitized); |
| | | return TIMESTAMP_FORMAT.format(new Date(millis)); |
| | | } catch (NumberFormatException ex) { |
| | | return sanitized; |
| | | |
| | | 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 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); |
| | | } |
| | |
| | | |
| | | if (updated.size() < 2) { |
| | | currentBoundary = null; |
| | | currentBoundaryPath = null; |
| | | boundaryBounds = null; |
| | | boundaryPointsVisible = false; |
| | | Dikuaiguanli.updateBoundaryPointVisibility(currentBoundaryLandNumber, false); |
| | |
| | | adjustViewAfterBoundaryReset(); |
| | | } else { |
| | | currentBoundary = updated; |
| | | rebuildBoundaryPath(); |
| | | boundaryBounds = computeBounds(updated); |
| | | Dikuaiguanli.updateBoundaryPointVisibility(currentBoundaryLandNumber, boundaryPointsVisible); |
| | | visualizationPanel.repaint(); |
| | | } |
| | | pendingTrackBreak = true; |
| | | } |
| | | |
| | | private boolean persistBoundaryChanges(List<Point2D.Double> updatedBoundary) { |
| | |
| | | return Math.hypot(dx, dy) <= BOUNDARY_POINT_MERGE_THRESHOLD; |
| | | } |
| | | |
| | | private boolean isHighPrecisionFix(String fixQuality) { |
| | | if (fixQuality == null) { |
| | | return false; |
| | | } |
| | | String trimmed = fixQuality.trim(); |
| | | if (trimmed.isEmpty()) { |
| | | return false; |
| | | } |
| | | if ("4".equals(trimmed)) { |
| | | return true; |
| | | } |
| | | try { |
| | | double value = Double.parseDouble(trimmed); |
| | | return Math.abs(value - 4.0d) < 1e-6; |
| | | } catch (NumberFormatException ex) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 判断定位状态是否有效,可用于显示拖尾 |
| | | * 接受状态1(单点定位)、2(码差分)、3(无效PPS)、4(固定解)、5(浮点解) |
| | | */ |
| | | private boolean isValidFixForTrail(String fixQuality) { |
| | | if (fixQuality == null) { |
| | | return false; |
| | | } |
| | | String trimmed = fixQuality.trim(); |
| | | if (trimmed.isEmpty()) { |
| | | return false; |
| | | } |
| | | // 接受状态1,2,3,4,5(只要不是0或无效状态) |
| | | if ("1".equals(trimmed) || "2".equals(trimmed) || "3".equals(trimmed) || |
| | | "4".equals(trimmed) || "5".equals(trimmed)) { |
| | | return true; |
| | | } |
| | | try { |
| | | double value = Double.parseDouble(trimmed); |
| | | // 接受1.0, 2.0, 3.0, 4.0, 5.0(只要不是0) |
| | | return value >= 1.0 && value <= 5.0; |
| | | } catch (NumberFormatException ex) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | private boolean isPointInsideActiveBoundary(Point2D.Double point) { |
| | | if (point == null || !Double.isFinite(point.x) || !Double.isFinite(point.y)) { |
| | | return false; |
| | | } |
| | | if (realtimeTrackLandNumber == null) { |
| | | return false; |
| | | } |
| | | Path2D.Double path = getRealtimeBoundaryPath(); |
| | | return isPointInsideBoundary(point, path); |
| | | } |
| | | |
| | | private boolean isPointInsideBoundary(Point2D.Double point, Path2D.Double path) { |
| | | if (point == null || path == null || !Double.isFinite(point.x) || !Double.isFinite(point.y)) { |
| | | return false; |
| | | } |
| | | if (path.contains(point.x, point.y)) { |
| | | return true; |
| | | } |
| | | double size = BOUNDARY_CONTAINS_TOLERANCE * 2.0; |
| | | return path.intersects(point.x - BOUNDARY_CONTAINS_TOLERANCE, point.y - BOUNDARY_CONTAINS_TOLERANCE, size, size); |
| | | } |
| | | |
| | | private void rebuildBoundaryPath() { |
| | | currentBoundaryPath = buildBoundaryPath(currentBoundary); |
| | | } |
| | | |
| | | private Path2D.Double buildBoundaryPath(List<Point2D.Double> boundary) { |
| | | if (boundary == null || boundary.size() < 3) { |
| | | return null; |
| | | } |
| | | Path2D.Double path = new Path2D.Double(); |
| | | boolean started = false; |
| | | for (Point2D.Double point : boundary) { |
| | | if (point == null || !Double.isFinite(point.x) || !Double.isFinite(point.y)) { |
| | | continue; |
| | | } |
| | | if (!started) { |
| | | path.moveTo(point.x, point.y); |
| | | started = true; |
| | | } else { |
| | | path.lineTo(point.x, point.y); |
| | | } |
| | | } |
| | | if (!started) { |
| | | return null; |
| | | } |
| | | path.closePath(); |
| | | return path; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 绘制视图信息 |
| | | */ |
| | | 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); |
| | | /** |
| | | * 绘制障碍物坐标点(带序号) |
| | | * 序号显示在点中心,字体大小与障碍物名称一致(11号),不随缩放变化 |
| | | */ |
| | | private void drawObstaclePointsWithNumbers(Graphics2D g2d, List<Obstacledge.Obstacle> obstacles, double scale) { |
| | | if (obstacles == null || obstacles.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | // 去掉操作提示:"滚轮缩放 | 拖拽移动 | 右键重置" |
| | | // String tips = "滚轮缩放 | 拖拽移动 | 右键重置"; |
| | | // g2d.drawString(tips, visualizationPanel.getWidth() - 200, 30); |
| | | // 保存原始变换 |
| | | 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号),不随缩放变化 |
| | | */ |
| | | public double getScale() { |
| | | return scale; |
| | | 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; |
| | | } |
| | | |
| | | // 设置点的大小(边界线宽度的2倍) |
| | | // 边界线宽度:3 / Math.max(0.5, scale) |
| | | double scaleFactor = Math.max(0.5, scale); |
| | | double boundaryLineWidth = 3.0 / scaleFactor; // 边界线宽度 |
| | | double markerDiameter = boundaryLineWidth * 2.0; // 边界点直径 = 边界线宽度的2倍 |
| | | 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)); |
| | | |
| | | // 在地图顶部左侧显示遥控摇杆对应速度(若非零) |
| | | 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 info = String.format("缩放: %.2fx", scale); |
| | | g2d.setColor(new Color(80, 80, 80)); |
| | | g2d.drawString(info, 15, visualizationPanel.getHeight() - 15); |
| | | } |
| | | |
| | | /** |
| | |
| | | * 设置视图变换参数(用于程序化控制) |
| | | */ |
| | | public void setViewTransform(double scale, double translateX, double translateY) { |
| | | this.scale = scale; |
| | | // 限制缩放范围 |
| | | scale = Math.max(MIN_SCALE, Math.min(scale, MAX_SCALE)); |
| | | // 如果缩放比例改变了,保存到配置文件 |
| | | if (Math.abs(this.scale - scale) > SCALE_EPSILON) { |
| | | this.scale = scale; |
| | | saveScaleToProperties(); |
| | | } else { |
| | | this.scale = scale; |
| | | } |
| | | this.translateX = translateX; |
| | | this.translateY = translateY; |
| | | visualizationPanel.repaint(); |
| | |
| | | public void setCurrentBoundary(String boundaryCoordinates, String landNumber, String landName) { |
| | | this.boundaryName = landName; |
| | | this.currentBoundaryLandNumber = landNumber; |
| | | this.realtimeBoundaryPathCache = null; |
| | | this.realtimeBoundaryPathLand = null; |
| | | |
| | | if (boundaryCoordinates == null) { |
| | | clearBoundaryData(); |
| | |
| | | return; |
| | | } |
| | | |
| | | currentBoundary = parsed; |
| | | boundaryBounds = computeBounds(parsed); |
| | | currentBoundary = parsed; |
| | | rebuildBoundaryPath(); |
| | | pendingTrackBreak = true; |
| | | boundaryBounds = computeBounds(parsed); |
| | | |
| | | Rectangle2D.Double bounds = boundaryBounds; |
| | | SwingUtilities.invokeLater(() -> { |
| | |
| | | |
| | | private void clearBoundaryData() { |
| | | currentBoundary = null; |
| | | currentBoundaryPath = null; |
| | | boundaryBounds = null; |
| | | boundaryName = null; |
| | | boundaryPointsVisible = false; |
| | | currentBoundaryLandNumber = null; |
| | | pendingTrackBreak = true; |
| | | realtimeBoundaryPathCache = null; |
| | | realtimeBoundaryPathLand = null; |
| | | } |
| | | |
| | | public void setCurrentObstacles(String obstaclesData, String landNumber) { |
| | |
| | | obstacleBounds = null; |
| | | selectedObstacleName = null; |
| | | currentObstacleLandNumber = null; |
| | | obstaclePointsVisible = false; |
| | | } |
| | | |
| | | private List<Obstacledge.Obstacle> parseObstacles(String obstaclesData, String landNumber) { |
| | |
| | | return coords; |
| | | } |
| | | |
| | | // Remove wrapper characters like parentheses that are used when persisting payloads |
| | | sanitized = sanitized.replace("(", "").replace(")", ""); |
| | | |
| | | String[] pairs = sanitized.split(";"); |
| | | for (String pair : pairs) { |
| | | if (pair == null) { |
| | |
| | | if (trimmed.isEmpty()) { |
| | | continue; |
| | | } |
| | | trimmed = trimmed.replace("(", "").replace(")", ""); |
| | | if (trimmed.isEmpty()) { |
| | | continue; |
| | | } |
| | | String[] parts = trimmed.split(","); |
| | | if (parts.length < 2) { |
| | | continue; |
| | |
| | | visualizationPanel.repaint(); |
| | | } |
| | | |
| | | public void setObstaclePointsVisible(boolean visible) { |
| | | this.obstaclePointsVisible = visible; |
| | | visualizationPanel.repaint(); |
| | | } |
| | | |
| | | /** |
| | | * 设置是否显示边界距离 |
| | | */ |
| | | public void setBoundaryLengthVisible(boolean visible) { |
| | | boundaryLengthVisible = visible; |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取是否显示边界距离 |
| | | */ |
| | | public boolean isBoundaryLengthVisible() { |
| | | return boundaryLengthVisible; |
| | | } |
| | | |
| | | 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(); |
| | |
| | | public void clearHandheldBoundaryPreview() { |
| | | handheldBoundaryPreviewActive = false; |
| | | handheldBoundaryPreview.clear(); |
| | | boundaryPreviewMarkerScale = 1.0d; |
| | | visualizationPanel.repaint(); |
| | | } |
| | | |
| | |
| | | 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; |
| | |
| | | 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() { |
| | | mowerUpdateTimer.stop(); |
| | | stopMowerInfoTimer(); |
| | | if (mowerInfoDialog != null) { |
| | | mowerInfoDialog.dispose(); |
| | | mowerInfoDialog = null; |
| | | mowerInfoManager.dispose(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前边界点列表 |
| | | * @return 当前边界点列表,如果没有边界则返回null |
| | | */ |
| | | public List<Point2D.Double> getCurrentBoundary() { |
| | | return currentBoundary; |
| | | } |
| | | |
| | | /** |
| | | * 获取割草机实例 |
| | | * @return 割草机实例 |
| | | */ |
| | | public Gecaoji getMower() { |
| | | return mower; |
| | | } |
| | | |
| | | /** |
| | | * 设置往返路径绘制管理器 |
| | | */ |
| | | public void setReturnPathDrawer(WangfanDraw drawer) { |
| | | this.returnPathDrawer = drawer; |
| | | } |
| | | |
| | | /** |
| | | * 设置预览的往返路径 |
| | | */ |
| | | public void setPreviewReturnPath(List<Point2D.Double> path) { |
| | | this.previewReturnPath = path; |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | resetMowerInfoLabels(); |
| | | } |
| | | |
| | | /** |
| | | * 开始往返路径绘制 |
| | | */ |
| | | public void startReturnPathDrawing() { |
| | | if (returnPathDrawer != null) { |
| | | // 禁用拖尾效果(在往返路径绘制模式下不显示实时轨迹拖尾) |
| | | idleTrailSuppressed = true; |
| | | clearIdleMowerTrail(); |
| | | // 清空之前的路径点(通过 WangfanDraw 管理) |
| | | repaint(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 停止往返路径绘制 |
| | | */ |
| | | public void stopReturnPathDrawing() { |
| | | // 恢复拖尾效果 |
| | | idleTrailSuppressed = false; |
| | | repaint(); |
| | | } |
| | | |
| | | /** |
| | | * 添加往返路径点(已废弃,路径点由 WangfanDraw 直接管理) |
| | | */ |
| | | @Deprecated |
| | | public void addReturnPathPoint(double x, double y) { |
| | | // 路径点由 WangfanDraw 直接管理,这里只需要重绘 |
| | | repaint(); |
| | | } |
| | | |
| | | /** |
| | | * 获取往返路径点列表的快照 |
| | | */ |
| | | public List<Point2D.Double> getReturnPathPointsSnapshot() { |
| | | if (returnPathDrawer != null) { |
| | | return returnPathDrawer.getPointsSnapshot(); |
| | | } |
| | | return new ArrayList<>(); |
| | | } |
| | | |
| | | } |