user542719
user542719

Reputation: 307

how to stretch image

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

Answers (2)

Ranjan Singh rinku
Ranjan Singh rinku

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

dogbane
dogbane

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

Related Questions