'How to Resize an Image in Swing?
I've learnt how to add an image in Javax swing a lot of times. I've used containers and all imageicons, however the problem is that my photos are too large to fit in. They are above 1800*1700 pixels in size and the screen obviously can't accomodate them. I would request you guys to suggest a way to scale the image down. Just FYI, I've tried most of these methods like using awt Image, setBounds, SCALE_DEFAULT and stuff. For some reason, my IDE, BlueJ doesn't seem to work with a lot of the methods which are suggested, still I would request you to please help me. Here is the code:
import javax.swing.*;
import java.awt.*;
class JLabelDemo
{
public JLabelDemo()
{
JFrame jfrm=new JFrame("Image from book");
jfrm.setLayout(new FlowLayout());
jfrm.setDefaultCloseOperation(jfrm.EXIT_ON_CLOSE);
jfrm.setSize(500,700);
ImageIcon ii=new ImageIcon("D:\\Dhruv Rana\\UNATIS Mandarmoni Trip\\Sunrise.jpg");
JLabel jl=new JLabel("Background",ii,JLabel.CENTER);
jfrm.add(jl);
jfrm.setVisible(true);
}
public static void main(String [] args)
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
new JLabelDemo();
}
}
);
}
}
Solution 1:[1]
put it in an bufferedImage
Graphics2D grph = image.createGraphics();
grph.scale(2.0, 2.0);
grph.dispose();
Or look at How to scale a BufferedImage
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Medrik |