I have the following project setup (Maven Project):
project setup
And the absolute path:
C:\Users\jenny\IdeaProjects\gameapp\client\src\main\resources\images\game\black_market.png
This code throws a Nullpointerexception:
private void setUpImage(ImageView image){
Platform.runLater(()->{
var url = getClass().getResourceAsStream("images/game/black_market.png");
System.out.println(url.toString());
Image img = new Image(url);
image.setImage(img);
});
}
It's probably because the filepath is incorrect but I don't really know how to fix it on my own. I've tried for the last 2-3 hours researching similar problems on Stackoverflow.
The ImageView that gets passed on this function is a reference to an ImageView. My function should load the image and put it into the passed ImageView. To be honest, I don't actually know if that will work since Java is pass by value.
why setImage() doesn't change my ImgView in JavaFx?
This question fixed it. I've noticed that the #FXML annotations were missing. Adding them fixed the entire problem.
#FXML
private void setUpImage(ImageView image){
Platform.runLater(()->{
Image img = new Image("/images/game/black_market.png");
image.setImage(img);
});
}
Related
Here is the simple code for javafx in intellij:
Image image = new Image(url);
ImageView imageView = new ImageView(image);
movieBox.getChildren().add(imageView);
//checking
System.out.println(imageView.getImage().getUrl());
Textfield tx = new Textfield();
tx.setText("hi");
movieBox.getChildren().add(tx);
I copied the project over to intellij on my mac and everything works accept the images won't show. Here are the things I tried:
Checking that the URL is valid by printing out the imageview image url. It prints out a valid URL of an image i can open in my browswer
Adding a random textfield to the movieBox (a flowpane object) to make sure it works. And it does.
There shouldn't be any error in the code because it works in my windows intellij. It seems like a problem with the environment, but I haven't even got a clue how should i approach this problem. Any sort of advice will be nice, thanks!
Try downloading the image and using the path to the image on your disk, if it works then the problem will be with the url, otherwise its with the ImageView. If the problem is with the URL, post the one you are trying to use so the community can test it, and try another URL.
P.S. If you aren't manipulating the image you can just pass the path/url directly top the ImageView constructor.
public ImageView(String url)
Allocates a new ImageView object with image loaded from the specified URL.
The new ImageView(url) has the same effect as new ImageView(new Image(url)).
I use getResource() to load the image, but I get nullpointer exception. Also I tried to use css but I get nothing.
My code for label:
Image mine = new Image(getClass().getResourceAsStream("bomb3.png"));
ImageView im = new ImageView(mine);
Label label = new Label();
label.setGraphic(im);
And for button:
btn.setStyle("-fx-background-image: url('flag.png')");
This code is located in Painter class.
My file tree:
If you use getClass().getResourceAsStream("bomb3.png") java search in the packetof your class. So if your class is Game, your resource must be in the packet: com.name.minesweeperClasses
Image mine = new Image(getClass().getResourceAsStream("bomb3.png"));
here path of bomb3.png is incorrect
Please mention the relative path :-
./resources/boob3.png
I would like to put an image on a Label, and the codes are:
Label userPic = new label();
ImageView userImage = new ImageView(
new Image("test/123/headPortrait.png",50.0,50.0,false,false));
userPic.setGraphic(userImage);
But it continually throws java.lang.IllegalArgumentException: Invalid URL or resource not found.
And I've tried file://headPortrait.png, file://E:/eclipse/ClassSchedule/headPortrait.png(the absolute URL). This time the compiler did not throw any exception, but the image still won't show up and the application runs very slowly.
Before I tried to add an image to the label, all of my code had worked perfectly.
It's not good pratice to put an ImageView into a label. I would recommend to you that you add the ImageView to your scene directly.
To load an Image in the classpath the best way to do that would be using a ResourceStream,
this.getClass().getResourceAsStream("/assets/headPortrait.png")
as example. In this case your image must be in a folder called assets.
Your whole code could look like this:
ImageView imageView = new ImageView(new Image(this.getClass().getResourceAsStream("/assets/headPortrait.png"), 50, 50, false, true));
I would recommend to you to set the last boolean flag of the Image constructor to true, because it increases the quality of the image.
I cannot create an image in any of my JavaFX projects using the following kind of code:
final String url = "line.jpg";
Image image = new Image(Config.class.getResourceAsStream(url));
because there is always a null pointer exception pointing to the second line. Obviously, I have checked that the image file is in the correct directory. I have tried example programs, some directly copied from these boards, but these also fail for the same reason.
I suspect I lack a resource in Netbeans or JavaFX but I can't figure out what is missing.
The only workaround appears to be to include the image file in a css stylesheet and link it to the program by setting the gui components id like this:
Button homeButton = new Button();
homeButton.setId("homebutton");
In the stylesheet there is:
#homebutton {
-fx-background-image: url("images/homebtn.jpg");
-fx-pref-width: 30;
-fx-pref-height: 30;
}
Its not ideal being forced into this solution and Swing seems far better at dealing with image files. I assume a bug in JavaFX that always causes the following to fail:
Image image = new Image(Config.class.getResourceAsStream(url));
I made a custom Eclipse plugin that uses and displays multiple dialogs, and I want to know if I could set the top left icon image with the one I use in the plugin's icons folder. I want to get that icon and set it instead of the default one that Eclipse uses.
I'm overriding the configureShell() method to change the dialog title, and I also want to change the icon.
#Override
protected void configureShell(Shell parent){
super.configureShell(parent);
parent.setText("Choose variant...");
Image icon = new Image(parent.getDisplay(), "icons/best.gif"); - this method does not work as it cannot find the file
parent.setImage(icon);
}
I also tried using the getClass().getResource("best.gif") and having the image in the same package, still can't find the location I'm giving(FileNotFoundException), and also, the Image constructor does not accept URL objects.
#Override
protected void configureShell(Shell parent){
super.configureShell(parent);
parent.setText("Choose variant...");
Image icon = new Image(parent.getDisplay(), getClass().getResource("icons/best.gif"));
parent.setImage(icon);
}
Is there a way to use the icon that I already have in my eclipse plugin?
The main problem is getting the icon from the icons folder of the plugin and making it a Image object.
Thank you.
You can register the icon in your plugins activator class like this:
#Override
protected void initializeImageRegistry(final ImageRegistry reg) {
reg.put(IMAGE_PATH, imageDescriptorFromPlugin(PLUGIN_ID, IMAGE_PATH));
}
The image path is relative to your plugin, e.g. icons/icon.png.
You can access these images via the activator class as well:
final Image image = MyActivatorClass.getDefault().getImageRegistry().get(IMAGE_PATH);
myShell.setImage(image);
(Note that I used the image path as key in the image registry, you do not have to do it like this but it makes everything a little bit less complicated by just using the same static String.)
For an Eclipse plugin you use the FileLocator class to find resources in your plugin.
For an image use something like:
String path = "icons/best.gif";
URL url = FileLocator.find(bundle, new Path(path), null);
ImageDescriptor desc = ImageDescriptor.createFromURL(url);
Image image = desc.createImage();
Note: You must arrange for the image to be disposed when no longer needed.
If your activator extends AbstractUIPlugin you can also use the ImageRegistry available from that. The registry will deal with disposing.
Be sure that the icons directory is listed in the build.properties file. Missing this will causes issues when you export your plugin.