In a JavaFX application i'm developing I use several icons to style some buttons and view objects. The first icons I used were displayed and packed to the deployed files (.dmg with .app and .exe) successfully, since I had to tell Ant to include the resources folder with the icons.
When I run the project with Eclipse, all the icons and images are displayed right, but when I deploy the project, only two of them (the last two included in the last improvement) are not shown. Why could be this, since I didn't change any folder or configuration?
When I get the deployed .app, I right click on it and clicking "show package contents" I can see that not only these two icons are not packed there in /Contents/Java/Resources/, but many others that mysteriously are actually shown in the application. For now I solved this copying all the required icons there, but I can't do this with the .exe generated file, which doesn't show these 2 correct icons.
My application is a music organizer/player called Musicott, is on github so you can see all the code there. The two icons are the slider thumb used to visualize and move through a track; and the default cover image for tracks that doesn't have one.
In this image you can see the problem better http://i.stack.imgur.com/8aVcz.png
Code where I set the default cover image at /com/musicott/view/RootLayoutController.java lines 503-506
Code where I set the default cover image in the play queue list at
/com/musicott/view/PlayQueueController.javalines 98-101
Code where I set the slider thumb icon in
/com/musicott/SceneManager.javalines 245-249
Thanks in advance. I will answer any doubt about the code and the project.
Nice looking project! So, as James_D pointed you are currently loading your icons with the following code:
new Image("file:resources/images/default-cover-icon.png")
new Image("file:resources/images/default-cover-icon.png", 45, 45, true, true));
The problem is that when you pack your application the "file" protocol is no longer valid (remember you are sending a URL to the Image constructor). You could send instead an InputStream (you can get one from the current Class), see this sample code:
new Image(RootLayoutController.class.getResourceAsStream("images/default-cover-icon.png"));
Now, if you use the code exactly like that you will need to make sure (after building the jar) that the "images" folder ends up inside the "com/musicott/view/" folder (since the RootLayoutController class is inside that package), so you will have to add the two extra "com" and "musicott" folders inside your current "resources" folders and move the "images" folder there.. you will probably need to add this in your pom.xml file too (to make sure you export the resources since I think maven will only export by default the "resources" folder located at "src/main/resources"):
<resource>
<directory>resources</directory>
</resource>
Note that if you want to keep your current folder structure (without creating the "com/musicott/view" folders inside the "resources" folder) you can change the path in the getResourceAsStream() call to "/images/default-cover-icon.png" (slash at the start of the string) to indicate that the images folder will be at the root.
Keep up the good work!
Related
I don't quite understand how creation of a Swing application really works.
I have created a simple application that gets a background image from a path.
ImageIcon imageIcon = new ImageIcon("C:\\Java\\ApplicationName\\out\\Resourses\\backGround.jpg");
Image image = imageIcon.getImage(); // Создание картинки из него
Image temp = image.getScaledInstance(500,500,Image.SCALE_SMOOTH);
imageIcon = new ImageIcon(temp);
g2d.drawImage(temp,0,0,null);
It works, so I decided to make a JAR file independent from changes in the Java folder (like deleting this image) and put images in src - images ( package ). I don't know if it is possible though.
I started to get background image like this.
ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("/images/backGround.jpg"));
The program works perfectly in IntelliJ IDEA but if I extract it to JAR file I get a blank background screen. Why is that?
And can I have my entire application including images in a single JAR file? One that is not dependent on other folders?
You can certainly put the image and the code in a single jar file. The image is not loading either because it's not in the jar file, or because it's not at the path /images/backGround.jpg inside the jar.
One helpful thing about jar files that you may not know: they are in .zip format, so you can use any zip tool to see what is inside the jar (temporarily renaming the jar file to .zip might make this easier). IF the image is not there, fix your jar build (you didn't say how you are making it, so not sure what you need to change). If the image is there but under a different path, then fix the jar build to put it in the right place, or change the path you are reading it from.
Yes definitely you can include your entire application into single jar file i.e. Runnable jar file. But i would suggest you not to include your image inside the jar file, instead pass that image path to an Property file and use that property file in your code for image path. So that even if next time you need to change the image you just change the path in your property file and you dont have to export your entire project again.
I'm working on a game, and I've stumbled across a fews ways to load an image into my program. There are two common ways:
The first (No class folder needed):
ImageIcon ii = new ImageIcon("image.png");
Image i = ii.getImage();
When using this method, you simply add a folder to your Project Folder (In Eclipse) and DO NOT have to make it an external class folder.
The second option (Class folder needed):
Image i = ImageIO.read(ImageLoader.class.getResource("image.png"));
You can only load images this way if you made a resource folder (typically called 'res') and added it as an external class folder to your project.
My question is why would you use the second option instead of the first? From my limited experience, it seems like the first option works just the same as the second, just without having to turn the folder you're adding it to into an external class folder.
The first option doesn't work if you package your application as a jar. The second option works in both cases, as an exploded application in an ide or on the hard drive and packaged in a jar file.
new ImageIcon("image.png"); assumes that the image is stored on the file system (in your case, in the current working directory), which raises up some issues - as the working directory isn't always the same location that the jar is stored.
ImageIO.read(ImageLoader.class.getResource("image.png")) embeds the image within the Jar file which resolves the issues associated with the first method as the image is where ever the Jar file is.
It also simplifies installation, as you only need to track the jar files and not all the other "external" files.
ImageIcon doesn't report the reasons it fails. It loads the images in a separate thread, which make diagnosing issues difficult.
ImageIO loads the image in the current thread (and returns a fully realised image) but will also generate a IOException if the image fails to load for some reason, making a much better way to load images in general. It also supports (or has the capacity to support) a wider range of images.
In general, using ImageIO and embedded images is simpler and easier to work with and is generally the recommended approach for fixed resources
I'm a teacher attempting to use the AP Computer Science Picture Lab activity. Here are the teacher instructions:
Students should keep the images folder and the classes folder together in the pixLab folder.
The FileChooser expects the images to be in a folder called images, at the same level as the classes folder.
If it does not find the images there it also looks in the same folder as the class files that are executing.
If you wish to modify this, change the FileChooser.java class to specify the folder where the pictures are stored. For example, if you want to store the images in “r://student/images/,” change the following line in the method getMediaDirectory() in FileChooser.java:
URL fileURL = new URL(classURL,"../images/");
And modify it to
URL fileURL = new URL("r://student/images/");
I have created a GitHub repo for them to fork and use in Eclipse, but I'm having trouble getting the images in the right place for Eclipse to see them. Where should they be in the Eclipse Package Explorer? The tree now is:
PixLab > src > default package > various classes.
At what level should I drag and drop the images folder into?
Alternatively, what should I the following line to read?
URL fileURL = new URL(classURL,"../images/");
I'm wondering if there is some confusion between the naming of the project in Eclipse with the folder name for the source files.
The directory structure that you give does not include a pixLab folder. I'd be expecting something more like the following in Eclipse:
PixLab > src > pixLab > various classes
> images > various images
Then, the line:
URL fileURL = new URL(classURL,"../images/");
makes sense, as you are going up one level from the classes folder, and down from there to the images folder.
For the reference to a "student" folder, I think they are considering the scenario where the images are stored in a shared network file folder. In that case, they would NOT be part of the Eclipse package, and the FileChooser Url would have to be modified to reflect your chosen network location for the images.
If you've already set up the project in Eclipse with the "default package", can I suggest doing the following:
From within Eclipse:
1) make a package, named pixLab
2) drag & drop files from the default package to the new package
Eclipse should automatically add the following line to the top of all the source files that you brought over:
package pixLab;
3) place the images folder under src, parallel to the pixLab package.
A "package" functions as a folder. If everything is in the same folder, then the rest of the code should work fine.
Thanks for the attempt, but I'm afraid Java still can't find the jpg images. I'm afraid I'm not yet allowed to post images, but I took a screenshot, and the files are laid out exactly as suggested above. Any other ideas, anyone?
setNodeIcon("icons/person.png");
or
setIcon("icons/person.png");
In respectively SmartGWT TreeGrid and Tab (in TabSet) should find "person.png" in folder "icons" I guess but where should I put "icons" in the project itself?
In the JavaDoc it's also said that the default value for setNodeIcon is [SKIN]file.gif what does [SKIN] mean ?
Thanks
I don't know where you find the default value (to my sense this use of the Skin folder is for using images already in the smartgwt skin)but any way I usually put my images directly in the war in my project with subfolder related to their size.
To use the icon I simply do for example btnClear.setIcon("16/clear.png");
With war/images/16.clear.png image folders structure.
You can have a look at getSkinImgDirand do a test
In my drawable folders i have my icon image, and when testing i use it as a temp picture. I added 2 other images and a XML file and when ever the app loads the imageviews with the icon set to it has different images..
this is what it is meant to be be..
but this is what it is when i add other images to the drawable folder
And no, the names of the files are not the same.
The icon is called 'icon.png' - thats what it should be
instead it is 'ic_menu_compose.png' - a completely different name.
Also the files i'm adding are called:
'buttonnormal.png'
'buttonclicked.png'
and a XML file - 'buttonselector.xml'
Even if i dont use these images any where within my app the problem still occurs.
Any help would be much appreciated. Thanks! :)
[EDIT]
Are you building your app inbetween tests?
In eclipse: Project > Build Automatically (ticked) (this regenerates your R file)
Are you referencing the image by a raw ID?
setImageResource(324234234); WRONG
setImageResource(R.drawable.blah); Right
Are you using drawable image names that are already in use by the Android system: http://androiddrawableexplorer.appspot.com/
Personally I would rename that images from "ic_menu_compose" to something like "icon_menu_compose_overwritten" just incase it is clashing and screwing up your R file.