Resizing imaget to scale off the panel it is in/ label

60 views Asked by At

I have been able to make fonts scale with the size of the component, however, I cant seem to figure out how to make an image scale. The way my program works is the components are added to an arrayList and then at the end the bounds for each of the components are set. I tried variations of this and have been unable to get it to work:

public class ImagePanel extends JPanel implements Updater{
    BufferedImage resizer;
    private int width;
    private int height;
    private JLabel picLabel;
    private BufferedImage myPicture;
    /**
     * Searches for the image in the specified location and sets the background of the ImagePanel to the specified color.
     * @param location
     * @param bGColor
     */
    public ImagePanel(String location, Color bGColor)
    {

        myPicture = null;
        this.setLayout(new BorderLayout());
        try {
            // TODO make it use the string.
            myPicture = ImageIO.read(new File("images/logo-actavis.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        picLabel = new JLabel(new ImageIcon(myPicture));
        add(picLabel,BorderLayout.CENTER);
         this.addComponentListener(new ComponentAdapter() {
                @Override
                /**
                 *  Makes it so it does not stretch out text. Resizes the fonts to scale with the screen width..
                 */
                public void componentResized(ComponentEvent e) {
                    if(picLabel.getHeight()!=0&&picLabel.getWidth()!=0)
                     {
                      width = picLabel.getWidth();
                      height = picLabel.getHeight();
                      myPicture=resize(myPicture, width, height);
                     }

                }
            });


        this.setBackground(bGColor);
    }
    public static BufferedImage resize(BufferedImage image, int width, int height) {
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
        Graphics2D g2d = (Graphics2D) bi.createGraphics();
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
        g2d.drawImage(image, 0, 0, width, height, null);
        g2d.dispose();
        return bi;
    }
}
3

There are 3 answers

0
StanislavL On

You can use the method to get scaled image from the original one

public Image getScaledInstance(int width, int height, int hints)

0
Cole Gordon On

Ended up doing this:

     this.addComponentListener(new ComponentAdapter() {
            @Override
            /**
             *  Makes it so it does not stretch out text. Resizes the fonts to scale with the screen width..
             */
            public void componentResized(ComponentEvent e) {
                Double width = (double) e.getComponent().getWidth();
                Double height = (double) e.getComponent().getHeight();
                double scalingFactor=Math.min(width/originalWidth, height/originalHeight)*.7;
                myPicture = myPicture.getScaledInstance((int)(originalWidth*(scalingFactor)),(int) (originalHeight*(scalingFactor)), Image.SCALE_SMOOTH);
                picLabel = new JLabel(new ImageIcon(myPicture));
                add(picLabel,BorderLayout.CENTER);

            }
     });

Where original height/ width are the aspect ratios of the original picture. I multiplied the scalingFactor by .7 because I did not want it to fill the whole Panel.

0
camickr On

Instead of using an ImageIcon. You can use Darryl's Stretch Icon.

The Icon will automatically scale depending on space available to the label. You can have the image scale proportionally or have the image fill then entire space.