Source code: org/ydp/gfx/PhotoPreview.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 PhotoPreview extends JComponent
13 {
14 ImageIcon thumbnail = null;
15 String fileName = null;
16 int scaledsize = 160;
17
18 public PhotoPreview(File f)
19 {
20 //System.out.println(f.getPath());
21 setFileName(f.getPath());
22 }
23
24 public PhotoPreview(File f, int twidth)
25 {
26 //System.out.println(f.getPath());
27 scaledsize = twidth;
28 setFileName(f.getPath());
29 }
30
31 public PhotoPreview()
32 {
33 setPreferredSize(new Dimension(100, 50));
34 }
35
36 public void setFileName(String str)
37 {
38 fileName = str;
39 loadImage();
40 }
41
42 public void loadImage()
43 {
44 //System.out.println("Attempting to load image to make preview");
45
46 if(fileName != null)
47 {
48 ImageIcon tmpIcon = new ImageIcon(fileName);
49 if(tmpIcon.getImageLoadStatus() == MediaTracker.ERRORED)
50 {
51 System.out.println("Couldn't find file, using default image");
52 tmpIcon = new ImageIcon("resources/Default.jpg");
53 }
54
55 if(tmpIcon.getIconWidth() > scaledsize)
56 {
57 thumbnail = new ImageIcon(
58 tmpIcon.getImage().getScaledInstance(scaledsize, -1, Image.SCALE_DEFAULT));
59 tmpIcon = thumbnail;
60 }
61 if(tmpIcon.getIconHeight() > scaledsize)
62 {
63 thumbnail = new ImageIcon(
64 tmpIcon.getImage().getScaledInstance(-1, scaledsize, Image.SCALE_DEFAULT));
65 }
66 else if(tmpIcon.getIconWidth() <= scaledsize && tmpIcon.getIconHeight() <= scaledsize || scaledsize == 0)
67 {
68 scaledsize = tmpIcon.getIconWidth();
69 thumbnail = tmpIcon;
70 }
71 setPreferredSize(getSize());
72
73 //System.out.println("Thumbnail created from image data");
74 }
75 }
76
77 public ImageIcon getThumbnail(String str)
78 {
79 setFileName(str);
80 return thumbnail;
81 }
82
83 public ImageIcon getThumbnail()
84 {
85 return thumbnail;
86 }
87
88 public Image getImage()
89 {
90 return thumbnail.getImage();
91 }
92
93 public void paint(Graphics g)
94 {
95 if(thumbnail == null)
96 {
97 loadImage();
98 }
99 if(thumbnail != null)
100 {
101 thumbnail.paintIcon(this, g, 0, 0);
102 }
103 }
104
105 public Dimension getSize()
106 {
107 return new Dimension(thumbnail.getIconWidth(), thumbnail.getIconHeight());
108 }
109
110 }