Java Stretch Icon for Button Installation

I'm trying to resize the icon so that it spans the entire button and is in the center of the button. When I try, it stretches my button and ruins everything else. How can I do it? My current code is:

In my class constructor ..

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

Inside this 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)); } 

VIEW:

 .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 will knit them all horizontally and vertically so that they are the same size.

Instead, it looks like this. Also, if I change the icon of the first button, all buttons are resized due to my limitations. How to make icons suitable for buttons?

enter image description here

+6
source share
2 answers

Try something like this (if I was not mistaken in brackets):

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

Thus, your image will be resized (in my case, to the size of 64x64) and added to the button , as in this example:

enter image description here

EDIT:

This is a way to resize the image and keep the 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))); 
+10
source

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); } } } 
0
source

All Articles