Java stretch icon fit button

I'm trying to resize my icon so that it covers the entire button and is in the center of the button When I try, it stretches my button and messes up everything else What shall I do? At present, my code is:

In the constructor of my class

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

In 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)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Debug,javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Console,javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)

Then I connect them all horizontally and vertically so that they are all the same size

Instead, it eventually looks like the following In addition, if I change the icon of the first button, all buttons will change size due to my constraints How do I fit icons to buttons?

Solution

Try something like this (if I'm not wrong with parentheses):

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

In this way, your image will be resized (in my example, the size is 64) × 64) and add to the button, as shown in the following example:

Edit:

This is a way to resize and maintain the image scale:

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)));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>