Add images into an array JPanel - java

I have made a chessboard out of JPanel. Using ImageIcon doesn't work, so I looked over the site, but all of it seems complicated, how do I add images to an array like
tiles[0][0].setIcon(br);
This is the JPanel that I created for the chessboard
private JPanel[][] tiles = new JPanel[6][6];
I have tried this:
ImageIcon bn = new ImageIcon("art/BN.gif");
ImageIcon bb = new ImageIcon("art/BB.gif");
ImageIcon br = new ImageIcon("art/BR.gif");
ImageIcon wn = new ImageIcon("art/WN.gif");
ImageIcon wb = new ImageIcon("art/WB.gif");
ImageIcon wr = new ImageIcon("art/WR.gif");
tiles[0][0].add(new JLabel(bn));
tiles[0][1].add(new JLabel(wn));
tiles[0][2].add(new JLabel(wb));
tiles[0][3].add(new JLabel(wb));
tiles[0][4].add(new JLabel(wn));
tiles[0][5].add(new JLabel(wr));
tiles[5][0].add(new JLabel(br));
tiles[5][1].add(new JLabel(bn));
tiles[5][2].add(new JLabel(bb));
tiles[5][3].add(new JLabel(bb));
tiles[5][4].add(new JLabel(bn));
tiles[5][5].add(new JLabel(br));
But it doesn't work

Where are your images being stored? What exactly doesn't work?
I'm going to take a shot in the dark and assume you're attempting to load files that are embedded in your application.
Taken from; https://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html
ImageIcon(String filename)
Creates an ImageIcon from the specified file.
ImageIcon(URL location)
Creates an ImageIcon from the specified URL.
Try this;
ImageIcon bn = new ImageIcon(getClass().getResource("art/BN.gif"));
It will attempt to create an ImageIcon from a URL returned by .getResource()

Related

How do i put a icon to jLabel

By default it has an image with the path /img/profileicon/29.png
but then from the main I want to change it, but for some reason instead of changing it disappears.
The code:
int iconId = summoner.getProfileIconId();
ImageIcon img = new javax.swing.ImageIcon("/img/profileicon/"+iconId+".png");
profileIconImg.setIcon(img);
Finally I was able to use this code, making use of class.getResource
URL iconUrl = EuwGG.class.getResource("/img/profileicon/"+profileIconId+".png");
Image profileImage = ImageIO.read(iconUrl);
ImageIcon profileIcon = new ImageIcon(profileImage);
Image i = profileIcon.getImage().getScaledInstance(125, 125, Image.SCALE_SMOOTH);
profileIcon = new ImageIcon(i);
profileIconImg.setIcon(profileIcon);

Error when I try to put an ImageIcon in Java

I tried to put a simple icon in a JPanel formatted with the BoxLayout.
JPanel panel_4 = new JPanel();
contentPane.add(panel_4, BorderLayout.CENTER);
panel_4.setLayout(new BoxLayout(panel_4, BoxLayout.X_AXIS));
ImageIcon seven= new ImageIcon("‪C:\\Users\\alewe\\workspace\\SlotMachine\\Lucky_Seven-128.png");
JLabel lblNewLabel_1 = new JLabel(seven);
panel_4.add(lblNewLabel_1);
When I ran the code it gave me the error "Some characters cannot be mapped using "Cp1252" character encoding", I saved by UTF-8, now it starts but I can't see the icon.
Maybe if you use setIcon will help you:
ImageIcon seven= new ImageIcon("‪C:\\Users\\alewe\\workspace\\SlotMachine\\Lucky_Seven-128.png");
JLabel lblNewLabel_1 = new JLabel();
//Set your icon to your label
lblNewLabel_1.setIcon(seven);
panel_4.add(lblNewLabel_1);
You can read more about icons here
you will need a inputstream to read the picture. use it like this:
File f = new File("filepath");
InputStream in=new FileInputStream(f);
if (in != null) {
ImageIcon imageIcon = new ImageIcon(ImageIO.read(in));
label.setIcon(imageIcon);
} else {
LOG.debug("No icon found...");
}

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

Rendering Image into label

I have a label. I want to render image into it. But the following code would do anything.
CardLayout cl = (CardLayout) (mainPanel.getLayout());
cl.show(mainPanel, "newPersonaCard");
BufferedImage myPicture = ImageIO.read(new File("C:\\Desktop\\Documents\\Pictures\\always.jpg"));
ImageIcon icon = new ImageIcon(myPicture);
icon.getImage().flush();
I am using netbean designer.
You are right, in some cases there issue with repainting Icon in the JLabel, then you have to call,
myIcon.getImage().flush();
myLabel.setIcon(myIcon);
rest of methods is implemented in the Icon and JLabel correctly
.
.
File file = fileChooser.getSelectedFile();
JLabel label = new JLabel();
ImageIcon icon = new ImageIcon(file.getAbsolutePath());
label.setIcon(icon);
//add label to panel
fileChooser.showDialog(saveBtn2, null);
File file = fileChooser.getSelectedFile();
System.out.println("The path to file "+file.getAbsolutePath());
ImageIcon icon = new ImageIcon(file.getAbsolutePath());
pictureLbl.setIcon(icon);

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