Relative URL not working in javafx - java

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.

Related

Why does my ImageView work on Windows but not on Mac

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)).

How should I use getResource()?

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

Java: Set iconimage without using full path

I'm creating a game in java, and I have some problems setting my icon image:
ImageIcon img = new ImageIcon("/Users/me/location/mygame/res/logo.png");
f.setIconImage(img.getImage());
This works fine, but instead of /Users/me/location/mygame/res/logo.png, I want /logo.png, as I do with all images except this one.
Example of a place when it works perfectly:
player_sheet = loader.loadImage("/player_sheet.png");
This example is from my Texture class.
If I put /logo.png in the path, there will be no icon at all.

Cannot create image from getResourceAsStream() in JavaFX projects

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

Display Bitmap resource in MainScreen banner for BlackBerry

Hey guys,
I'm having trouble displaying my icon in the banner of my MainScreen. The Icon is located in the res/ directory. Here's my MainScreen code:
HorizontalFieldManager mngr = new HorizontalFieldManager(USE_ALL_WIDTH);
LabelField label = new LabelField("AACD");
BitmapField pic=new BitmapField(Bitmap.getBitmapResource("res/aacdIconSmall.PNG"),Field.FOCUSABLE);
mngr.add(pic);
mngr.add(label);
this.setBanner(mngr);
BrowserField browserField = new BrowserField();
add(browserField);
The AACD Label shows up above my BrowserField but my pic bitmap never shows up. Any ideas?
Your code is OK.
However I do suspect you did not add the image to your JDE project, so the image is not included in the resulting .cod file during building the project.
No need to use res folder
try Bitmap.getBitmapResource("img/aacdIconSmall.png")
the folder should be like this
res
- img
- aacdIconSmall.png
(i dunno is accdIconSmall.PNG is case-sensitive or not, have not tested yet, just make sure you are using the same upper and lower case)

Categories

Resources