Java Reference
In-Depth Information
When the user clicks the calcButton, it triggers the actionPerformed()
method. The actionPerformed() method includes the getText() method to
retrieve the input values, formulas to calculate BMI, and the setText() method to
display output to the user. Anytime you use a TextField component in Java, you
can use the getText() and setText() methods to transfer data back and forth eas-
ily from the user to the applet, just as you did with the ISR when the program
was a console application.
The paint() Method
The final step in coding the Body Mass Index Calculator applet is to enter
the code for the paint() method that draws the image in the applet after it is
initialized. As shown in Figure 3-45, line 57 will draw the Sun Fitness Center
logo image at a location of 125 pixels from the left side of the applet and 160
pixels from the top. The brace in line 58 closes the block of code for the paint()
method. Line 59 displays the closing brace for the entire BodyMassApplet class.
55
public void
paint
(
Graphics
g
)
56
{
57
g.drawImage
(
logo,125,160,
this
)
;
58
}
59
}
FIGURE 3-45
The following step enters the paint() method.
To Code the paint() Method
1.
With the insertion point on line 55, enter the code as shown in
Figure 3-45.
The paint() method is entered (Figure 3-46). Line 59 displays the closing
brace for the BodyMassApplet class.
paint()
method
code entered
closing brace
for class block
FIGURE 3-46
Search WWH ::
Custom Search