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 int _lastWidth = -1;
22 private int _lastHeight = -1;
23 private boolean _loadingImage = false;
24 private static String _loadingString = null;
30 public PhotoThumbnail()
32 // TODO: Make size of thumbnail dynamic, as big as it can be
34 _loadingString = I18nManager.getText("details.photo.loading") + " ...";
40 * @param inPhoto Photo object to show thumbnail for
42 public void setPhoto(Photo inPhoto)
44 // Check whether the photo has changed
45 if (_photo == inPhoto) {return;}
52 * Override paint method
53 * @see javax.swing.JComponent#paint(java.awt.Graphics)
55 public void paint(Graphics inG)
60 // recalculate thumbnail if photo has changed
61 if (_thumbnail == null || getWidth() != _lastWidth || getHeight() != _lastHeight)
63 // initiate load if not already started
67 new Thread(this).start();
70 // Set width and height
71 _lastWidth = getWidth();
72 _lastHeight = getHeight();
73 // if loading, display image
76 inG.setColor(Color.BLACK);
77 inG.drawString(_loadingString, 10, 30);
81 // Copy scaled, smoothed image onto the screen
82 inG.drawImage(_thumbnail, 0, 0, _thumbnail.getWidth(), _thumbnail.getHeight(), null);
89 * Run method, for loading image in separate thread
90 * @see java.lang.Runnable#run()
94 // Use exif thumbnail?
95 if (_photo.getExifThumbnail() != null) {
96 Image image = new ImageIcon(_photo.getExifThumbnail()).getImage();
97 _thumbnail = ImageUtils.createScaledImage(image, image.getWidth(null), image.getHeight(null));
102 // no exif thumbnail available, going to have to read whole thing
103 int picWidth = _photo.getWidth();
104 int picHeight = _photo.getHeight();
105 if (picWidth > -1 && picHeight > -1)
107 int displayWidth = Math.min(getWidth(), getParent().getWidth());
108 int displayHeight = Math.min(getHeight(), getParent().getHeight());
110 // calculate maximum thumbnail size
111 Dimension thumbSize = ImageUtils.getThumbnailSize(picWidth, picHeight, displayWidth, displayHeight);
112 // Work out if need to remake image
113 boolean needToRemake = (_thumbnail == null)
114 || _thumbnail.getWidth() != thumbSize.width || _thumbnail.getHeight() != thumbSize.height;
115 if (thumbSize.width > 0 && thumbSize.height > 0 && needToRemake)
117 // Make icon to load image into
118 Image image = new ImageIcon(_photo.getFile().getAbsolutePath()).getImage();
119 // save scaled, smoothed thumbnail for reuse
120 _thumbnail = ImageUtils.createScaledImage(image, thumbSize.width, thumbSize.height);
122 // TODO: Calculate and set size of thumbnail here
123 // setPreferredSize(new Dimension(200, 200));
127 _loadingImage = false;