6

I'm trying to resize my icon so that it covers the whole button and sits in the center of the button. When I try, it stretches my button and messes up everything else. How can I do it? Currently, my code is:

In my constructor of a class..

javax.swing.JButton Console = new javax.swing.JButton;
ScaleButtonImage(Console, ConsoleEnabledImage);

Within that class..

private void ScaleButtonImage(javax.swing.JButton Button, java.awt.Image ButtonIcon) {
        double Width  = ButtonIcon.getWidth(Button);
        double Height = ButtonIcon.getHeight(Button);
        double xScale = 28/Width;//Button.getWidth() / Width;
        double yScale = 28/Height;//Button.getHeight() / Height;
        double Scale = Math.min(xScale, yScale);   //ToFit
        //double Scale = Math.max(xScale, yScale); //ToFill
        java.awt.Image scaled = ButtonIcon.getScaledInstance((int)(Scale * Width), (int)(Scale * Height), java.awt.Image.SCALE_SMOOTH);
        Button.setIcon(new javax.swing.ImageIcon(scaled));
    }

LAYOUT:

.addGroup(layout.createSequentialGroup()
                        .addComponent(Enable, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Graphics, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Debug, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Console, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

Then I link all of them horizontally and vertically so they're all the same size.

Instead, it ends up looking like the below. Also, if I change the icon of the first button, all the buttons change size because of my constrains. How do I make the icons fit the buttons?

enter image description here

Brandon
  • 22,723
  • 11
  • 93
  • 186
  • [please to check Q&A by @mre](http://stackoverflow.com/search?q=user:584862+[jbutton]), JButton haven't implemented any LayoutManager – mKorbel Dec 10 '12 at 22:43
  • Tried that already. The adding label image then adding that to the button doesn't work. I also tried implementing borderlayout in the button. Still does not work. – Brandon Dec 10 '12 at 23:03
  • I suspect there is something wrong in the way you are resizing your image (double xScale = 28/Width; double yScale = 28/Height; ...). This way, I think, your ratio will be 1:1 instead of original ratio like 4:3 or 16:9. – Branislav Lazic Dec 10 '12 at 23:33

2 Answers2

10

Try something like this (If I didn't make any mistake with brackets):

JButton button = new JButton(new ImageIcon(((new ImageIcon(
            "path-to-image").getImage()
            .getScaledInstance(64, 64,
                    java.awt.Image.SCALE_SMOOTH)))));

This way your image will be resized (in my case to size 64x64) and added to button, like in this example here:

enter image description here

EDIT:

This is the way to resize your image and to keep image ratio:

ImageIcon ii = new ImageIcon("path-to-image");
int scale = 2; // 2 times smaller
int width = ii.getIconWidth();
int newWidth = width / scale;
yourComponent.setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
            java.awt.Image.SCALE_SMOOTH)));
Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
0

This worked for me:

public class ImageButton
extends JButton {
    private static final long serialVersionUID = 1;

    /** @serial */
    private Image image;

    /** @serial */
    private final Rectangle innerArea = new Rectangle();

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (image != null) {
            SwingUtilities.calculateInnerArea(this, innerArea);

            g.drawImage(image,
                innerArea.x, innerArea.y, innerArea.width, innerArea.height,
                this);
        }
    }
}
VGR
  • 40,506
  • 4
  • 48
  • 63