java-org.hwo.ui/src/org/hwo/ui/JIconButton.java

54 lines
1.1 KiB
Java

package org.hwo.ui;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class JIconButton extends JButton{
/**
*
*/
private static final long serialVersionUID = 1L;
private ImageIcon icon;
private int iconWidth, iconHeight;
public JIconButton(){
super();
}
public JIconButton(ImageIcon icon) {
super(icon);
this.icon = icon;
this.iconWidth = icon.getIconWidth();
this.iconHeight = icon.getIconHeight();
}
public Dimension getIconDimension() {
return new Dimension(this.iconWidth, this.iconHeight);
}
public void setIconDimension(Dimension dim) {
resizeIcon(dim.width, dim.height);
}
private void resizeIcon(int width, int height) {
if(this.icon != null) {
Image img = this.icon.getImage();
BufferedImage bimg = new BufferedImage(this.iconWidth, this.iconHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bimg.createGraphics();
g.drawImage(img, 0, 0, width, height, null, null);
this.icon = new ImageIcon(bimg);
setIcon(this.icon);
}
}
}