Java Reference
In-Depth Information
Code 11.4
continued
ImageViewer
class
with
ImagePanel
private
void
makeFrame()
{
frame =
new
JFrame(
"ImageViewer"
);
makeMenuBar(frame);
Container contentPane = frame.getContentPane();
imagePanel =
new
ImagePanel();
contentPane.add(imagePanel);
// building is done - arrange the components and show
frame.pack();
frame.setVisible(
true
);
}
//
MakeMenuBar method omitted.
}
When comparing this code with the previous version, we note that there are only two small changes:
■
In method
makeFrame
, we now create and add an
ImagePanel
component instead of a
JLabel
. Doing this is not more complicated than adding the label. The
ImagePanel
object
is stored in an instance field so that we can access it again later.
■
Our
openFile
method has now been changed to actually open and display an image file.
Using our image-processing classes, this also is easy now. The
ImageFileManager
class
has a method to select and open an image, and the
ImagePanel
object has a method to dis-
play that image. One thing to note is that we need to call
frame.pack()
at the end of the
openFile
method, as the size of our image component has changed. The
pack
method will
recalculate the frame layout and redraw the frame so that the size change is properly handled.
Exercise 11.14
Open and test the
imageviewer0-4
project. The folder for this chapter's
projects also includes a folder called
images
. Here, you can find some test images you can
use. Of course, you can also use your own images.
Exercise 11.15
What happens when you open an image and then resize the frame? What if
you first resize the frame and then open an image?
With this version, we have solved the central task; we can now open an image file from disk
and display it on screen. Before we call our project “version 1.0,” however, and thus declare it
finished for the first time, we want to add a few more improvements (see Figure 11.2).
■
We want to add two labels: one to display the image filename at the top and a status text at
the bottom.
■
We want to add a
Filter
menu that contains some filters that change the image's appearance.
■
We want to add a
Help
menu that contains an
About ImageViewer
item. Selecting this menu item
should display a dialog with the application's name, version number, and author information.
Search WWH ::
Custom Search