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?
Related
I wrote code that works whenever i run in Eclipse, but whenever i try to export it into a runnable Jar, my images dont show up.
For my images this is the code being used pic1=ImageIO.read(FourCournersRunner.class.getResource("/Images/1.png"));
When exporting the project I've chosen "Package required libraries into generated JAR":
Please also show the code where you display the image in GUI, the problem might lie in there.
Did you also package the Images folder to the .jar file?
The problem might be caused by the way you set up build path too.
In short, I don't think the code you provide right now can reproduce the problem.
I am trying to learn Java from an online class, however I am having problems following the instructor's video.
is his screen, where he is trying to print a line.
My screen looks like
As you can see, the "out" part of System.out is not the same on our screens, and the icon for class is different on both our screens as well. The main problem I am having is I'm not able to run the line of code that I wrote. I apologize if this question has been asked already, I was not sure what keywords I would use to search this problem.
As you can see, the "out" part of System.out is not the same on our screens
This probably is due to the fact that you haven't opened up a Java Project. As you can probably see in the Two Images, the instructor's image, has A Project hierarchy on the left, while yours is empty. Therefore, To correct this Try The Following Steps:
File->New Project->Java Project
Now follow the onscreen instructions to create the Java Project. Then the project would be created. Now expand the project on the left pane, and do it until you find a folder named src . Select the folder, and choose new Class, and then You can create your new Classes
The main problem I am having is I'm not able to run the line of code that I wrote.
This is probably due to the fact that you Have not set up your JDK path in IntellIJ. This is backed up by the fact, that your run button (The Green Arrow on the Instructor's), is grayed. To fix this, click On the small Button beside the arrow. There, Click On edit Configurations. The following Window should pop up
If Default is not expanded, then expand it and Choose Application. The Window Should probably be like this :-
Then Click On the plus icon on the top
There, Enter You Main Class(The Class Which has public static main void(String args[]))
If you can't, choose the ... option, and choose
Finally, select the JRE path, that is, the location where you installed The JRE.
Also, make sure that You have the 'path' environment variable set to the bin of your jdk Installation Folder.
Ask if you have any problems doing this.
First you shoud do this: File- New- Project... Then create new project. And then you click right mouse button on src folder(in your new project), choose new "Java class" and copy code to that new class. And then you'll be able to run your main method.
Problem is that you just use java-file from somewhere (desktop, probably), but you should have it inside your project.
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.
CONTEXT FOR MY PROBLEM
I am creating a Project using java swings on netbeans IDE 7.1.2 plateform on
windows m/c.Then I'm executing the jar file created on windows m/c in a linux m/c(cent os).
In a part of my project i needed to display some pictures in run time.
PROBLEM STATEMENT
Case 1)
Now what happened is that in run time i brought those pictures from some other location to src directory of my project by executing cp(copy) command through java code.
And yes i have checked those pictures were copied inside src directory successfully.
After that when i tried to display those images in a label,pictures were not being displayed.
So i wondered why is that happening.
Case 2)
After this problem i made a change and i imported those pictures inside src folder of my project before "cleaning and building" the project on windows m/c instead of bringing them in run time on linux m/c.Then after this when i executed the jar file on linux m/c,pictures were getting displayed in label.
So only change i made was that i included the pictures in src directory during "cleaning and building" my project on windows m/c.
So what i don't understand is that
why pictures are not getting displayed in 1st case.
MY QUESTIONS AND DOUBT
1) In context to resources like files and pictures(which are being used in project and i'm not talking about source codes of project here),what exactly happens when a jar file is created ? Are those resources(files & pictures) also included in jar while building the project or not?
2) Why i'm facing such a problem ?(And i have also checked the permissions on those image files.They were exactly same in both cases.)
Any insights that any of you can provide will be deeply appreciated.
So I assume you ar doing some thing like
label.setIcon(new ImageIcon("src/path/to/image"));
or
BufferdImage img = ImageIO.read(new File("src/path/to/image"));
or worse, using absolute file references...
Netbeans will add the content of of your src directory to the resulting jar file when the project is built (excluding your source files ;)), this changes the way in which these resources need to be treated, you can no longer treat them as you would if they were files on the file system.
Once embedded within your application context (such as been included in your jar file), they become what is commonly known as embedded resources.
Instead, you need to use Class#getResource or Class#getResourceAsStream depending on your needs, for example...
label.setIcon(new ImageIcon(getClass().getResource("path/to/image")));
Updated with after more details
Image im=newImageIcon(this.getClass().getResource("/home/aman/Desktop/diya.jpg")).getImage();
Is the wrong approach for his type of file. This file is a file on the file system, instead you should be using something more like...
BufferedImage image = ImageIO.read(new File("/home/aman/Desktop/diya.jpg"));
jLabel1.setIcon(new ImageIcon(image));
Note, ImageIO.read throws an IOException, which will be expected to catch, this is useful for diagnosing missing files or bad image formats
Unless you have previously changed the visibility state of the label, there is no need to call setVisible, in fact, you're setting label invisible anyway...
Okay; So I've been using Windows for quite some time, however I've recently moved over to a linux-based operating system. I've ran into numerous issues such as the File-System for Linux being case-sensative. IE: Filename.PNG will not load if you try to load Filename.png.
Anyway, That's not my problem here, and I'm kind-of confused.
Here's my problem, in cropped picture format, code and all.
I've tried varations, such as "/SpriteSheets/tileset.jpg", "assets/SpriteSheets/tileset.jpg"
However, I can't get it to work.
Well it seems your file name is tileset.png and not tileset.jpg.
Print your current path first with a System.out.println(new File(".").getAbsolutePath()) and then try and see where you should point your path in the program.
EDIT: Eclipse usually points your path inside the workspace, I've had trouble with that previously. You can set the runtime path to a different directory; Go to Run configurations, choose your configuration, choose tab Arguments and set the working directory (at the bottom) to a directory of your choosing.