Java Reference
In-Depth Information
should getPreferredSize()
// be called more than
once.
// perform other initialization (such as registering
a mouse listener) here
}
@Override
public Dimension getPreferredSize()
{
return d;
}
@Override
public void paint(Graphics g)
{
// perform painting here
}
}
It is often not a good idea to override
paint()
in the context of Swing because
JComponent
overridesthismethodtodelegatetheworkofpaintingtothreeprotected
methods:
paintComponent()
,
paintBorder()
, and
paintChildren()
.
Thesemethodsarecalledinthisordertoensurethatchildrenappearontopofthecom-
ponent.
Generallyspeaking,thecomponentanditschildrenshouldnotpaintintheinsetsarea
allocated to the border.
Although,subclassescanoverridethismethod,asubclassthatonlywantstospecial-
ize the UI delegate's
paint()
method should just override
paintComponent()
.
If you're not concerned about UI delegates, borders, and children, the previous
SwingCanvas
class should meet your needs. For more information, check out The
Java Tutorial's “Performing Custom Painting” lesson (
ht-
index.html
)
.
Java 2D
Java 2D
is a collection of AWT extensions that provide advanced two-dimensional
graphical, textual, and imaging capabilities. This API offers a flexible framework for