4 import java.awt.Dimension;
5 import java.awt.Graphics;
7 import java.awt.image.BufferedImage;
9 import javax.swing.ImageIcon;
10 import javax.swing.JPanel;
12 import tim.prune.I18nManager;
13 import tim.prune.data.Photo;
16 * GUI component for showing photo thumbnail
18 public class PhotoThumbnail extends JPanel implements Runnable
20 private Photo _photo = null;
21 private BufferedImage _thumbnail = null;
22 private int _lastWidth = -1;
23 private int _lastHeight = -1;
24 private boolean _loadingImage = false;
25 private static String _loadingString = null;
31 public PhotoThumbnail()
33 // TODO: Make size of thumbnail dynamic, as big as it can be
35 _loadingString = I18nManager.getText("details.photo.loading") + " ...";
41 * @param inPhoto Photo object to show thumbnail for
43 public void setPhoto(Photo inPhoto)
45 // Check whether the photo has changed
46 if (_photo == inPhoto) {return;}
53 * Override paint method
54 * @see javax.swing.JComponent#paint(java.awt.Graphics)
56 public void paint(Graphics inG)
61 // recalculate thumbnail if photo has changed
62 if (_thumbnail == null || getWidth() != _lastWidth || getHeight() != _lastHeight)
64 // initiate load if not already started
68 new Thread(this).start();
71 // Set width and height
72 _lastWidth = getWidth();
73 _lastHeight = getHeight();
74 // if loading, display image
77 inG.setColor(Color.BLACK);
78 inG.drawString(_loadingString, 10, 30);
82 // Copy scaled, smoothed image onto the screen
83 inG.drawImage(_thumbnail, 0, 0, _thumbnail.getWidth(), _thumbnail.getHeight(), null);
90 * Run method, for loading image in separate thread
91 * @see java.lang.Runnable#run()
95 int picWidth = _photo.getWidth();
96 int picHeight = _photo.getHeight();
97 if (picWidth > -1 && picHeight > -1)
99 int displayWidth = Math.min(getWidth(), getParent().getWidth());
100 // System.out.println("width = " + getWidth() + ", " + getParent().getWidth() + " = " + displayWidth);
101 int displayHeight = Math.min(getHeight(), getParent().getHeight());
102 // System.out.println("height = " + getHeight() + ", " + getParent().getHeight() + " = " + displayHeight);
104 // calculate maximum thumbnail size
105 Dimension thumbSize = ImageUtils.getThumbnailSize(picWidth, picHeight, displayWidth, displayHeight);
106 // Work out if need to remake image
107 boolean needToRemake = (_thumbnail == null)
108 || _thumbnail.getWidth() != thumbSize.width || _thumbnail.getHeight() != thumbSize.height;
109 if (thumbSize.width > 0 && thumbSize.height > 0 && needToRemake)
111 // Make icon to load image into
112 Image image = new ImageIcon(_photo.getFile().getAbsolutePath()).getImage();
113 // save scaled, smoothed thumbnail for reuse
114 _thumbnail = ImageUtils.createScaledImage(image, thumbSize.width, thumbSize.height);
116 // TODO: Calculate and set size of thumbnail here
117 // setPreferredSize(new Dimension(200, 200));
120 _loadingImage = false;