Resize a picture to fit a JLabel

后端 未结 8 551
不思量自难忘°
不思量自难忘° 2020-11-29 01:56

I\'m trying to make a picture fit a JLabel. I wish to reduce the picture dimensions to something more appropriate for my Swing JPanel.

I tried with setPreferredSiz

8条回答
  •  -上瘾入骨i
    2020-11-29 02:46

    Or u can do it this way. The function u put the below 6 lines will throw an IOException. And will take your JLabel as a parameter.

    BufferedImage bi=new BufferedImage(label.width(),label.height(),BufferedImage.TYPE_INT_RGB);
    
    Graphics2D g=bi.createGraphics();
    
    Image img=ImageIO.read(new File("path of your image"));
    
    g.drawImage(img, 0, 0, label.width(), label.height(), null);
    
    g.dispose();
    
    return bi;
    

提交回复
热议问题