Sample output from this program is shown here:
Once created, a frame window takes on a life of its own. Notice that main( ) ends with the
call to appwin.setVisible(true). However, the program keeps running until you close the
window. In essence, when creating a windowed application, you will use main( ) to launch
its top-level window. After that, your program will function as a GUI-based application, not
like the console-based programs used earlier.
Displaying Information Within a Window
In the most general sense, a window is a container for information. Although we have already
output small amounts of text to a window in the preceding examples, we have not begun to
take advantage of a window's ability to present high-quality text and graphics. Indeed, much of
the power of the AWT comes from its support for these items. For this reason, the remainder
of this chapter discusses Java's text-, graphics-, and font-handling capabilities. As you will
see, they are both powerful and flexible.
Working with Graphics
The AWT supports a rich assortment of graphics methods. All graphics are drawn relative
to a window. This can be the main window of an applet, a child window of an applet, or a
stand-alone application window. The origin of each window is at the top-left corner and is 0,0.
Coordinates are specified in pixels. All output to a window takes place through a graphics
context. A graphics context is encapsulated by the Graphics class and is obtained in two ways:
· It is passed to an applet when one of its various methods, such as paint( ) or update( ),
· It is returned by the getGraphics( ) method of Component.
For the sake of convenience the remainder of the examples in this chapter will demonstrate
graphics in the main applet window. However, the same techniques will apply to any other
The Graphics class defines a number of drawing functions. Each shape can be drawn
edge-only or filled. Objects are drawn and filled in the currently selected graphics color, which
is black by default. When a graphics object is drawn that exceeds the dimensions of the
window, output is automatically clipped. Let's take a look at several of the drawing methods.
Search WWH :