4 import java.awt.Dimension;
5 import java.awt.Graphics;
7 import java.awt.image.BufferedImage;
8 import javax.swing.ImageIcon;
9 import javax.swing.JPanel;
11 import tim.prune.I18nManager;
12 import tim.prune.data.Photo;
15 * GUI component for showing photo thumbnail
17 public class PhotoThumbnail extends JPanel implements Runnable
19 private Photo _photo = null;
20 private BufferedImage _thumbnail = null;
21 private boolean _loadingImage = false;
22 /** String to show before photo is loaded */
23 private static final String LOADING_STRING = I18nManager.getText("details.photo.loading") + " ...";
29 public PhotoThumbnail()
37 * @param inPhoto Photo object to show thumbnail for
39 public void setPhoto(Photo inPhoto)
41 // Check whether the photo has changed
42 if (_photo != inPhoto) {
50 * Force a refresh / reload
52 public void refresh() {
57 * Override paint method
58 * @see javax.swing.JComponent#paint(java.awt.Graphics)
60 public void paint(Graphics inG)
65 // read thumbnail in separate thread
66 if (_thumbnail == null && !_loadingImage)
69 new Thread(this).start();
71 // if loading, display message
74 inG.setColor(Color.BLACK);
75 inG.drawString(LOADING_STRING, 10, 30);
79 // Copy scaled, smoothed (and rotated) image into scaled
80 int usableWidth = getParent().getWidth()-10;
81 Image scaled = ImageUtils.rotateImage(_thumbnail, usableWidth, usableWidth, _photo.getRotationDegrees());
82 int scaleWidth = scaled.getWidth(null);
83 int scaleHeight = scaled.getHeight(null);
84 // Draw scaled / rotated image to component
85 int horizOffset = (getWidth() - scaleWidth) / 2;
86 int vertOffset = (getHeight() - scaleHeight) / 2;
87 inG.drawImage(scaled, horizOffset, vertOffset, scaleWidth, scaleHeight, null);
88 if (getHeight() < getWidth())
90 setPreferredSize(new Dimension(usableWidth, usableWidth));
99 * Run method, for loading image in separate thread
100 * @see java.lang.Runnable#run()
104 // Use exif thumbnail?
105 if (_photo.getExifThumbnail() != null) {
106 Image image = new ImageIcon(_photo.getExifThumbnail()).getImage();
107 _thumbnail = ImageUtils.createScaledImage(image, image.getWidth(null), image.getHeight(null));
112 // no exif thumbnail available, going to have to read whole thing
113 int picWidth = _photo.getWidth();
114 int picHeight = _photo.getHeight();
115 if (picWidth > -1 && picHeight > -1)
117 // Just set a "reasonable" thumbnail size for now
118 final int DEFAULT_THUMB_SIZE = 400;
119 // calculate maximum thumbnail size
120 Dimension thumbSize = ImageUtils.getThumbnailSize(picWidth, picHeight, DEFAULT_THUMB_SIZE, DEFAULT_THUMB_SIZE);
121 // Make icon to load image into
122 Image image = new ImageIcon(_photo.getFile().getAbsolutePath()).getImage();
123 // save scaled, smoothed thumbnail for reuse
124 _thumbnail = ImageUtils.createScaledImage(image, thumbSize.width, thumbSize.height);
128 _loadingImage = false;