I know there are a thousand answers on the message boards to this question, and I have tried them all and none of them have worked for whatever reason. Here is an excerpt from my code:
ImageIcon icon = new ImageIcon("/home/james/programmingpics/A_Flute");
ImageIcon icon1 = new ImageIcon("/home/james/programmingpics/C_D_Flute");
ImageIcon icon2 = new ImageIcon("/home/james/programmingpics/D_E_Flute");
ImageIcon icon3 = new ImageIcon("/home/james/programmingpics/E_Flute");
ImageIcon icon4 = new ImageIcon("/home/james/programmingpics/F_G_Flute");
ImageIcon icon5 = new ImageIcon("/home/james/programmingpics/G_Flute");
ImageIcon icon6 = new ImageIcon("/home/james/programmingpics/B_Flute");
ImageIcon icon7 = new ImageIcon("/home/james/programmingpics/C_Flute");
ImageIcon icon8 = new ImageIcon("/home/james/programmingpics/D_Flute");
ImageIcon icon9 = new ImageIcon("/home/james/programmingpics/F_Flute");
pretty simple, works perfect while it is on my computer. Whenever I zip the dist folder move the pictures out of the directory on my computer and run the program, I get back empty JFrame's. So I then did the project/properties/sources/add folder thing, compiled it and still the same result. Then I moved the classes folder into the dist folder, tried to change the path and see if that worked, still nothing. It sounds like there is a simple answer to this, but I have clearly missed it.
You use absolute path names in the constructors. Examples of absolute path names are "/home/myusername/folder/foo/bar.png" or "C:\Folder\Graphics\foo\bar.png".
Your code given in your posting always wants to read exactly from the given path at runtime. If you move the graphics to a different folder, you're program won't be able to find them. This way, the graphics aren't included/packed into your program, but they're loaded at runtime (not compile time!) from the path specified in the constructor.
Such absolute paths are very bad practice because they prevent your program from being portable. Other people won't be able to run your program on their computers. Because if you try to run that on a different machine, the code will probably fail, because the path "/home/james/..." might not exist on that computer. Maybe your user doesn't even use Linux or at least his user name is not "james".
So you have to get rid of the absolute paths. Instead, you should take care to include your graphics in your project and have them being put into the JAR archive.
One approach would be putting your graphics into your project's directory structure and then doing something like this:
URL imageURL = getClass().getClassLoader().getResource("images/A_Flute.png");
ImageIcon fluteIcon = new ImageIcon(imageURL);
This, of course, won't work if your graphics stay in your home folder. You have to copy the graphics.
Related
I have added an icon to my JButton and the image is taken from a specific path inside my project files. But the problem is on another machine the path may be slightly different.
For instance my entire image path is home/jon/hello/folder/image.png, but on another machine the path may be /project/folder/william/hello/folder/image.png. Meaning the starting part of the path is different but from the hello part would be the same.
How can I make my path in my button code to be able to match the difference? My button code is below :
btn = new JButton(BUTTON_COLLAPSE_NESTED().toString());
btn.setIcon(newImageIcon("home/jon/hello/folder/image.png"));
btn.setActionCommand(BUTTON_COLLAPSE_NESTED().toString());
btn.addActionListener(btnListener);
btnPnl.add(btn);
This is why you shouldn't use absolute paths. Place the image in the jar, and use relative paths to resources, with the path based on the classpath to your class files.
For instance, if image.png is located in an images folder just off of the class file folder, you'd do something like:
URL url = getClass().getResource("/images/image.png");
BufferedImage img = ImageIO.read(url);
ImageIcon icon = new ImageIcon(img);
or something similar.
I'm working on compiling all my code into one jar file, but I have many images that I need to include. Java doesn't include them, and I have tried a lot of different methods, but most out there are not suited for the GImage object. Is there a way to include these, or do I need to switch to icons objects?
I can't really understand what the Gimage means. But I can read the image from jar file by below method:
java.net.URL imageURL = getClass().getResource("bomb0.png");
ImageIcon bomb0 = new ImageIcon(imageURL);
And then you can:
GImage image = new GImage(bomb0.getImage());
If you think this method is ok, you can have a test ;-)
I have already tried the following =
ImageIcon clear = new ImageIcon ("icons\delete1.png");
JButton clearBT = new JButton(clear);
And it works perfectly, I just have a question to make. The directory of the image is into my hard disk, but I want to deliver my project to my professor, so I am not sure if the image will still show up when in his computer. The thing is that I am not sure if the directory that I've put in my code (which exactly is "C:\\Users\\George\\Desktop\Giorgos\\icons\\delete1.png") will have something to show on my professor's pc.
Thank you in advantage for your answers, and if I am not clear enough, I am willing to rephrase.
If icons folder is in project directory folder and in code you are calling the image with :
ImageIcon clear = new ImageIcon ("icons\delete1.png");
And It's working perfectly ,then It will work anywhere.
Just Make sure you are not calling the image location address as its complete address that is referring to your computer hard disk.
If Your Professor's PC has internet access then you can use something like
(Please upload your file somewhere on internet like imgur and pass that url of your image new URL(here))
URL lenna =
new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png");
ImageIcon clear = new ImageIcon (URL);
JButton clearBT = new JButton(clear);
Put the picture in you projects and you will have to change your code to
JButton clearBT = new JButton(new ImageIcon(getClass().getResource(image_path)));
Another option would be to ship the image bundled with your source code. For instance, create a "package" / directory inside your project hierarchy and store images there.
Then, you can load them this way:
ImageIcon image = new ImageIcon(getClass().getResource("/path/to/the/image");
For instance, if the image is under org/example/program then the above will be:
ImageIcon image = new ImageIcon(getClass().getResource("/org/example/program/image.png");
I've been putting all of my images for my Java application in a package called "rtype" inside src where I also also have my Class that deals with these images. I wanted to sort the images and put them in a folder of their own. When I do this, The images will no longer load into the class, and I know it's because I changed the file path. I've done some research and tried a few different things. This is basically what I had originally:
String walkingDown = "WalkingDown.gif";
ImageIcon ii;
Image image;
ii = new ImageIcon(this.getClass().getResource(walkingDown));
image = ii.getImage();
and It worked just fine before I moved the location of the images outside the location of the class. Now it cant find the images. Here is what I tried and found online to try (The folders Name is Sprites):
//use getClassLoader() inbetween to find out where exactly the file is
ii = new ImageIcon(this.getClass().getClassLoader().getResource(standingDown));
and
//Changing the path
String walkingDown = "src\\Sprites\\WalkingDown.gif";
//also tried a variation of other paths with no luck
I am using the C drive, but don't want to use "C" in my extension, as I want it to be accessible no matter where I put the project. I am fairly stuck at this point and have done enough looking into it to realize that It was time to ask.
I have a separate "package" for images with that name (in the src folder)
Try something like this:
try {
ClassLoader cl = this.getClass().getClassLoader();
ImageIcon img = new ImageIcon(cl.getResource("images/WalkingDown.gif"));
}
catch(Exception imageOops) {
System.out.println("Could not load program icon.");
System.out.println(imageOops);
}
Your variable is named walkingDown, but you pass in standingDown to the getResource() method.
new ImageIcon("src/Sprites/WalkingDown.gif");
I have a Java project called MyProject. I have a few different packages (keeping names simple for the purpose of this question), as follows:
src/PackageA
src/PackageA/PackageAa
src/PackageA/PackageAa/PackageAaa
src/PackageB
src/PackageB/PackageBa
src/PackageB/PackageBa/PackageBaa
I have a class
src/PackageA/PackageAa/PackageAaa/MyJavaFile.java
And I have an image
src/PackageB/PackageBa/PackageBaa/MyImage.png
Inside of MyJavaFile.java, I would like to declare an Image oject of MyImage.png
Image img = new Image(....what goes here?...)
How can I do this?
You could either call Class.getResource and specify a path starting with /, or ClassLoader.getResource and not bother with the /:
URL resource = MyJavaFile.class
.getResource("/PackageB/PackageBa/PackageBaa/MyImage.png");
or:
URL resource = MyJavaFile.class.getClassLoader()
.getResource("PackageB/PackageBa/PackageBaa/MyImage.png");
Basically Class.getResource will allow you to specify a resource relative to the class, but I don't think it allows you to use ".." etc for directory navigation.
Of course, if you know of a class in the right package, you can just use:
URL resource = SomeClassInPackageBaa.class.getResource("MyImage.png");
(I'm assuming you can pass a URL to the Image constructor in question. There's also getResourceAsStream on both Class and ClassLoader.)
you can use relative path since the the relative path is project folder.
ImageIcon img = new ImageIcon("src/PackageB/PackageBa/PackageBaa/MyImage.png");
/folderB/folderBa/folderBaa/MyImage.png
The image can stored into a project folder location .eg: /images/MyImage.png
Then try:
Image img = new Image(/images/MyImage.png);
Using a file path is not possible when running a program that's in a jar file, especially if the program is being loaded as an applet or WebStart application then you can use ClassLoader to get image.
use the following code to load the images:
ClassLoader cldr = this.getClass().getClassLoader();
java.net.URL imageURL = cldr.getResource("/PackageB/PackageBa/PackageBaa/MyImage.png");
ImageIcon aceOfDiamonds = new ImageIcon(imageURL);
This IS the best way to handle all images and icons in a JAR App.
Once you've zipped up all of your images and icons into its own JAR file - Configure your build path by adding the images JAR file into your libraries tab so that its now included in your classpath.
Then simply use the following 3x lines of code at the start of your constuctor to access any image you need for anything including a SystemTray image which doesn't accept the simple ImageIcon's as its main icon (weird I know). The 3x lines are:
URL iconUrl = this.getClass().getResource("/image-iconb.png");
Toolkit tk = this.getToolkit();
imageIcon = tk.getImage(iconUrl);
(imageIcon is just a constructor declared Image variable)
Now you can set a window icon as simply as:
setIconImage(imageIcon );
and at the same time use the same variable when setting the System TrayIcon by declaring:
trayIcon = new TrayIcon(imageIcon, "SystemTray Demo", popupMenu);
The above allows you to declare Images or ImageIcons easily and centrally without running the risk of not keeping image resources in the right place. It keeps it nice and tidy, with the JAR containing all your images automatically compiled at run time and distribution of your program.
As a bonus, once the JAR is registered in your classpath - you can keep adding any other images into the same JAR at any time without any fuss too - Everything just works and the added images are instantly available to your app.
Much better in my view.
Use the getResource method to read resources inside the src root. For example, the following code retrieves images from a folder src/images.
// Get current classloader
ClassLoader cl = this.getClass().getClassLoader();
// Create icons
Icon saveIcon = new ImageIcon(cl.getResource("images/save.gif"));
Icon cutIcon = new ImageIcon(cl.getResource("images/cut.gif"));
The example assumes that the following entries exist in the application's JAR file:
images/save.gif
images/cut.gif
Image img = new Image("./src/PackageB/PackageBa/PackageBaa/MyImage.png");
This shall go the path of the image is first inside src (source) then package so the program would access the image this way.