Resizing Icon to Fit on Jbutton in Java

resizing a ImageIcon in a JButton

   Image img = icon.getImage() ;  
Image newimg = img.getScaledInstance( NEW_WIDTH, NEW_HEIGHT, java.awt.Image.SCALE_SMOOTH ) ;
icon = new ImageIcon( newimg );

from http://www.coderanch.com/t/331731/GUI/java/Resize-ImageIcon

Resizing icon to fit on JButton in Java?

There are any number of issues. To start with, all Swing components DON'T auto scale images. Sure, might be a nice idea, but given the amount of time and processing required to do it efficiently, I understand why they don't, so you need to do all the work...

You should also remember, that the size of a component is not determined until it is laid out. And while you can provide all the sizing hints you might like, the layout manager is well within its rights to ignore one or more of these hints.

Instead of "hoping" you know the size of the button, you should make use of the ComponentListener API to receive notifications of when the component is actually resized...

Auto resizable icon

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("C:\\svg\\Revert 256x256.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();
}
}
});
}

}

Note: This example is far from optimised, but simply provides a broad concept of a possible solution...

Now, a word of warning. Image#getScaledInstance is neither the fastest or greatest of scaling algorithms...

Take a look at...

  • The Perils of Image.getScaledInstance()
  • Quality of Image after resize very low -- Java

for more details...

Fit size of an ImageIcon to a JButton

You can do it this way by simply adding a little method to your project:

private static Icon resizeIcon(ImageIcon icon, int resizedWidth, int resizedHeight) {
Image img = icon.getImage();
Image resizedImage = img.getScaledInstance(resizedWidth, resizedHeight, java.awt.Image.SCALE_SMOOTH);
return new ImageIcon(resizedImage);
}

Now, to use this method in your example code:

JFrame frame2 = new JFrame("Tauler Joc");
JPanel panell = new JPanel();
ImageIcon icon = new ImageIcon("king.jpg");
JButton jb= new JButton();
jb.setBounds(200,200,700,700);
panell.add(jb);

// Set image to size of JButton...
int offset = jb.getInsets().left;
jb.setIcon(resizeIcon(icon, jb.getWidth() - offset, jb.getHeight() - offset));

frame2.add(panell);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

If you just want the image and no border just set the offset variable to 0 or get rid of the offset variable altogether.

Auto-resizing JButton Icon

in Swing you can add any JComponent to the another JComponent, for Image is JLabel the best JComponent, then why not put the JLabel#setIcon() to the JButton

Sample Image Sample Image

import java.awt.*;
import javax.swing.*;

public class ResizeIconInButton extends JFrame {
private static final long serialVersionUID = 1L;

public ResizeIconInButton() {
JButton myButton = new JButton();
myButton.setLayout(new BorderLayout());
myButton.add(new CustomComponents0());
add(myButton, BorderLayout.CENTER);
setPreferredSize(getPreferredSize());
setTitle("Resize Icon In Button");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}

public static void main(String[] args) {
Runnable r = new Runnable() {

@Override
public void run() {
ResizeIconInButton main = new ResizeIconInButton();

}
};
javax.swing.SwingUtilities.invokeLater(r);
}
}

class CustomComponents0 extends JLabel {

private static final long serialVersionUID = 1L;

@Override
public Dimension getMinimumSize() {
return new Dimension(200, 100);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}

@Override
public void paintComponent(Graphics g) {
int margin = 10;
Dimension dim = getSize();
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
}
}

EDIT:

import java.awt.*;
import javax.swing.*;

public class ResizeIconInButton extends JFrame {

private static final long serialVersionUID = 1L;
private static final String IMAGE_PATH = "http://duke.kenai.com/misc/Bullfight.jpg";
private JButton myButton = new JButton();
private JLabel myLabel = new JLabel();

public ResizeIconInButton() {
Icon myIcon = new ImageIcon(IMAGE_PATH);
myLabel.setIcon(myIcon);
myButton.setLayout(new BorderLayout());
myButton.add(myLabel);
add(myButton, BorderLayout.CENTER);
setPreferredSize(new Dimension(200, 100));
setTitle("Resize Icon In Button");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}

public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
ResizeIconInButton main = new ResizeIconInButton();
}
});
}
}

How to set an ImageIcon to a JButton and resize the picture according to the button's size?

ImageIcon icon = ...;
JButton b = ...;
Image im = icon.getImage();
Image im2 = im.getScaledInstance(b.getWidth(), b.getHeight(), ...);
b.setIcon(new ImageIcon(im2));

automatic resizing of jbutton icon in gridlayout

You might be able to use Darryl's Stretch Icon.



Related Topics



Leave a reply



Submit