A Word About Overriding paint( )
Before concluding our examination of AWT controls, a short word
about overriding paint( ) is in
order. Although not relevant to the simple AWT examples shown in this book,
when overriding paint( ), there are
times when it is necessary to call the superclass implementation of paint( ). Therefore, for some programs,
you will need to use this paint( )
skeleton:
public
void paint(Graphics g) {
//code to repaint this window
//Call superclass paint()
super.paint(g);
}
In Java, there are two general types of components: heavyweight and
lightweight. A heavyweight component has its own native window, which is called
its peer. A lightweight component is
implemented completely in Java code and uses the window provided by an
ancestor. The AWT controls described and used in this chapter are all
heavyweight.
However, if a container holds any lightweight components (that is,
has lightweight child components), your override of paint( ) for that container must call super.paint( ). By calling super.paint(
), you ensure that any lightweight child components, such as lightweight controls, get properly painted. If you
are unsure of a child component’s type, you can call isLightweight( ), defined by
Component, to find out. It returns
true if the component is lightweight,
and false otherwise.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.