Java Swing + ImageIcons



  • Hi,

    I've been reading wtf for a while but this is my first time posting so I hope I don't come over too stupid!

    Is there any way I can dynamically resize a png image before putting it onto a JLabel?

    Thanks.
     



  • It's been a while since I've done any image manipulation.

    But, give this a try;

    Image image = Toolkit.getDefaultToolkit().getImage(<font color="#0066ff">"image.png"</font>);

    // attach observer, and make sure it's loaded.

    // image.getHeight(null), getWidth(null) might be useful...

    Image scaledImage= image.getScaledInstance(newHeightInPixels, newWidthInPixels, Image.SCALE_DEFAULT)

     ImageIcon icon = new ImageIcon(scaledImage);

     


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.