-
Notifications
You must be signed in to change notification settings - Fork 0
/
PictureFrame.java
175 lines (143 loc) · 3.72 KB
/
PictureFrame.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import javax.swing.*;
import java.awt.*;
/**
* Class that holds a digital picture and displays it using a JFrame
*
* @author Barb Ericson
*/
public class PictureFrame
{
////////////////// fields ////////////////////////////
/** Main window used as the frame */
JFrame frame = new JFrame();
/** ImageIcon used to display the picture in the label*/
ImageIcon imageIcon = new ImageIcon();
/** Label used to display the picture */
private JLabel label = new JLabel(imageIcon);
/** Digital Picture to display */
private DigitalPicture picture;
///////////////// constructors ////////////////////////
/**
* A constructor that takes no arguments. This is needed
* for subclasses of this class
*/
public PictureFrame()
{
// set up the frame
initFrame();
}
/**
* A constructor that takes a picture to display
* @param picture the digital picture to display in the
* picture frame
*/
public PictureFrame(DigitalPicture picture)
{
// set the current object's picture to the passed in picture
this.picture = picture;
// set up the frame
initFrame();
}
///////////////////////// methods ///////////////////////////////
/**
* Method to set the picture to show in this picture frame
* @param picture the new picture to use
*/
public void setPicture(Picture picture)
{
this.picture = picture;
imageIcon.setImage(picture.getImage());
frame.pack();
frame.repaint();
}
/**
* A method to update the picture frame image with the image
* in the picture
*/
public void updateImage()
{
// only do this if there is a picture
if (picture != null)
{
// set the image for the image icon from the picture
imageIcon.setImage(picture.getImage());
// set the title of the frame to the title of the picture
frame.setTitle(picture.getTitle());
}
}
/**
* A method to update the picture frame image with the image in
* the picture and show it
*/
public void updateImageAndShowIt()
{
// first update the image
updateImage();
// now make sure it is shown
frame.setVisible(true);
}
/**
* A method to make sure the frame is displayed
*/
public void displayImage()
{
frame.setVisible(true);
}
/**
* A method to hide the frame
*/
public void hide()
{
frame.setVisible(false);
}
/**
* A method to set the visible flag on the frame
* @param flag the flag to use
*/
public void setVisible(boolean flag)
{
frame.setVisible(flag);
}
/**
* A method to close a picture frame
*/
public void close()
{
frame.setVisible(false);
frame.dispose();
}
/**
* Method to set the title for the picture frame
* @param title the title to use
*/
public void setTitle(String title)
{
frame.setTitle(title);
}
/**
* Method to force the picture frame to repaint (redraw)
*/
public void repaint()
{
// make it visible
frame.setVisible(true);
// update the image from the picture
updateImage();
// tell the JFrame to handle the repaint
frame.repaint();
}
/**
* A method to initialize the picture frame
*/
private void initFrame()
{
// set the image for the picture frame
updateImage();
// add the label to the frame
frame.getContentPane().add(label);
// pack the frame (set the size to as big as it needs to be)
frame.pack();
// make the frame visible
frame.setVisible(true);
}
}