/* * Original code Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved. * Taken from ImageIcon example. * http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html * * Modified by Brad Broerman 3/2009 to scale images for icons used in JFileChooser. */ import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.net.URL; import javax.swing.ImageIcon; /* Utils.java is used by FileChooserDemo2.java. */ public class Utils { public final static String jpeg = "jpeg"; public final static String jpg = "jpg"; public final static String gif = "gif"; public final static String tiff = "tiff"; public final static String tif = "tif"; public final static String png = "png"; /* * Get the extension of a file. */ public static String getExtension(File f) { String ext = null; String s = f.getName(); int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) { ext = s.substring(i+1).toLowerCase(); } return ext; } /** Returns an ImageIcon, or null if the path was invalid. */ protected static ImageIcon createImageIcon(URL imgURL) { if (imgURL != null) { ImageIcon icon = new ImageIcon(imgURL); double aspectRatio = (double)icon.getIconWidth() / (double)icon.getIconHeight(); return new ImageIcon(getScaledImage(icon.getImage(), (int)Math.round(64 * aspectRatio), 64)); } else { System.err.println("Couldn't find file: " + imgURL.toExternalForm()); return null; } } private static Image getScaledImage(Image srcImg, int w, int h){ BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = resizedImg.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(srcImg, 0, 0, w, h, null); g2.dispose(); return resizedImg; } }