Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Stretch Icon to fit button

Tags:

java

swing

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

like image 418
Brandon Avatar asked Dec 10 '12 22:12

Brandon


1 Answers

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)));
like image 120
Branislav Lazic Avatar answered Oct 30 '22 16:10

Branislav Lazic