0

我曾尝试在桌面上的 JavaFX 应用程序中使用Gluon Maps 项目。经过一些拖动和滚动后,程序会引发异常并停止工作。StackTrace 如下:

java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 2
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:373)
at java.base/java.util.ArrayList.get(ArrayList.java:425)
at javafx.graphics/javafx.scene.Parent.updateCachedBounds(Parent.java:1704)
at javafx.graphics/javafx.scene.Parent.recomputeBounds(Parent.java:1648)
at javafx.graphics/javafx.scene.Parent.doComputeGeomBounds(Parent.java:1501)
at javafx.graphics/javafx.scene.Parent$1.doComputeGeomBounds(Parent.java:115)
at javafx.graphics/com.sun.javafx.scene.ParentHelper.computeGeomBoundsImpl(ParentHelper.java:84)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.computeGeomBounds(NodeHelper.java:115)
at javafx.graphics/javafx.scene.Node.updateGeomBounds(Node.java:3847)
at javafx.graphics/javafx.scene.Node.getGeomBounds(Node.java:3809)
at javafx.graphics/javafx.scene.Node.getLocalBounds(Node.java:3757)
at javafx.graphics/javafx.scene.Node.updateTxBounds(Node.java:3911)
at javafx.graphics/javafx.scene.Node.getTransformedBounds(Node.java:3703)
at javafx.graphics/javafx.scene.Node.updateBounds(Node.java:771)
at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1835)
at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
at javafx.graphics/javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2491)
at javafx.graphics/com.sun.javafx.tk.Toolkit.lambda$runPulse$2(Toolkit.java:413)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:412)
at javafx.graphics/com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:439)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:563)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:543)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:536)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:342)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:830)

调试程序后,我在 Gluon Maps Library(源代码)中的 com.gluonhq.impl.maps.BaseMap 类(在 GitHub 上)中找到了问题,我怀疑第 518 行中的方法(markDirty(){.. .}),但我不知道如何解决它。具体元素的代码是:

MainCenterPane.java

import com.gluonhq.maps.MapLayer;
import com.gluonhq.maps.MapPoint;
import com.gluonhq.maps.MapView;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Orientation;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.util.Pair;

public class MainCenterPane extends SplitPane {
    
    private final MapPoint mapPoint = new MapPoint(50D, 4D);

    public MainCenterPane() {
        this.setOrientation(Orientation.VERTICAL);
        
        this.setDividerPosition(0, 0.8);

        MapView map = new MapView();
        
        PoiLayer pl = new PoiLayer();
        pl.addPoint(mapPoint, new Circle(7, Color.RED));
        
        map.addLayer(pl);
        map.setZoom(3);
        
        map.flyTo(1, mapPoint, 2);
        
        BorderPane center = new BorderPane(map);
        
        this.getItems().add(center);
    }
    
    public class PoiLayer extends MapLayer {


        private final ObservableList<Pair<MapPoint, Node>> points = FXCollections.observableArrayList();
        
        public PoiLayer() {
        }

        public void addPoint(MapPoint p, Node icon) {
            points.add(new Pair<>(p, icon));
            this.getChildren().add(icon);
            this.markDirty();
        }

        @Override
        protected void layoutLayer() {
            if(!Platform.isFxApplicationThread()) {
                Platform.runLater(()->layoutLayer());
            }
            for (Pair<MapPoint, Node> candidate : points) {
                MapPoint point = candidate.getKey();
                Node icon = candidate.getValue();
                Point2D mapPoint = getMapPoint(point.getLatitude(), point.getLongitude());
                icon.setVisible(true);
                icon.setTranslateX(mapPoint.getX());
                icon.setTranslateY(mapPoint.getY());
            }
        }

    }

}

谢谢你的帮助!

4

0 回答 0