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);
Related
This is the code
btnVoltar.setIcon(new ImageIcon(AdicionarRefeiĆ§Ć£o1.class.getResource("/icons/Back Icon.png")));
I want to resize it so it fits on a label, but its way too big.
You can try this codes here, but then again this post is a duplicate from stackoverflow
private ImageIcon image; // where in your case it's your class resource
Image IMG = image.getImage(); // transform it
Image newimg = IMG.getScaledInstance(200, 200, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
//Change the 200,200 to the number you prefer to resize it to
image = new ImageIcon(newimg); // transform it back
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()
This is my code thus far
ImageLoader saver = new ImageLoader();
saver.data = new ImageData[]
{ toSave.getImageData() };
saver.save(fileName, SWT.IMAGE_PNG);
toSave is an image that was loaded using the SWTResourceManager that is transparent in the program. fileName is a string representing the file I want to save the image to (ends with .png)
The result is an image with a black background instead of a transparent background that I want. How do I make the background transparent? I think it has something to do with the transparency mask, but I could be wrong.
Thanks in advance!
SWTResourceManager seems to be causing your problem. I would not recommend to use it.
Try this code, it works for me:
Display d = Display.getDefault();
Image image = new Image(d, "/pictures/Llama.gif");
ImageLoader saver = new ImageLoader();
saver.data = new ImageData[] { image.getImageData() };
saver.save("output.png", SWT.IMAGE_PNG);
image.dispose();
Remember to dispose the Image when you don't need it anymore.
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);
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);