I have been developing an android game for a while now and i have this problem. Let's take this example code
class game {
private Texture image;
public game()
{
image = new Texture(Gdx.files.internal("image.png");
}
}
If i write my code like this everything is fine, the image loads properly and the game runs smoothly.
The problems start when i try to create a folder inside my assets folder and load the image from there.
For example
class game {
private Texture image;
public game()
{
image = new Texture(Gdx.files.internal("newfolder/image.png");
}
}
When i do this and compile it i get no erros inside Eclipse and everything is fine. But when i try to export the project into a runnable deskop jar or as an android application, the game start for a second, shows a black screen then it stops. On android it doesn't even start. I believe it is not able to locate and load the image and throws an exception.
Any idea why this is happening and what am I doing wrong?
When you export your executable in Eclipse you need to package the required assets into the jar.
Often times when you export a JAR file it doesn't include the added library folders, and it searches for an external folder wherever the JAR is located.
Try checking the option for packing required assets into the JAR. This will actually put the assets inside the JAR file and make the necessary changes to the manifest.
I would also double check the LibGDX wiki and make sure that your projects build paths are properly configured to use assets folder from the android project.
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.
Title says it all, when I compile in eclipse it works fine, when I Export to a Jar file, they don't show up.
If you need anymore of the code, I will provide.
Image sprite = new ImageIcon("Sprites/Characters/spikeEnemy.png").getImage();
sprite = (e) ? new ImageIcon("Sprites/enemyProjectile.png").getImage() : new ImageIcon("Sprites/projectile.png").getImage();
(e: true for enemy, false for ally) sprite is declared earlier as (private Image sprite)
you need to use getClassLoader:
put the picture inside the src forlder
Image sprite = ImageIO.read(getClass().getClassLoader().getResource(path));
-> src /icons/icon.png
ImageIO.read(getClass().getClassLoader().getResource("icons/icon.png"));
this whay the image will be inside the executable jar
This is a build path issue.
Right click the project in eclipse -> add source folder. Place your images in there and update your references in your code accordingly.
I now got it to only work in JAR files, but it doesn't work in eclipse...
I accidently ran it like
sprite = new ImageIcon("Sprites/Characters/Player1.png").getImage();
And the sprites now show up in the jar, but there is no folder called "Sprites" anymore. So is it just a default thing you are suppose to put there?
im fairly new to java and im making a pokemon style game for practice and i would like to be able to send the game to my friends.
here is the main problem: the game works fine in my netbeans IDE, but using the jar file in my dist folder does not work and throws a nullPointerException. i have narrowed down the problem. my game uses imageIcons and png/gif images that i have imported in my libraries. im getting access to them like this
Icon bckground = new javax.swing.ImageIcon(getClass().getResource("/pictures/BG.gif"));
i am unsure how to get the images into the lib folder for the program to find. i have tried copying the files straight into the lib folder and creating a folder for them called pictures; neither worked. right now the lib folder contains only a single jar from one of my other libraries. (that is the only jar file that i am importing to my libraries)
Pic of what it looks like in IDE
In my case, i did something like this.
final BufferedImage image = ImageIO.read(new File("something.jpg"));
I just transferred something.jpg into the dist folder, and it worked fine.
After clean-building your project, put the BG.gif into the dist folder, then run your jar file in dist folder. Now everything should be fine. When sending your game to your friends, you can encapsulte (hide, set read-only) your code (google encapsultaion java), then with the BG.gif being transferred into dist folder, archive the project. Then your friends only need to unzip it and find jar file in dist folder.
Hope this will help:)
Please put your picture (BG.gif) in the package (directory) where it is used as icon Icon bckground = new javax.swing.ImageIcon(getClass().getResource("BG.gif")); inside the jar file. You need to change the path of the file first getResource("BG.gif") and then create the jar file from IDE. If the jar does not contain the image, you can open the jar using using any unzip application (winrar etc.) and copy and paste the the image file in the directory where the class is present. Please let me know the outcome.
I am trying to fix this problem. Trying different solutions but nothing works. I am using NetBeans IDE. I created a project with this structure and files:
E:\java\project\ecadpb\src\ecadpb
The image files are in
E:\java\project\ecadpb\src\
I have specified working folder for my project in Netbeans as E:\java\project\ecadpb
I have created my image icons like this
new ImageIcon("device21.png");
In my source file, it works perfectly while running the project in Netbeans but images are not showing up when I build and run my JAR file separately. But the image files are inside the JAR.
I have also tried the answers for the same question asked previously.
URL imageUrl=getClass().getResource("device21.png");
new ImageIcon(imageUrl);
But it doesn't work in my case. I am building a JAR file for the first time. Can anyone help me with this!!
A simple way of doing this will be to add the image in your classpath or a directory in your classpath say img as shown below:
E:\java\project\ecadpb\src\main\java\img\device21.png
And then load your image from this location like this:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource("img/device21.png");
ImageIcon icon = new ImageIcon(resource);
Create a source folder called Images (ie if you're using eclipse, right-click your project -> new ->sourceFolder). Call it whatever you want, i called my Images. Put some images in it.
Now i had JLabels where i gave them ImageIcons. Look at the following code.
ImageIcon BPawn;
ImageIcon WPawn;
JLabel Label = new JLabel[8][8] //2D array of labels that gives each spot a position.
public void setPieces(){
//set up pawns in their respective positions.
BPawn = new ImageIcon("Images/BPawn.png", "BPawn");
WPawn = new ImageIcon("Images/WPawn.png", "WPawn");
for(int i=0;i<Label[0].length;i++){
Label[1][i].setIcon(BPawn);
Label[6][i].setIcon(WPawn);
}//end for
}//end setPieces.
There is a lot more in setPieces() method, but this glimpse is how you would reference the images in your source folder when you create an executable jar and want the images to show up.
I think answer could be one these suggestions here
I have used a similar approach,
a) Specify the package path to the image file name.
b) Make sure that the image file is not ommited by your build scripts and that it is present in your jar file.
I can't get icons to appear on my button. I've copied this code from a book as I'm a beginner trying to grasp Java programming. Unfortunately I can't progress without getting this working, the other exercises are based on GUI images. I believe I've followed the exact steps, hopefully somebody can help!
Here is the simple code:
package Chapter13;
import javax.swing.*;
/** #author Chris */
public class TestButtonIcons extends JFrame
{
public static void main(String[] args)
{
JFrame frame = new TestButtonIcons();
frame.setTitle("ButtonIcons");
frame.setSize(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public TestButtonIcons()
{
ImageIcon usIcon = new ImageIcon("image/usIcon.gif");
ImageIcon caIcon = new ImageIcon("image/caIcon.gif");
ImageIcon ukIcon = new ImageIcon("image/ukIcon.gif");
JButton jbt = new JButton("Click it", usIcon);
jbt.setPressedIcon(caIcon);
jbt.setRolloverIcon(ukIcon);
getContentPane().add(jbt);
}
}
File Hierachy:
As you can see I've copied the "image" file twice as an attempt to try and debug this. All the images are in the "image" folder despite the photos not showing them all.
you need to create the image folder outside of the source package when you run from the netbeans while when you run directly executing the jar then you need to put the executable jar file and image folder in same folder or same place
It seems a bit of a problem with BookExcercises; that is not a normal project.
A normal NetBeans project (you can see all in the Files tab to the right of the Projects tab): there is a src directory shown under Source Packages. Building a project fills a target directory. Now a build would put the .class files there and also copy your images. Run (project) would run with as class path the target directory. Run File however might not find the images in the target directory.
A solution depends on the BookExcercises project. You might create a new project for every excercise. Depends.
If you are using a command Prompt to run your program, then simply paste this image folder alongside your chapter13 package folder(which contains your TestButtonIcons.class).
Or if you using some IDE, check where your compiled stuff goes. Again paste your image folder alongside your chapter13 package folder (which contains your TestButtonIcons.class). That will do the needful to run the program as you are expecting.
Regards