Java Reference
In-Depth Information
Swing makes use of some of the AWT classes, replaces some AWT classes with its own
versions, and adds many new classes (Figure 11.1).
Figure 11.1
AWT and Swing
In this topic, we shall use the Swing libraries. This means that we shall use some AWT classes
that are still used with Swing programs but use the Swing versions of all classes that exist in
both libraries.
Wherever there are equivalent classes in AWT and Swing, the Swing versions have been iden-
tified by adding the letter J to the start of the class name. You will, for example, see classes
named Button and JButton , Frame and JFrame , Menu and JMenu , and so on. The classes
starting with a J are the Swing versions; these are the ones we shall use, and the two should not
be mixed in an application.
That is enough background for a start. Let us look at some code.
11.4
The ImageViewer example
As always, we shall discuss the new concepts by using an example. The application we shall
build in this chapter is an image viewer (Figure 11.2). This is a program that can open and dis-
play image files in JPEG and PNG formats, perform some image transformations, and save the
images back to disk.
As part of this, we shall use our own image class to represent an image while it is in memory,
implement various filters to change the image's appearance, and use Swing components to
build a user interface. While doing this, we shall concentrate our discussion on the GUI aspects
of the program.
Concept:
Image format
Images can be
stored in different
formats. The dif-
ferences primarily
affect file size and
the quality of the
image.
If you are curious to see what we shall build, you can open and try out the imageviewer1-0
project—that is the version displayed in Figure 11.2; just create an ImageViewer object. There
are some sample images in the images folder inside the chapter11 folder (one level up from the
project folder). You can, of course, also open your own images. Here, we start slowly, initially
with something much simpler, and we shall work our way to the final application step by step.
11.4.1
First experiments: creating a frame
Almost everything you see in a GUI is contained in a top-level window. A top-level window is
one that is under the control of the operating system's window management and which typically
can be moved, resized, minimized, and maximized independently.
 
 
Search WWH ::




Custom Search