代码之家  ›  专栏  ›  技术社区  ›  Chris Powell

如何在视图中显示胶子贴图?

  •  0
  • Chris Powell  · 技术社区  · 2 年前

    我正在尝试胶子贴图。我创建了下面的地图。fxml:

    <View fx:id="map" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.runtheworld.MapPresenter">
        <center>
            <Pane fx:id="pane" prefHeight="500.0" prefWidth="300.0" />
        </center>
    </View>
    

    控制器/演示者尝试显示地图:

    public class MapPresenter {
        @FXML Pane pane;
        MapView  mapView = new MapView();
        MapPoint mapPoint = new MapPoint(56.946,24.10589);  // Riga, Latvia
        
        public void initialize() {
            StackPane sp = new StackPane();
            sp.getChildren().add(mapView);
            pane.getChildren().add(sp);
            
            // This throws error: "WARNING: sorry, would be too small". Uncommented, it crashes with IndexOutOfBoundsException
            // mapView.setZoom(9);
        }
    }
    

    视图MapView如下所示:

    public class MapView {
        public View getView()
        {
            try {
                View view = FXMLLoader.load(MapView.class.getResource("map.fxml"));
                return view;
            } catch (IOException e) {
                System.out.println("IOException: " + e);
                return new View();
            }
        }
    }
    

    主要课程如下:

    public class Main extends Application {
        public static final String MAP_VIEW = "map";
        private final AppManager appManager = AppManager.initialize(this::postInit);
    
        @Override
        public void init() {
            appManager.addViewFactory(MAP_VIEW, () -> new MapView().getView());
        }
    
        @Override
        public void start(Stage stage) {
            appManager.start(stage);
        }
    
        private void postInit(Scene scene) {
            appManager.switchView(MAP_VIEW);
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    如果我没有在控制器/演示器中设置缩放,或者我将缩放设置为小于6(?)的值,我得到以下错误:“警告:对不起,太小了”,我只能放大,不能缩小。查看代码,此错误意味着(负)缩放计算小于LayoutBounds()。getHeight(),对于一个区域,它始终为0.0(MapView扩展了该区域)。

    如果我将“控制器/演示器”中的“缩放”设置为6到10左右的值,则需要一段时间才能显示胶子飞溅或空白屏幕,有时甚至需要一分钟或更长时间才能最终与堆栈崩溃,如下所示:

    Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 83
        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:266)
        at java.base/java.util.Objects.checkIndex(Objects.java:359)
        at java.base/java.util.ArrayList.get(ArrayList.java:427)
        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:116)
        at javafx.graphics/javafx.scene.Node.updateGeomBounds(Node.java:3813)
        at javafx.graphics/javafx.scene.Node.getGeomBounds(Node.java:3775)
        at javafx.graphics/javafx.scene.Node.getLocalBounds(Node.java:3723)
        at javafx.graphics/javafx.scene.Node.updateTxBounds(Node.java:3877)
        at javafx.graphics/javafx.scene.Node.getTransformedBounds(Node.java:3669)
        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.Scene$ScenePulseListener.pulse(Scene.java:2530)
        at javafx.graphics/com.sun.javafx.tk.Toolkit.lambda$runPulse$2(Toolkit.java:421)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at javafx.graphics/com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:420)
        at javafx.graphics/com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:450)
        at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:575)
        at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:555)
        at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:548)
        at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:353)
        at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
        at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.enterNestedEventLoopImpl(Native Method)
        at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._enterNestedEventLoop(GtkApplication.java:360)
        at javafx.graphics/com.sun.glass.ui.Application.enterNestedEventLoop(Application.java:515)
        at javafx.graphics/com.sun.glass.ui.EventLoop.enter(EventLoop.java:107)
        at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(QuantumToolkit.java:647)
        at javafx.graphics/javafx.application.Platform.enterNestedEventLoop(Platform.java:301)
        at com.gluonhq.charm.glisten.control.Dialog.lambda$installIntoMobileApplication$7(Dialog.java:605)
        at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
        at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
        at javafx.base/javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:104)
        at javafx.base/javafx.beans.property.ReadOnlyBooleanWrapper.fireValueChangedEvent(ReadOnlyBooleanWrapper.java:101)
        at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:111)
        at javafx.base/javafx.beans.property.BooleanPropertyBase$Listener.invalidated(BooleanPropertyBase.java:239)
        at javafx.base/com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:348)
        at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
        at javafx.base/javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:104)
        at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:111)
        at javafx.base/javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
        at javafx.base/javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:79)
        at com.gluonhq.charm.glisten.layout.Layer.show(Layer.java:289)
        at com.gluonhq.charm.glisten.control.Dialog.showAndWait(Dialog.java:556)
        at com.gluonhq.impl.charm.glisten.util.NagScreenPresenter.lambda$showDialog$1(NagScreenPresenter.java:77)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
        at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
        at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:290)
        at java.base/java.lang.Thread.run(Thread.java:833)
    

    每当我在屏幕上移动光标时都会重复,但长度值会增加。如果输入12或更高的缩放值,则不会加载任何屏幕。

    如何让地图在视图中正确显示?

    编辑: 下载Gluon maps代码后,我添加了几行调试代码,以确定可能发生的情况。在BaseMap中,zoom()方法中会进行一个“高度”计算,该计算会与父容器高度进行比较。如果此计算值大于父级高度,则会出现一个神秘且无用的错误:“警告抱歉,将太小”。

    父高度似乎是一个任意值(我看到1024.0、1098.0、1261.0…等等),高度计算通常超过这个看似任意的父高度值。

    诀窍是设置容器大小,例如:

    sp.setPrefSize(300.d,500.d);
    

    并恢复正常服务。

    0 回复  |  直到 2 年前