P
P
P_Alexander2016-12-06 13:13:42
Java
P_Alexander, 2016-12-06 13:13:42

How to adjust image size to button size?

When uploading an image to a button, only part of the image is visible, how can I adjust the size of the image to fit the button?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-12-08
@P_Alexander

If the size of the button does not change while the application is running and is known in advance, then everything is relatively simple:

JButton button = new JButton(
    new ImageIcon("/path/to/image.png")
        .getImage()
        .getScaledInstance(buttonHeight, buttonWidth, java.awt.Image.SCALE_SMOOTH)
);

And for automatic scaling, you need to do a hell of a lot, however:
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestButton {
    public static void main(String[] args) {
        new TestButton();
    }

    private BufferedImage master;

    public TestButton() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                try {
                    master = ImageIO.read(new File("/path/to/image.png"));

                    JButton btn = new JButton() {
                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(90, 50);
                        }
                    };

                    btn.addComponentListener(new ComponentAdapter() {
                        @Override
                        public void componentResized(ComponentEvent e) {
                            JButton btn = (JButton) e.getComponent();
                            Dimension size = btn.getSize();
                            Insets insets = btn.getInsets();
                            size.width -= insets.left + insets.right;
                            size.height -= insets.top + insets.bottom;
                            if (size.width > size.height) {
                                size.width = -1;
                            } else {
                                size.height = -1;
                            }
                            Image scaled = master.getScaledInstance(size.width, size.height, java.awt.Image.SCALE_SMOOTH);
                            btn.setIcon(new ImageIcon(scaled));
                        }
                    });

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(btn);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException exp) {
                    exp.printStackTrace();
                }
            }
        });
    }
}

6XbXW.gif

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question