项目要用,网上找的
出处:
1 package ImageAPI; 2 import javax.swing.*; 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.beans.*; 6 import java.io.File; 7 8 /** 9 * @author matthew 10 * 11 */ 12 public class ImagePreviewPanel extends JPanel implements PropertyChangeListener { 13 14 private int width, height; 15 16 private ImageIcon icon; 17 18 private Image image; 19 20 private static final int ACCSIZE = 155; 21 22 private Color bg; 23 24 public ImagePreviewPanel() { 25 setPreferredSize(new Dimension(ACCSIZE, -1)); 26 bg = getBackground(); 27 } 28 29 public void propertyChange(PropertyChangeEvent e) { 30 String propertyName = e.getPropertyName(); 31 32 // Make sure we are responding to the right event. 33 if (propertyName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) { 34 File selection = (File) e.getNewValue(); 35 String name; 36 37 if (selection == null) 38 return; 39 else 40 name = selection.getAbsolutePath(); 41 42 /* 43 * Make reasonably sure we have an image format that AWT can handle 44 * so we don't try to draw something silly. 45 */ 46 if ((name != null) && name.toLowerCase().endsWith(".jpg") 47 || name.toLowerCase().endsWith(".jpeg") 48 || name.toLowerCase().endsWith(".gif") 49 || name.toLowerCase().endsWith(".png")) { 50 icon = new ImageIcon(name); 51 image = icon.getImage(); 52 scaleImage(); 53 repaint(); 54 } 55 } 56 } 57 58 private void scaleImage() { 59 width = image.getWidth(this); 60 height = image.getHeight(this); 61 double ratio = 1.0; 62 63 /* 64 * Determine how to scale the image. Since the accessory can expand 65 * vertically make sure we don't go larger than 150 when scaling 66 * vertically. 67 */ 68 if (width >= height) { 69 ratio = (double) (ACCSIZE - 5) / width; 70 width = ACCSIZE - 5; 71 height = (int) (height * ratio); 72 } else { 73 if (getHeight() > 150) { 74 ratio = (double) (ACCSIZE - 5) / height; 75 height = ACCSIZE - 5; 76 width = (int) (width * ratio); 77 } else { 78 ratio = (double) getHeight() / height; 79 height = getHeight(); 80 width = (int) (width * ratio); 81 } 82 } 83 84 image = image.getScaledInstance(width, height, Image.SCALE_DEFAULT); 85 } 86 87 public void paintComponent(Graphics g) { 88 g.setColor(bg); 89 90 /* 91 * If we don't do this, we will end up with garbage from previous images 92 * if they have larger sizes than the one we are currently drawing. 93 * Also, it seems that the file list can paint outside of its rectangle, 94 * and will cause odd behavior if we don't clear or fill the rectangle 95 * for the accessory before drawing. This might be a bug in 96 * JFileChooser. 97 */ 98 g.fillRect(0, 0, ACCSIZE, getHeight()); 99 g.drawImage(image, getWidth() / 2 - width / 2 + 5, getHeight() / 2 100 - height / 2, this); 101 } 102 103 /** 104 * @param args 105 */ 106 public static void main(String[] args) { 107 JFileChooser chooser = new JFileChooser(); 108 ImagePreviewPanel preview = new ImagePreviewPanel(); 109 chooser.setAccessory(preview); 110 chooser.addPropertyChangeListener(preview); 111 chooser.showOpenDialog(null); 112 113 } 114 115 }