From 7e95a9d4f0fa6d83b157835941e4028aae4f4fc2 Mon Sep 17 00:00:00 2001 From: Niclas Thobaben Date: Sat, 10 Feb 2018 12:10:53 +0100 Subject: [PATCH] added JIconButton added JIconButton --- src/org/hwo/ui/JIconButton.java | 53 +++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/org/hwo/ui/JIconButton.java diff --git a/src/org/hwo/ui/JIconButton.java b/src/org/hwo/ui/JIconButton.java new file mode 100644 index 0000000..6624aa8 --- /dev/null +++ b/src/org/hwo/ui/JIconButton.java @@ -0,0 +1,53 @@ +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); + } + } + + +}