/
ImagePreview.java
108 lines (96 loc) · 2.46 KB
/
ImagePreview.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Leaves Recognition - a neuronal network based recognition of leaf images
* Copyright (C) 2001 Jens Langner, LightSpeed Communications GbR
*
* LightSpeed Communications GbR
* Lannerstrasse 1
* 01219 Dresden
* Germany
* http://www.light-speed.de/
* <Jens.Langner@light-speed.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
*/
package lrecog.tools;
import javax.swing.*;
import java.beans.*;
import java.awt.*;
import java.io.File;
/**
* special class for loading/saving and administrating
* the project relevant data
*/
public class ImagePreview extends JComponent implements PropertyChangeListener
{
ImageIcon thumbnail = null;
File file = null;
public ImagePreview(JFileChooser fc)
{
setPreferredSize(new Dimension(100, 50));
fc.addPropertyChangeListener(this);
}
public void loadImage()
{
if(file == null)
{
return;
}
ImageIcon tmpIcon = new ImageIcon(file.getPath());
if (tmpIcon.getIconWidth() > 90)
{
thumbnail = new ImageIcon(tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT));
}
else
{
thumbnail = tmpIcon;
}
}
public void propertyChange(PropertyChangeEvent e)
{
String prop = e.getPropertyName();
if (prop.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY))
{
file = (File) e.getNewValue();
if (isShowing())
{
loadImage();
repaint();
}
}
}
public void paintComponent(Graphics g)
{
if(thumbnail == null)
{
loadImage();
}
if(thumbnail != null)
{
int x = getWidth()/2 - thumbnail.getIconWidth()/2;
int y = getHeight()/2 - thumbnail.getIconHeight()/2;
if(y < 0)
{
y = 0;
}
if(x < 5)
{
x = 5;
}
thumbnail.paintIcon(this, g, x, y);
}
}
}