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);
Related
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);
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);
I'm trying to display both image from the web and text in JLabel only the image will show.
jlabel.setText("Hello" + "http://");
Try:
URL url = new URL("http://www.url.com/image.jpg");
Image image = ImageIO.read(url);
jlabel.setIcon(new ImageIcon(image));
jlabel.setText("the text");
JLabel has a setIcon method which you can use to set the image, pass it an ImageIcon, which can be created from an URL:
jlabel.setIcon(new ImageIcon(new URL("http:/...")));
jlabel.setText("Hello");
You can also include an image through HTML directly:
jlabel.setText("<html><img src=\"http://...\"></html>");
I have a an Image Icon generated in my code which i place it as an icon on a label as per the following code:
ImageIcon icon = new ImageIcon(barcode.drawBarcode());
jLabel36.setIcon(icon);
Now my problem is that how can i change the ImageIcon type to Image and save it on the hard disk. when i try to type cast ImageIcon to Image i get the following error :
java.lang.ClassCastException: javax.swing.ImageIcon cannot be cast to java.awt.Image
Can anyone suggest me how can i achieve this task both type casting and saving the image.
Just use getImage():
// get image from imageicon
Image image = icon.getImage();
// cast it to bufferedimage
BufferedImage buffered = (BufferedImage) image;
try {
// save to file
File outputfile = new File("saved.png");
ImageIO.write(buffered, "png", outputfile);
} catch (IOException e) {
e.printStackTrace();
}
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.