Adding Image In JFrame : Java - java

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.

Related

How do I get an image path?

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

Java getClass().getResource on a png returning Null Pointer

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

how to add image from spectified package into label, frames etc

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.

how to put Image on JPanel using Netbeans

And how do I put Image on a JPanel using Netbeans?
Have a look at this tutorial: Handling Images in a Java GUI Application
At the same time you could code as well:
JPanel panel = new JPanel();
ImageIcon icon = new ImageIcon("image.jpg");
JLabel label = new JLabel();
label.setIcon(icon);
panel.add(label);
You can use ImageIcon and JLabel:
ImageIcon icon = createImageIcon("image.gif", "sample image");
// Label with text and icon
JLabel label1 = new JLabel("Image and Text", icon, JLabel.CENTER);
// Label with icon only
JLabel label2 = new JLabel(icon);
// Add to your JPanel
panel.add(label1);
panel.add(label2);
1) First Paste the image to the Application folder
2) Add a label to the panel
3) Right click label-> Properties
4) Select Icon-> more option
5) Select Package and File and Click oK
6) U R DONE :)
You have to set a JLabel with your icon property set to the picture.
For a more detailed solution to the particular problem, go to
http://www.netbeanstutorials.com/p/handling-images.html
There's even a video how to do it there in case you need it.

Displaying jpg image on JPanel

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.

Categories

Resources