JavaFX - 图像,imageview和

时间:2017-07-19 16:01:31

标签: java image javafx fxml scenebuilder

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

当我按“重新开始游戏按钮”时出现错误

SerializableFunction<BoundedWindow, String>

我的董事会代码

> 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);
   }

3 个答案:

答案 0 :(得分:3)

来自Imagedocumentation

  

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,其中包含“已添加重复的子项”消息,因为您要多次向场景图添加blackSquare。您需要为网格中的每个方块创建一个新的ImageView(如果您愿意,可以在不同的Image中使用相同的ImageView。)

所以你应该这样做:

@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);
}

答案 1 :(得分:0)

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

计划结构: src目录:

- &GT; ShowImage.java

- &gt; 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);
    }
}

答案 2 :(得分:-2)

使用此代码获取图像

error: ISO C++ forbids casting to an array type 'va_list {aka __va_list_tag [1]}'