首页 文章

JavaFX - 图像,imageview和

提问于
浏览
0

我正在尝试制作名为“奥赛罗”的游戏 . 我在scenebuilder中创建了一个按钮来重启游戏 . 我有一个gridPane [从0到7] [从0到7](昏暗数组)和每个块,例如myGridPane [i] [j]一个图像查看器,其中包含我的作品(黑色或白色) . 我有一些错误 . 但我无法理解我的错误 . 帮帮我,谢谢 .

当我按“重启游戏按钮”时出错

> Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 52 more
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1118)
    at javafx.scene.image.Image.<init>(Image.java:620)
    at othello.controller.BoardController.restartGame(BoardController.java:72)
    ... 62 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1110)
    ... 64 more

我的董事会代码

@FXML
   private void restartGame(ActionEvent event)throws Exception{
      myGrid = new GridPane();
      black = new Image("images/black.png");
      white = new Image("images/white.png");
      empty = new Image("images/empty.png");
      ImageView square = new ImageView();
      square.setImage(empty);
      ImageView blackSquare = new ImageView();
      blackSquare.setImage(black);
      ImageView whiteSquare = new ImageView();
      whiteSquare.setImage(white);
      for (int i = 0; i < 8; i++){ //Per righe
         for (int j = 0; j < 8; j++){ // Per colonne
            myGrid.add(square, i, j);
         }

      }
      myGrid.add(blackSquare, 3, 3);
      myGrid.add(blackSquare, 4, 3);
      myGrid.add(whiteSquare, 4, 4);
      myGrid.add(whiteSquare, 4, 3);
   }

路径:

I have main project in src of netbeans.
Inside it, i have:
- othello (it contains my main)
- othello.images (it cointains all my image also backgrounds)
- othello.view (it contains my FXML files)
- othello.model (now nothing)
- othello.controller (it contains the controllers about the fxml files)

3 回答

  • 3

    来自 Image class documentation

    URL支持的所有URL都可以传递给构造函数 . 如果传递的字符串不是有效的URL,而是路径,则在该情况下在类路径上搜索Image .

    因此,将相对于类路径搜索您的图像路径 images/black.png ,并且显然在类路径的根处没有 images 资源 . 你需要

    new Image("othello/images/black.png")
    

    等等

    由于您的 Main 类位于 othello 包中,因此另一个选项是获取相对于该资源的资源,例如

    new Image(Main.class.getResource("images/black.png"))
    

    等等

    请注意,您还有其他错误,因为代码在到达之前失败了,因此未显示错误 . 您不能多次将同一节点添加到场景图中,所以

    myGrid.add(blackSquare, 3, 3);
      myGrid.add(blackSquare, 4, 3);
    

    将抛出 IllegalStateException 并显示消息"duplicate child added",因为您要多次向场景图添加 blackSquare . 您需要为网格中的每个方块创建一个新的 ImageView (如果您愿意,可以在不同的 ImageView 中使用相同的 Image ) .

    所以你应该这样做:

    @FXML
    private void restartGame(ActionEvent event)throws Exception{
      myGrid = new GridPane();
      black = new Image(Main.class.getResource("images/black.png"));
      white = new Image(Main.class.getResource("images/white.png"));
      empty = new Image(Main.class.getResource("images/empty.png"));
      for (int i = 0; i < 8; i++){ //Per righe
         for (int j = 0; j < 8; j++){ // Per colonne
            myGrid.add(square, i, j);
         }
    
      }
      myGrid.add(new ImageView(black), 3, 3);
      myGrid.add(new ImageView(black), 4, 3);
      myGrid.add(new ImageView(white), 4, 4);
      myGrid.add(new ImageView(white), 4, 3);
    }
    
  • -2

    你得到了 java.lang.IllegalArgumentException: Invalid URL . 您没有提供有效的图片网址 . 假设我想显示一个名为boldedRow.png的图像,而boldedRow.png恰好位于视图文件夹中,我的主java文件位于src目录中,然后我可以通过以下方式获取图像网址 .

    程序结构:src目录:

    • ShowImage.java

    • view / boldedRow.png

    public class ShowImage extends Application {
        @Override
        public void start(Stage primaryStage) throws Exception {
            Pane root = new Pane();
            ImageView image = new ImageView(new Image(getClass().getResourceAsStream("view/boldedRow.png")));
            root.getChildren().add(image);
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    
  • 0

    使用此代码获取图像

    File f = new File("images/black.png");
        Image img = new Image(f.toURI().toString());
    

相关问题