Java Reference
In-Depth Information
Figure 6.5 The program
TextOutputApplet uses
an Outputable text area
component to display
print messages.
output - panel.println (
" Outputable methods are print(String) &
println(String)");
}
} // class TextOutputApplet
6.6 Drawing
Agraphics interface often needs more than just buttons and text display. The
display of dynamic figures such as graphs and diagrams is a common requirement.
A simulation typically requires an animation created by a sequence of drawn
pictures. Furthermore, the interface needs the capability to draw images and text
on the displays. The Java graphics system provides a large assortment of drawing
tools to perform all of these tasks. In the following sections we discuss the basics
of drawing in a Swing environment.
In Swing the JPanel class usually serves as the drawing board. You override
the panel's paintComponent() method to perform the drawing operations.
Since all of the Swing components are lightweight, you can override their painting
methods and create custom features. For example, you could make your own
button component that displays a custom appearance in the pressed and unpressed
states.
6.6.1 Graphics contexts
The paintComponent (Graphics g) method receives an instance of the
Graphics class as a parameter. This class is referred to as the graphics context
since it provides the context under which the graphics commands operate for a
component. The Graphics object essentially represents a drawing surface or
tablet along with all of the settings such as the current foreground and background
colors and the font selection for the strings. In addition to representing the current
 
Search WWH ::




Custom Search