Reputation: 191
Method 'insertPhoto' will be used to add the functionality of inserting User Photo image to the specific Label. I've been reading and trying to find why this won't work before I decided to ask for help.
Description of method: Method insertPhoto has three JLabels passed as an argument where one is an insertImage thumbnail, the other is deleteImage and the last one is the actual photo with an absolute path to it. Its format is jpg and I want to scale it to 200x200px so it will fit my window. I used ImageIO.read to get a bufferedImage so I am sure it is fully loaded. The algorithm I used for scaling is SCALE_SMOOTH.
My question is, how can I make it work, is the problem that the image is not yet fully loaded or should I use some other technique such as repainting to scale it according to my needs.
Here is the method:
public void insertPhoto(JLabel l_insert, JLabel l_delete, JLabel l_photo) {
JFileChooser jfc = new JFileChooser();
jfc.setFileFilter(new FileNameExtensionFilter("JPG files", "jpg"));
int window = jfc.showOpenDialog(null);
if (window == jfc.APPROVE_OPTION) {
l_insert.setIcon(null);
l_delete.setIcon(null);
String filePath = jfc.getSelectedFile().getAbsolutePath();
try {
Image photo = ImageIO.read(new File(filePath));
photo.getScaledInstance(200, 200, Image.SCALE_SMOOTH);
ImageIcon im = new ImageIcon(photo);
l_photo.setIcon(im);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
}
Upvotes: 0
Views: 38
Reputation: 37875
You never assign the result of the scaling operation to anything:
photo.getScaledInstance(200, 200, Image.SCALE_SMOOTH);
// ^^^
// Ignoring the return value.
ImageIcon im = new ImageIcon(photo);
It should be e.g.:
photo = photo.getScaledInstance(200, 200, Image.SCALE_SMOOTH);
ImageIcon im = new ImageIcon(photo);
Upvotes: 3