Reputation: 307
i want to strech image by using Graphics but unable here is my code it shows image in size that i want but not strach the image
void imageload () {
FileDialog fd = new FileDialog(MainFram.this,"Open", FileDialog.LOAD);
fd.show();
if(fd.getFile() == null){
//Label1.setText("You have not chosen any image files yet");
}else{
String d = (fd.getDirectory() + fd.getFile());
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image1 = toolkit.getImage(d);
saveImage = d;//if user want to save Image
ImageIcon icon=new ImageIcon(Image1);
lblImage.setIcon(icon);
lblImage.setMinimumSize(new Dimension(50, 70));
lblImage.repaint();
}
}
Upvotes: 7
Views: 14732
Reputation: 21
TO set background image from filchooser
final JFileChooser fc = new JFileChooser();
int r = fc.showOpenDialog(this);
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if (r == JFileChooser.APPROVE_OPTION) {
String name = fc.getSelectedFile().getAbsolutePath();
JOptionPane.showMessageDialog(null,"ADDED successfully");
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage(name);
Image scaledImage = image.getScaledInstance(1366, 768, Image.SCALE_DEFAULT);
ImageIcon icon=new ImageIcon(scaledImage);
my.jLabel10.setIcon(icon);}
Upvotes: 2
Reputation: 274562
Call getScaledInstance()
to scale the image to the size you want before you create the ImageIcon
. You don't need to call setMinimumSize
on the label.
Image image = toolkit.getImage("pic.jpg");
Image scaledImage = image.getScaledInstance(50, 70, Image.SCALE_DEFAULT);
ImageIcon icon=new ImageIcon(scaledImage);
Upvotes: 7