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.
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 am working on GUI java project which contains FileChooser(combined with JLabel it becomes ImageChooser) and JTextArea (inside of JScrollPane). Both of these components are inside of JPanel.
When ever I ran it inside of IntelliJ Idea (version 2017.2.4)everything works fine:
UI when executed from IDE
But if I build Artifacts and create .jar file, then image inside of JLabel is not initialized and the size(height) of JTextArea becomes minimal(though minimal value is set to 200):
IU when executed from .jar file
I suspect that ImageIcon cannot be initialized due to relative path I provide:
...
imagePath = "src/main/resources/" + item.getImageName();
//item.getImageName() returns a proper image name, tested with
//System.out.println() and there is a proper image in that folder.
ImageIcon img = new ImageIcon(imagePath);
img = ImageManager.resize(img);
...
//Resize function in ImageManager class
public static ImageIcon resize(ImageIcon imageIcon, int size){
return resize(imageIcon, size, size);
}
public static ImageIcon resize(ImageIcon icon){
return resize(icon, defaultSize);
}
However, I've tried options with relative path like main/resources/ and /main/resources/ , but none of them worked both in IDE and .jar executable.
Is it a problem with a path?
If yes, why does it affect JTextArea's size?
P.S.
JTextArea's size becomes normal if there is an image in JLabel.
You are right, the way you fetch resources is problematic in a jar.
The way you should access them:
ImageIcon img = new ImageIcon(this.getClass().getClassLoader().getResource(item.getImageName()));
This method supports relative paths. Just make sure your src/main/resources directory is properly marked as a 'Resource Root' in IntelliJ IDEA.
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.
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)
In my project I have 2 packages.
images - contain images and
notification - contain java files
In notification/main.java I get Image object from image using this code
Image image = Toolkit.getDefaultToolkit().getImage("/images/key-16x16.png");
and I can't get image.
How can I fix this bug.
I'm using Netbeans to develop Java desktop application and I have solved my problem.
Image image = new ImageIcon(this.getClass().getResource("/images/bell-icon16.png")).getImage();
"this" is a class extends JFrame
/ means an absolute path, save Java web-apps where / means relative to context. So, I would suggest to use relative URL, means get rid of that / in front, and then provide the right path.
In case, even then you can't solve it, try to create a file on the same path you are looking for image. This way you will know that where you are looking exactly and where you should look.
You could also try
Image image = new ImageIcon(path).getImage();
Incase the solution above doesn't work, try this (which worked for me):
Image image = Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("/Images/bell-icon16.png"));
i've also been on this and it turns out that you need to create a package inside of your src folder.
for instace if you create a package called images inside of the src folder, your relative path will be
/images/yourimage.png.
Notice that the slash(/) must be there!
more info here http://forums.macrumors.com/showthread.php?t=533922
it worked for me
Toolkit tk = Toolkit.getDefaultToolkit();
Class<? extends JFrame> j = YOURJFRAME.getClass();
Image image = tk.createImage(j.getResource("/images/bell-icon16.png"));
Try that code, if you have a JFrame that will work.
If you have an Applet, then just use
Toolkit tk = Toolkit.getDefaultToolkit();
Image image = tk.createImage("images/bell-icon16.png");
With applets you never want to use the / in the beginning, but if you have a JFrame, and you are using getResource, you need the / in the beginning of the Path.
SwingResourceManager.getImage(YourClass.class,"key-16x16.png");
The getIcon method will return Icon as similar
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("../resources/MainIcon.png")));