Java Reference
In-Depth Information
The update() and paint() methods
Java programs that use the AWT are event-driven. The programmer codes
theoperationsthatdrawontheprogram'sclientareabutdoesnotperform
thedrawingdirectly.Insteadthecodewaitsforasystemorusergenerated
eventtodrawtothewindow.Thiseventcanbetheprogramwindowbeing
displayed for the first time, or the program window being uncovered,
resized, minimized, maximized, or otherwise modified by the user. When
the Java event handler needs to redraw a window, it triggers a call to a
method called update() which is located in the Component class. The up-
date() method erases the window background and then calls the paint()
methodintheJavaCanvasclass.Atypicalapplicationredefinesthepaint()
methodsothatitreceivescontrolwhenevertheprogram'swindowrequires
updating.Thepaint()methodreceivesaGraphicsobjectasitsonlyparame-
ter. For example:
public void paint(Graphics g)
{
g.drawString(“Hello World!!!”, 50, 100);
}
In this case the code has redefined paint() with its own version so that
it receives control whenever a screen update is required. The only output
operation in this case is displaying a message on the screen using the
drawString() method of the Graphics class. The second and third parame-
ters to drawString() are the x and y pixel coordinates in the client area.
Manipulating fonts
Afontisasetofcharactersofthesametypeface,style,andsize.IntheAWT
thetypefacesavailableareSansSerif,Serif,Monospaced,Dialog,andWin-
dowsDialog.Thesytlesarebold,italic,bold-italic,andplain.Thesizeisex-
pressed in units called points , each point being 1/72th of an inch.
In Java an attribute of the Graphics object is the default font that is
used in drawing text to the screen. Applications can select other fonts
and point sizes by instantiating an object of the Font class. The Font con-
structor is as follows:
public Font(String name, int style, int size)
----------- --------- --------
FONT NAME: ---- |
|
|------ point size
SansSerif
|------------ STYLES:
Serif
BOLD
Monospaced
ITALIC
Dialog
BOLD | ITALIC
Dialog input
PLAIN
Search WWH ::




Custom Search