Border is the Swing interface that encapsulates a border. You can obtain a border by calling
one of the factory methods defined by the BorderFactory class. The one used in the
program is createLineBorder( ), which creates a simple line border. It is shown here:
static Border createLineBorder(Color clr, int width)
Here, clr specifies the color of the border and width specifies its width in pixels.
Inside the override of paintComponent( ), notice that it first calls super.paintComponent( ).
As explained, this is necessary to ensure that the component is properly drawn. Next the width
and height of the panel are obtained along with the insets. These values are used to ensure the
lines lie within the drawing area of the panel. The drawing area is the overall width and height
of a component less the border width. The computations are designed to work with differently
sized PaintPanels and borders. To prove this, try changing the size of the window. The lines will
still all lie within the borders of the panel.
The PaintDemo class creates a PaintPanel and then adds the panel to the content pane.
When the application is first displayed, the overridden paintComponent( ) method is
called, and the lines are drawn. Each time you resize or hide and restore the window, a
new set of lines are drawn. In all cases, the lines fall within the paintable area.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home