| Home >> All >> org >> ydp >> [ gfx Javadoc ] |
Source code: org/ydp/gfx/FilePreviewer.java
1 package org.ydp.gfx; 2 3 import javax.swing.event.*; 4 import javax.swing.*; 5 6 import java.awt.event.*; 7 import java.awt.*; 8 9 import java.beans.*; 10 import java.io.*; 11 12 public class FilePreviewer extends JComponent implements PropertyChangeListener 13 { 14 ImageIcon thumbnail = null; 15 File f = null; 16 17 public FilePreviewer(JFileChooser fc) 18 { 19 setPreferredSize(new Dimension(100, 50)); 20 fc.addPropertyChangeListener(this); 21 } 22 23 public FilePreviewer() 24 { 25 setPreferredSize(new Dimension(100, 50)); 26 } 27 28 public void loadImage() 29 { 30 if(f != null) 31 { 32 ImageIcon tmpIcon = new ImageIcon(f.getPath()); 33 if(tmpIcon.getIconWidth() > 90) 34 { 35 thumbnail = new ImageIcon( 36 tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT)); 37 } 38 else 39 { 40 thumbnail = tmpIcon; 41 } 42 } 43 } 44 45 public void propertyChange(PropertyChangeEvent e) 46 { 47 String prop = e.getPropertyName(); 48 if(prop == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) 49 { 50 f = (File) e.getNewValue(); 51 if(isShowing()) 52 { 53 loadImage(); 54 repaint(); 55 } 56 } 57 } 58 59 public void paint(Graphics g) 60 { 61 if(thumbnail == null) 62 { 63 loadImage(); 64 } 65 if(thumbnail != null) 66 { 67 int x = getWidth()/2 - thumbnail.getIconWidth()/2; 68 int y = getHeight()/2 - thumbnail.getIconHeight()/2; 69 if(y < 0) 70 { 71 y = 0; 72 } 73 74 if(x < 5) 75 { 76 x = 5; 77 } 78 79 thumbnail.paintIcon(this, g, x, y); 80 } 81 } 82 }