I am trying to get to the url of an Image.
Icon icon = getIcon(); // gets a previously initialised Icon
ImageIcon imageIcon = (ImageIcon) icon;
Image image = imageIcon.getImage();
image.getSource(); // returns a java.awt.image.FilteredImageSource
The FilteredImageSource has a property src, which contains the url of the image.
However the src property is with default access modifier.
Is there any way I can get the image url at this point in time ?
Related
I want to initialize an ImageIcon with an offset. The resizing is already working properly, but how can I tell the resized image icon to map to a certain x and y coordinate?
I have a map in my project and the attached snippet is the initialization of the image icon.
The image icon is then printed to a JLabel with a static size and position.
ImageIcon imageIcon = createImageIcon("/mapEnd1.png", "Map");
Image image = imageIcon.getImage();
Image newimg = image.getScaledInstance(600, 800, java.awt.Image.SCALE_SMOOTH);
imageIcon = new ImageIcon(newimg);
So I have this code It's work when the image at my desktop, I added the image at src file put I couldn't convert it can you tell me what is the problem? this the code it set the image to fit the label too:
public void ScalImage() {
ImageIcon image = new ImageIcon("C:\\Users\\HP\\Desktop\\ath3.png");
Image img = image.getImage();
Image imgScale = img.getScaledInstance(jLabel2.getWidth(), jLabel2.getHeight(), Image.SCALE_SMOOTH);
ImageIcon scaliedicon = new ImageIcon(imgScale);
jLabel2.setIcon(scaliedicon);
}
I tried to say: ImageIcon image = new ImageIcon("ath3.png");
didn't work
So I have solved it I wrote like that
ImageIcon image = new ImageIcon(getClass().getResource("ath3.png"));
It's work for me. I wish I'm right
I am trying to make a mobile app using Java and codename one plugin. My question is - what's the simples way to populate an image from a URL to a label? I Googled it and all I found was this piece of code:
Image i = URLImage.createToStorage(placeholder, "fileNameInStorage", "http://xxx/myurl.jpg", URLImage.RESIZE_SCALE);
But I have no idea how to use it. What is a placeholder? It asks an EncodedImage parameter, but if I do:
EncodedImage image = new EncodedImage(10, 10);
I get the error that EncodedImage is protected.
I simply want to populate an image from a URL to my desired label in a form.
I am using GUI builder.
The placeholder image is the image that should show while the image from URL is being downloaded and it's an EncodedImage.
If your Label already have an icon as a placeholder, you can use its icon, otherwise you can create a new placeholder image. Below are 3 options to create an EncodedImage and a URLImage usage example:
Method 1:
//generate a grey placeholder that matches the size of the label's icon
Image placeholder = Image.createImage(label.getIcon().getWidth(), label.getIcon().getWidth(), 0xbfc9d2);
EncodedImage encImage = EncodedImage.createFromImage(placeholder, false);
Method 2:
//Convert the label icon to EncodedImage
EncodedImage encImage = (EncodedImage)label.getIcon();
Method 3:
//Create a fresh grey EncodedImage when label doesn't have any icon set initially
int deviceWidth = Display.getInstance().getDisplayWidth();
Image placeholder = Image.createImage(deviceWidth / 10, deviceWidth / 10, 0xbfc9d2); //square image set to 10% of screen width
EncodedImage encImage = EncodedImage.createFromImage(placeholder, false);
Usage Example:
It's a good practice to use the URL as the cached image name in storage. If you have a multiple sizes of the same image, just prefix them with a unique string like "Large" + URL
label.setIcon(URLImage.createToStorage(encImage, "Medium_" + "http://xxx/myurl.jpg", "http://xxx/myurl.jpg", URLImage.RESIZE_SCALE));
I've been wondering how can I set an ImageIcon to a button using the Image path and set it to a JButton?
I can resize the image's size according to the buttons size:
frontViewImageFile = fc.getSelectedFile();
MainFrame.btnFrontView.setIcon(new ImageIcon(ImageIO.read(
frontViewImageFile).getScaledInstance(150, 150, Image.SCALE_SMOOTH)));
But the image came from a file chooser and I can use getScaledInstance method to resize the picture.
How can i do this with image path since method getScaledInstance is undefined for the type String?
ImageIcon icon = ...;
JButton b = ...;
Image im = icon.getImage();
Image im2 = im.getScaledInstance(b.getWidth(), b.getHeight(), ...);
b.setIcon(new ImageIcon(im2));
I m making a java rmi based application where i pass and receive an ImageIcon object from the server...
(the image is stored in a separate URL in server)
The function involves the following....
1. Getting the image from the server at first....(on button press A)
2. Replacing it with a image file in the client[optional]....(on button press B)
3. Remove the image with a default image[optional]....(on button press C)
4. Sending it back to the Server....................(On button press D).....
Here the image is displayed in ajlabel calledimg_label
The codes i've used are as follows.....
Variables used
java.awt.Image img;
javax.swing.ImageIcon CurrentImageIcon;
javax.swing.ImageIcon DefaultImageIcon;
// CurrentImageIcon contains the image to be displayed in the img_label....
// img is used for copying as well for scaling......
// DefaultImageIcon holds the default Image......
On Button Press A
img = temp.getImage();
CurrentImageIcon = new ImageIcon(img);
// Assuming temp holds the ImageIcon taken from the server.......
img=img.getScaledInstance(83,85 , Image.SCALE_DEFAULT);
img_label.setIcon(new ImageIcon(img));
img_label.revalidate();
img_label.repaint();
On Button Press B
String url_text = jTextField.getText(); // taking the url frm the field.....
CurrentImageIcon = new ImageIcon(url_text);
img=CurrentImageIcon.getImage();
img=img.getScaledInstance(83,85 , Image.SCALE_DEFAULT);
img_label.setIcon(new ImageIcon(img));
img_label.revalidate();
img_label.repaint();
On Button Press C
img = DefaultImageIcon.getImage();
CurrentImageIcon = new ImageIcon(img);
img=img.getScaledInstance(83,85 , Image.SCALE_DEFAULT);
img_label.setIcon(new ImageIcon(img));
img_label.revalidate();
img_label.repaint();
On Button Press D
// ImagetoSend is an ImageIcon to be sent to the Server.....
ImagetoSend = CurrentImageIcon;
CurrentImageIcon = null;
Now the problem i m getting is a weird one......
The image is getting embedded as i wanted on repainting when i click this button........
But when i download the recently uploaded image next time on Button press A....it is displayed either magnified or reduced to size even though i included the getScaledInstance method....
like this...
The image i am handling is a jpg image....
I even checked the image at Server directory.....No size change has occured on that file which was uploaded from client to server. But the change is observed when it is downloaded and embedded to the jlabel...
Can Anyone help me to sort out this issue...??
Somehow it is getting scaled down twice. Either you scale it down then send it to the server where it is scaled down, or the both button clicks scale it. Eliminate the scale down code on the server side and see what happens.