Create an application that displays images for the user, as shown in Fig. 25.50. This application will display images of book covers, but you can easily modify the application to display your own images. You learned in Tutorial 2 that images can be displayed using the icon property of a JLabel. This application should display the current image in a large JLabel and display the previous and next images in smaller JLabels. The images are specified in the file books.txt. To display the next image as the large image, the user clicks the Next Image JButton. To display the previous image as the large image, the user clicks the Previous Image JButton. When either of these JButtons are pressed, the previous and next images are updated based on the next and previous images specified
above the Next Image JButton. When the first image in the file is displayed as the large image, no image will be displayed above the Previous Image JButton. A description of the book represented by the large image should be displayed in a JTextArea. Figure 25.50 displays the cover of C++ How to Program, 4/e as the large image. Figure 25.51 shows the corresponding entry in books.txt that contains the name used for this book’s image as well as a description of the book. You will examine the organization of this file more closely in the following steps. [Note: For presentation proposes, Fig. 25.51 does not show the entire description of the current text.]
a) Copying the template to your working directory. Copy the directory C:Examples Tutorial25ExercisesImageAlbum to your C:SimplyJava directory.
b) Opening the template file. Open the ImageAlbum.java file in your text editor.
c) Declaring the BufferedReader. In lines 26–27, declare BufferedReader input, which will be used to read informati
```
1 // ImageAlbum.java
2 // This application uses file processing to implement an image album
3 // with image having its own text description.
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.io.*;
7 import javax.swing.*;
8
9 public class ImageAlbum extends JFrame
10 {
11 // JLabel to show current book
12 private JLabel mainJLabel;
13
14 // JTextArea and JScrollPane to display information on a book
15 private JTextArea descriptionJTextArea;
16 private JScrollPane descriptionJScrollPane;
17
18 // JLabel and JButton to allow user to got to the previous book
19 private JLabel previousJLabel;
20 private JButton previousJButton;
21
22 // JLabel and JButton to allow user to got to the next book
23 private JLabel nextJLabel;
24 private JButton nextJButton;
25
26 // BufferedReader to read data from a file
27 private BufferedReader input;
28
29 // represents current image's index
30 private int current = 0;
31
32 private String largeImage[] = new String[ 6 ];
33 private String smallImage[] = new String[ 6 ];
34 private String descriptions[] = new String[ 6 ];
35
36 // no-argument constructor
37 public ImageAlbum()
38 {
39 createUserInterface();
40 }
41
42 // create and position GUI components; register event handlers
43 private void createUserInterface()
44 {
45 // get content pane for attaching GUI components
46 Container contentPane = getContentPane();
47
48 // enable explicit positioning of GUI components
49 contentPane.setLayout( null );
50
51 // set up mainJLabel
52 mainJLabel = new JLabel();
53 mainJLabel.setBounds( 16, 16, 273, 357 );
54 mainJLabel.setEnabled( true );
55 contentPane.add( mainJLabel );
56
57 // set up descriptionJTextArea
58 descriptionJTextArea = new JTextArea();
59 descriptionJScrollPane = new JScrollPane(
60 descriptionJTextArea );
61 descriptionJScrollPane.setBounds( 16, 392, 272, 48 );
62 descriptionJScrollPane.setVerticalScrollBarPolicy(
63 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
64 descriptionJTextArea.setLineWrap( true );
65 descriptionJTextArea.setWrapStyleWord( true );
66 descriptionJTextArea.setEditable( false );
67 contentPane.add( descriptionJScrollPane );
68
69 // set up previousJButton
70 previousJButton = new JButton();
71 previousJButton.setBounds( 307, 144, 123, 23 );
72 previousJButton.setText( "Previous Image" );
73 contentPane.add( previousJButton );
74 previousJButton.addActionListener(
75
76 new ActionListener() // anonymous inner class
77 {
78 // event handler called when previousJButton is
79 public void actionPerformed( ActionEvent event )
80 {
81 previousJButtonActionPerformed( event );
82 }
83
84 } // end anonymous inner class
85
86 ); // end call to addActionListener
87
88 // set up previousJLabel
89 previousJLabel = new JLabel();
90 previousJLabel.setBounds( 323, 16, 91, 119 );
91 contentPane.add( previousJLabel );
92
93 // set up nextJButton
94 nextJButton = new JButton();
95 nextJButton.setBounds( 307, 352, 123, 23 );
96 nextJButton.setText( "Next Image" );
97 contentPane.add( nextJButton );
98 nextJButton.addActionListener(
99
100 new ActionListener() // anonymous inner class
101 {
102 // event handler called when nextJButton is clic
103 public void actionPerformed( ActionEvent event )
104 {
105 nextJButtonActionPerformed( event );
106 }
107
108 } // end anonymous inner class
109
110 ); // end call to addActionListener
111
112 // set up nextJLabel
113 nextJLabel = new JLabel();
114 nextJLabel.setBounds( 323, 224, 91, 119 );
115 contentPane.add( nextJLabel );
116
117 // set properties of application's window
118 setTitle( "Image Album" ); // set title bar string
119 setSize( 447, 480 ); // set window size
120 setVisible( true ); // display window
121
122 retrieveData();
123
124 displayImage(); // display first image at start
125
126 } // end method createUserInterface
127
128 // extract descriptions from file and images from the dir
129 private void retrieveData()
130 {
131 // retrieve data from file
132 try
133 {
134 // initialize FileReader to read lines from file
135 File booksFile = new File( "books.txt" );
136 FileReader inputFile = new FileReader( booksFile );
137 input = new BufferedReader( inputFile );
138
139
140 // read first line before entering loop
String imageName = input.readLine();
141
142 int counter = 0;
143
144
145
146
147
148
149
150
144 // loop through lines in file
145 while ( imageName != null )
146 {
147 largeImage[ counter ] = ( "images/large/" +
148 imageName + "_large.jpg" );
149 smallImage[ counter ] = ( "images/small/" +
150 imageName + "_thumb.jpg" );
151 descriptions[ counter ] = input.readLine();
152
153 // read next line in file
154 imageName = input.readLine();
155
156 counter++;
157
158 } // end while
159
160 } // end try
161 catch ( IOException exception )
162 {
163 JOptionPane.showMessageDialog( this, "File error.",
164 "FILE ERROR", JOptionPane.ERROR_MESSAGE );
165 }
166 finally
167 {
168 // close input file
169 try
170 {
171 input.close();
172 }
173 catch ( IOException exception )
174 {
175 JOptionPane.showMessageDialog( this, "File error.",
176 "FILE ERROR", JOptionPane.ERROR_MESSAGE );
177 }
178 }
179
180 } // end method retrieveData
181
182 // display images in JLabels
183 private void displayImage()
184 {
185 // set main image
186 mainJLabel.setIcon( new ImageIcon( largeImage[ current
187
188 //
189 if
190 {
191 // disable previous image
192 previousJLabel.setVisible( false );
193
194 // preview next image
195 nextJLabel.setIcon( new
196 smallImage[ current + 1 ] ) );
197
198 // disable previousJButton
199 previousJButton.setEnabled( false );
200 }
201
202 // if index corresponds to last item in array,
203 // do not show next image
204 else if ( current == ( largeImage.length - 1 ) )
205 {
206 previousJLabel.setIcon( new ImageIcon(
207 smallImage[ current - 1 ] ) );
208
209 nextJLabel.setVisible( false ); // disable next image
210 nextJButton.setEnabled( false ); // disable nextJButton
211 }
212
213 // show previous, current and next image
214 else
215 {
216 previousJLabel.setVisible( true );
217 previousJLabel.setIcon( new ImageIcon(
218 smallImage[ current - 1 ] ) );
219
220 nextJLabel.setVisible( true );
221 nextJLabel.setIcon( new ImageIcon(
222 smallImage[ current + 1 ] ) );
223
224 // enable JButtons
225 previousJButton.setEnabled( true );
226 nextJButton.setEnabled( true );
227 }
228
229 // set description
230 descriptionJTextArea.setText( descriptions[ current ] );
231
232 } // end method displayImage
233
234 // display previous image
235 private void previousJButtonActionPerformed( ActionEvent event )
236 {
237 current--;
238 displayImage(); // display new images
239
240 } // end method previousJButtonActionPerformed
241
242 // display next image
243 private void nextJButtonActionPerformed( ActionEvent event )
244 {
245 current++;
246 displayImage(); // display new images
247
248 } // end method nextJButtonActionPerformed
249
250 // main method
251 public static void main( String args[] )
252 {
253 ImageAlbum application = new ImageAlbum();
254 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
255
256 } // end method main
257
258 } // end class ImageAlbum
```
You might also like to view...
Match the following terms to their descriptions:
I. row in an Excel table A. increases the number of columns II. creating a table from existing data in a range B. increases the number of rows III. converting a table back into a range C. restores Range tools IV. Adding a record to a table D. enables unique table tools V. adding a field to a table E. represents a record.
COGNITIVE ASSESSMENT What does NAS stand for?
A. network access server B. normal access space C. network attached storage D. normal attached space
A hard drive or solid state drive that is physically installed inside of a workstation would generally be considered to be what type of storage?
A. Local storage B. Direct-Attached Storage C. Network Attached Storage D. Storage Area Network
What type of information can be gathered from encrypted network traffic?
A. Private encryption keys B. Information transmitted during an SSL session C. The sender’s IP address, receiver’s IP address, the DNS request to resolve the host name and the port numbers used D. No information can be gathered