|
Java™ by example!
|
|
|
How can I specify a JButton's icon height and width?
Main.java:
import java.awt.*; import java.awt.geom.*; import java.awt.image.*; public class Main { public static ImageIcon zoom(ImageIcon imI, double width, double height) { Image source = imI.getImage(); BufferedImage dest = new BufferedImage( (int) (imI.getIconWidth() * width), (int) (imI.getIconHeight() * height), BufferedImage.TYPE_INT_RGB); AffineTransform ta = new AffineTransform(); ta.scale(width, height); Graphics2D g2d = dest.createGraphics(); g2d.drawImage(source, ta, null); g2d.dispose(); return new ImageIcon(dest); } public static void main(String args[]) { JFrame f = new JFrame(); JButton b = new JButton("test", new ImageIcon("button1.jpg")); b.setIcon(Main.zoom((ImageIcon) b.getIcon(), 0.5, 0.8)); f.getContentPane().add(b); f.setSize(150, 150); f.setVisible(true); } }
|
Further Information
Author of answer: Uwe Billen
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|