Not sure about image on JButton - java

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");

Related

Referencing a bufferedImage on a jLabel within a .jar file

I created a .jar file from Eclipse for a project.
Before the .jar creation, I was reading in images like this:
jLabel = new JLabel(new ImageIcon("src/someImage.png"));
But now, for the .jar, I'm using:
BufferedImage img = ImageIO.read(getClass().getResourceAsStream("someImage.png"));
jLabel = new JLabel(new ImageIcon(img));
Which works fine, however further in the code, I need to pick between particular images, and the non-.jar file used this:
icon = jLabel.getIcon().toString();
if (icon.contains("happy")) {
// do something
But obviously this won't work. The icon as is comes out just as something like javax.swing.ImageIcon#35083305
I tried just adding something like jLabel.setText("someText"); but I have been unable to retrieve said text.
I saw this, and note that BufferedImage doesn't have a method to get its filename (as of course it may not be a file).
Is there a way I can reference the jLabel in a similar fashion?

java - Reading a image filepath that may change

I am creating a simple GUI using swing in Java and simply want to display a JPEG image as a banner in the frame. I have the image working properly, but the filepath is subject to change as this will be sent to other people. The image is stored in the folder I will be sending to others. I am looking for a way to ensure that no matter what location the the folder has been moved to, the image will display. I am new to this site, and fairly new to Java. Thanks for you help in advance.
Relevant Code:
ImageIcon numberImage = new ImageIcon("C:\\Users\\Me\\Desktop\\numberGame\\numbers.jpg");
Image image = numberImage.getImage();
Image newimg = image.getScaledInstance(300,120,java.awt.Image.SCALE_SMOOTH);
numberImage = new ImageIcon(newimg);
JLabel imageLabel = new JLabel("", numberImage, JLabel.CENTER);
JPanel imagePanel = new JPanel(new BorderLayout());
imagePanel.add(imageLabel, BorderLayout.CENTER );
To ensure, the image could be displayed in every context, you must specify a relative path instead of an absolute path.
You could change:
ImageIcon numberImage = new ImageIcon("C:\Users\Me\Desktop\numberGame\numbers.jpg");
To:
ImageIcon numberImage = new ImageIcon("numbers.jpg");
And this should work in other contexts (with other different paths).
Or if the image is in a directory inside your program put "\imageFolder\numbers.jpg".
This should work, the main change is to replace the absolute path with a relative path.
You want to use a relative path instead of absolute. Here's a quick explanation that's pretty clear.
http://www.xyzws.com/javafaq/what-is-the-difference-between-absolute-relative-and-canonical-path-of-file-or-directory/60
Good luck. :)

How do I add an image to JPanel using imageicon (Java GUI)?

I looked all over the place but I am still stuck on how directory works to find the image to put onto the JPanel. Where is the image supposed to be? I clicked on properties for my image and it shows Location: C:\Users\Joseph\Pictures\Background and the picture's name is random.jpg.
I am trying to add an image to a tab using tabbedPane. Here is what I have so far, and I am not able to do it.
JPanel flPanel = new JPanel();
flPanel.setLayout(new FlowLayout());
ImageIcon image = new ImageIcon(getClass().getResource(""));
// Tried /Users/Joseph/Pictures/Background/random.jpg and doesn't work
JLabel j1 = new JLabel(image);
flPanel.add(j1);
tabbedPane.add("Tab 2", flPanel);
Is the picture supposed to be in the same package file as the project? Or is it supposed to be in the source file to be able to just do "random.jpg"?
If you want the image to be available to your application at runtime, then you should consider making sure that the image is included within your Jar when you application is built.
From the sounds of it, you are using Netbeans, you should copy the image to a directory within your src directory of your project.
You should then be able to use...
BufferedImage bi = ImageIO.read(getClass().getResource("/full/path/to/image/random.jpg"));
ImageIcon image = new ImageIcon(bi);
The path to the image should be the full path (from the context of the src directory) within your project.
That is, if you placed the image in the resources directory within the src directory, then you would use /resources/random.jpg as the path/file name
Take a look at Reading/Loading an Image for more details
getClass().getResource(...) will only get resources inside the classpath.
You can use ImageIO.read(File) like this:
BufferedImage bi = ImageIO.read(new File("C:\\Users\\Joseph\\Pictures\\Background\random.jpg"))
ImageIcon image = new ImageIcon(bi);

Import image GUI not appearing

I am trying to upload an image on a GUI. Here is what my code reads right now:
ImageIcon icon = new ImageIcon(nameSearched.getImage());
myLabelK = new JLabel();
myLabelK.setBounds(500,100,200,200);
myLabelK.setIcon(icon);
myPanel.setLayout(null);
myPanel.add(myLabelK);
Validate();
add(myPanel);
setVisible(true);
When I run my program this image is not popping up on my GUI.
The nameSearched.getImage() is calling a method in a different class which returns an image entered into the system. For example, Peter.jpg.
Please help me figure out how to get my image on the screen.
If you are just using Peter.jpg as you relative path name,
Using an IDE, you image should be ditrectly below the ProjectRoot directory
ProjectRootDir
Peter.jpg
src
If you wanted to have your image in an images folder:
use "images\Peter.jpg"
ProjectRootDir
images
Peter.jpg
src
Maybe you want to retrieve the image like this
ImageIcon icon = new ImageIcon("Peter.jpg");
Another possibility, If you do this:
ImageIcon icon = new ImageIcon(nameSearched.getImage());
then nameSearched.getImage() must return either a String fie path of the image
or an Image object.
Maybe you just want nameSearched.getImage() to return a String from an array of String paths. Not sure what nameSearched.getImage() actually does so I can't go into further explanation or correction

Exporting images with java program using NetBeans

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.

Categories

Resources