Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Icon resizeIcon(final Icon icon, final int newWidth, final Component targetComponent) {
- try {
- final BufferedImage image = iconToImage(icon, targetComponent);
- final int w = icon.getIconWidth(), h = icon.getIconHeight();
- if (w == newWidth) return icon;
- final int newHeight = (int) round(h / (w / ((double) newWidth)), 0);
- final BufferedImage bi = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
- final Graphics2D g = (Graphics2D) bi.getGraphics();
- final HashMap<Key, Object> hints = new HashMap<Key, Object>(7);
- hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- hints.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
- hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
- hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
- hints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
- hints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
- hints.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
- g.addRenderingHints(hints);
- g.drawImage(image, 0, 0, newWidth, newHeight, null);
- return new ImageIcon(bi);
- } catch (final Exception e) {
- return icon;
- }
- }
- public static BufferedImage iconToImage(final Icon icon, final Component targetComponent) {
- final int w = icon.getIconWidth();
- final int h = icon.getIconHeight();
- GraphicsConfiguration gc = targetComponent.getGraphicsConfiguration();
- if (gc == null) gc = Frame.getFrames()[0].getGraphicsConfiguration();
- final BufferedImage image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
- final Graphics2D g = image.createGraphics();
- icon.paintIcon(targetComponent, g, 0, 0);
- g.dispose();
- return image;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement