JavaFX - 图像、图像视图和
JavaFX - image, imageview an
我正在尝试创建名为 "Othello" 的游戏。我在 scenebuilder 中创建了一个按钮来重新启动游戏。
我有一个 gridPane [从 0 到 7][从 0 到 7](暗数组)和每个块,例如 myGridPane[i][j] 一个图像查看器,里面有一个图像,其中包含我的作品(黑色或白色) .我有一些错误..但我不明白我错了什么。帮帮我,谢谢。
当我按下 "RESTART GAME BUTTON"
时出错
> 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.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.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.access00(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$handleMouseEvent4(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(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)
使用此代码获取图像
File f = new File("images/black.png");
Image img = new Image(f.toURI().toString());
来自Image
classdocumentation:
All URLs supported by URL can be passed to the constructor. If the passed string is not a valid URL, but a path instead, the Image is searched on the classpath in that case.
所以你的图像路径 images/black.png
将相对于 class 路径进行搜索,显然 class 路径的根目录下没有 images
资源。你需要
new Image("othello/images/black.png")
等等
由于您的 Main
class 在 othello
包中,另一种选择是获取与之相关的资源,例如
new Image(Main.class.getResource("images/black.png"))
等等
请注意,您还有其他错误,这些错误尚未显示,因为代码在您到达之前就已失败。您不能将同一节点多次添加到场景图中,因此
myGrid.add(blackSquare, 3, 3);
myGrid.add(blackSquare, 4, 3);
将抛出带有消息 "duplicate child added" 的 IllegalStateException
,因为您要将 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);
}
您收到 java.lang.IllegalArgumentException:无效 URL。您没有提供有效的图像 URL。假设我想显示一个名为 boldedRow.png 的图像,而 boldedRow.png 恰好在视图文件夹中,而我的主要 java 文件在 src 目录中,那么我可以获得图像 url 方式如下
程序结构:
源目录:
-->ShowImage.java
-->查看/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);
}
}
我正在尝试创建名为 "Othello" 的游戏。我在 scenebuilder 中创建了一个按钮来重新启动游戏。 我有一个 gridPane [从 0 到 7][从 0 到 7](暗数组)和每个块,例如 myGridPane[i][j] 一个图像查看器,里面有一个图像,其中包含我的作品(黑色或白色) .我有一些错误..但我不明白我错了什么。帮帮我,谢谢。
当我按下 "RESTART GAME BUTTON"
时出错> 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.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.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.access00(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$handleMouseEvent4(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(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)
使用此代码获取图像
File f = new File("images/black.png");
Image img = new Image(f.toURI().toString());
来自Image
classdocumentation:
All URLs supported by URL can be passed to the constructor. If the passed string is not a valid URL, but a path instead, the Image is searched on the classpath in that case.
所以你的图像路径 images/black.png
将相对于 class 路径进行搜索,显然 class 路径的根目录下没有 images
资源。你需要
new Image("othello/images/black.png")
等等
由于您的 Main
class 在 othello
包中,另一种选择是获取与之相关的资源,例如
new Image(Main.class.getResource("images/black.png"))
等等
请注意,您还有其他错误,这些错误尚未显示,因为代码在您到达之前就已失败。您不能将同一节点多次添加到场景图中,因此
myGrid.add(blackSquare, 3, 3);
myGrid.add(blackSquare, 4, 3);
将抛出带有消息 "duplicate child added" 的 IllegalStateException
,因为您要将 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);
}
您收到 java.lang.IllegalArgumentException:无效 URL。您没有提供有效的图像 URL。假设我想显示一个名为 boldedRow.png 的图像,而 boldedRow.png 恰好在视图文件夹中,而我的主要 java 文件在 src 目录中,那么我可以获得图像 url 方式如下
程序结构: 源目录:
-->ShowImage.java
-->查看/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);
}
}