I want to get the image from my workspace, and to show it in a GUI.
So what I did was to place the image in the workspace: Image
And then, I did that:
File coughfile = new File(cough.jpg);
But I get an error; cough can't be resolved to a type.
How do I import it?
ImageIcon yourImage = new ImageIcon("C:/path/to/your/file/cough.jpg");
JLabel yourLabel = new JLabel();
yourLabel.setLocation(x,y);
yourLabel.setSize(width, heigh);
yourLabel.setIcon(yourImage);
yourFrame.add(yourLabel);
Related
If I Use This Way To Add image in panel
static ImageIcon bg = new ImageIcon("hangman1.png");
Then Where Should Be Mine hangman1.png should be so it display correctly??
Load your "hangman1.png" file as a resource and add it to JLabel:
JLabel label = new JLabel(new ImageIcon(getClass().getResource("/path/to/your/image/hangman1.png")));
Before of that, add it into some package in your Java project.
I am not sure if I am referring to the right location with this code, the images I am trying to access are titled Flower0.png etc.
They are located in the same directory as the rest of my code for this project.
This class is in a src folder called hangman.ui and the .png files are located in a directory folder called Resources.
Perhaps getClass().getResource is not right?
This is my first time trying to put images into a GUI.
Help is much appreciated!
public WiltingFlowerRendererRemix(HangmanLogic logic)
{
panel = new JPanel();
panel.setLayout(new BorderLayout());
imageLabel = new JLabel();
panel.add(imageLabel, BorderLayout.CENTER);
int numberOfImages = 10;
images = new ImageIcon[numberOfImages];
for (int i = 0; i < numberOfImages; i++)
{
images[i] = new ImageIcon(getClass().getResource("Flower"+Integer.toString(i) + ".png"));
}
}
You say the images are in a folder called "Resources"? You can load images like this then:
BufferedImage image = ImageIO.read(getClass().getResource("/Resources/Flower0.png"));
ImageIcon icon = new ImageIcon(image);
To use it on the GUI you can use a JLabel.
JLabel label = new JLabel();
label.setIcon(icon);
And then add the label to a panel for example.
For me Works...
According to Maven:
https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
src/main/resources: Application/Library resources
Then I put the ICON in this Location:
PROJECTNAME\src\main\resources\usedPictures\
--Open.png
Clean and Build. And you can check here the location where the file will be get....
PROJECTNAME\target\classes\usedPictures\
--Open.png
Now the Example using the ICON:
button.setIcon(
new javax.swing.ImageIcon(
getClass().getClassLoader().getResource("usedPictures/Open.png")
)
);
I have images i want to add in the following folder: G:\my java\DesktopApplication1\build\classes\desktopapplication1\resources.
How to go about adding image in this folder to my labels or frames?
Once built, the image will typically be inside a Jar. The resources in a Jar can be accessed a number of ways, here is one.
URL urlToImage = this.getClass().getResource(
"/desktopapplication1/resources/the.png");
ImageIcon imageIcon = new ImageIcon(urlToImage);
JLabel ...
You can do something like this:
ImageIcon image = new ImageIcon("path_to_image");
JLabel label = new JLabel(image);
I did something like this:
JLabel l2=new JLabel("");
try {
Image img = ImageIO.read(getClass().getResource("resources/wait20.gif"));
l2.setIcon(new ImageIcon(img));
}
catch (IOException ex) {
}
It does work, but i would have liked it more had the GIF animation was displayed. Nevertheless, if a static image is to be displayed, this is the way to do it.
I try to load an image with:
ImageIcon imageIcon = new ImageIcon(url);
if I query the load status:
imageIcon.getImageLoadStatus();
it returns MediaTracker.ERRORED
Is there a way to get an error message saying what the problem was?
You can try loading the image via alternate methods. That might give you better feedback:
Image img = ImageIO.read(url);
ImageIcon icon = new ImageIcon(img);
How can I display jpg image which I stored in arraylist in JPanel?
Im not able to display the jpg files in the JPanel.
String[] pictureFile = {"A.jpg","B.jpg","C.jpg"};
List<String> picList1 = Arrays.asList(pictureFile);
Collections.shuffle(picList1);
ImageIcon icon = new ImageIcon("picList1.get(0)");
JLabel label1 = new JLabel();
label1.setIcon(icon);
JPanel panel = newJPanel;
panel.add(label);
You should not put the call to the array in quotes.
Instead, you should try the following:
ImageIcon icon = new ImageIcon(picList1.get(0));
The problem is in the line
ImageIcon icon = new ImageIcon("picList1.get(0)");
It's interpreting the string as a file name. You should just need to unquote the picList1.get(0) bit.