JFrame background image. "absolute path in a file system" - java

I am creating a GUI using WindowBuilder in Eclipse and I searched for ways on how to add background images. Some suggested the use of a JLabel. There is an option in for labels called icon and I can set that to an image in an "absolute path in a file system" however this option gives a warning to never use in real application. I think this is because the program cannot find the image if the app is opened in another PC.
How can I add an image that can be used in a deployed application?

Related

Executable jar file window size is bigger and not as sharp as when opened via netbeans

When I clean and build my java application and run the .jar file, it is bigger and not as sharp as when I run it via netbeans. I couldn't figure out the reason for it, I tried to fix the frame sizes, but nothing has changed. enter image description here
Here's a picture of it:

How to put pictures in NetBeans?

I'm making a game in NetBeans and I have to put some pictures and when I start the program they are there, but when I do clean and build pictures are not in the JAR file. Can someone help me? I'm not using JFrame form.
I am using JFrame and I think it's similar to whatever you are using while it's on netbeans
you have to put them in src folder
right click on the jLabel (if you using JFrame you have to add jLabel )
choose properties
choose icon
choose image within project
and you will find all pictures in src folder
this way when you share the JAR file the pictures will be inside it
but if you chose External image in step 5 you will use just the path of the image ( I think here's your problem )

Inserting icons in downloadable jar application

I am a novice programmer trying to create a jar file that people can download and demonstrates a...JRPG-ish upgrade system I have. As a little visual pop, I want the central button to display a little icon as an extra visual pop. While it works fine in Eclipse, when I export it to a JAR file the icon is no longer displaying (though the rest of the program works as intended.
I have managed to make Eclipse import the files I want into the JAR file, as seen below.
This is the code I currently have to display them in Eclipse
imgDin = new ImageIcon("icons\Mark_of_Din.png");
imgNayru = new ImageIcon("icons\Mark_of_Nayru.png");
imgFarore = new ImageIcon("icons\Mark_of_Farore.png");
How can I modify this code to display them inside the JAR file?
You should be using
new ImageIcon(getClass().getResource("/Mark_of_Din.png"));
That little package icon in the icons folder icon, means the files in the icons folder will be at the root of the classpath. getClass() gets the calling class, and getResource() locates a resource relative to the class. The / brings the search to the root.
You can extract the jar and you will see the icons folder is not there.
See Related Post
"While it works fine in Eclipse.."
That's because when you pass a String to the ImageIcon constructor, it is looking for a file on the file system, relative to the working directory. In Eclipse (and most IDEs), the default working directory is the project root, and that's where your icons folder is.

Project Not showing icons after Executeable jar file

I am running net beans application, when i run it via net beans it is showing icons properly...but when i convert it to executable .exe it is not showing icons..i did a lot of research to solve this problem but everything gone in vain. This is the best method i have saw to get icons dynamically....
My icon heraricy is src/com/idoccsmain/icons/ and all of my icons inside the "icons" folder
ImageIcon icon = new ImageIcon(Main.Class.getClass().getResource("/com/idoccsmain/icons/"+"add_icon.p‌​ng")
through this line i am getting all of my different icons..can any one suggest any solution for me....?
Thanks guys for responses with your help i came up with this... solution
put images in Src folder e.g src/icons/ and inside icons folder.
Than use this line in netbeans to get your images working......
ImageIcon icon = new ImageIcon(getClass().getResource("/icons/AnyIconName.png"));
and make sure use this line for each icon.
For J2ME Application
Check this image carefully, Right Click on project then Properties.
Select Application Descriptor and then select MIDlets
If it is showing anything there, like showing in this picture then edit accordingly.
NOTE : - In the above scenario src/myproject/ where icon and .java exist.
===== UPDATE =====
Desktop based executable jar
Then use, this code
this.getFrame().setIconImage(new imageIcon(getClass().getClassLoader().getResource("add_icon.p‌​ng")));
Note: this line only works if the images are in the root of the jar file. If not, you have to specify the folder on the string:
getResource("yourfolder/add_icon.p‌​ng")
You might also interested for this link to create executable java file and also associate icon to it.

Graphics not appearing for java game

I have been working on a 2D top-down (like old zelda) game in java with eclipse IDE. When I run the game during dev and after I compile it for testing it runs fine on my computer but when I run it on another computer it only shows text that I put in with code not graphics. I watched a video on adding textures to your game by TheCherno on YouTube, I followed what he said by putting it in the project's res folder and I fixed the code to get the graphics from there. I then compiled it again and to no avail it still didn't work on other computers. If you have any solutions I would be extremely happy.
Here is a Link to the eclipse project folder so you can see the code and setup:
http://dl.dropbox.com/u/4229589/Copy%20of%20Copy%20of%20ld48.zip
It may be that the location of your resource folder changes when used on another computer.
It would be helpful to see the code you use to access the resource folder.
for example
If your filepath was say (inputting my username as an example):
C:\Users\IanMelrose\Game\Resources
It would not appear that way on another computer.
Try selecting all of the images, right clicking -> Build Path -> Add to build path
Can you show an example of how you reference an image in your code?

Categories

Resources