-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathImageViewerBean.java
More file actions
57 lines (51 loc) · 1.2 KB
/
Copy pathImageViewerBean.java
File metadata and controls
57 lines (51 loc) · 1.2 KB
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
package imageViewer;
import java.awt.*;
import java.io.*;
import java.nio.file.*;
import javax.imageio.*;
import javax.swing.*;
/**
* A bean for viewing an image.
* @version 1.22 2012-06-10
* @author Cay Horstmann
*/
public class ImageViewerBean extends JLabel
{
private Path path = null;
private static final int XPREFSIZE = 200;
private static final int YPREFSIZE = 200;
public ImageViewerBean()
{
setBorder(BorderFactory.createEtchedBorder());
}
/**
* Sets the fileName property.
* @param fileName the image file name
*/
public void setFileName(String fileName)
{
path = Paths.get(fileName);
try (InputStream in = Files.newInputStream(path))
{
setIcon(new ImageIcon(ImageIO.read(in)));
}
catch (IOException e)
{
path = null;
setIcon(null);
}
}
/**
* Gets the fileName property.
* @return the image file name
*/
public String getFileName()
{
if (path == null) return "";
else return path.toString();
}
public Dimension getPreferredSize()
{
return new Dimension(XPREFSIZE, YPREFSIZE);
}
}