Java Reference
In-Depth Information
way the window first comes up. So, if the user clicks no tab, it is the same as clicking
the Swatches tab. The RGB tab allows the user to choose a color by specifying the red,
green, and blue values. The HSB tab gives the user a chance to choose colors in a way we
will not discuss. To really understand the JColorChooser dialog window, you need to
run the program in Display 18.20 to see it in action.
Self-Test Exercises
17. How would you change the method paint in Display 18.18 so that the happy
face has one blue eye (the right eye) and one green eye (the left eye)?
18. How would you change the method paint in Display 18.18 so that the happy
face not only has blue eyes and a red mouth, but also has brown skin?
18.5
Fonts and the drawString Method
It is not of so much consequence what you say,
as how you say it.
ALEXANDER SMITH, Dreamthorp. On the Writing of Essays
Java has facilities to add text to drawings and to modify the font of the text. We will show
you enough to allow you to do most things you might want to do with text and fonts.
The drawString Method
Display 18.21 contains a demonstration program for the method drawString . When
the program is run, the GUI displays the text "Push the button." When the user
clicks the button, the string is changed to "Thank you. I needed that." The text is
written with the method drawString .
The method drawString is similar to the drawing methods in the class Graphics ,
but it displays text rather than a drawing. For example, the following line from
Display 18.21 writes the string stored in the variable theText starting at the x- and
y- coordinates X_START and Y_START :
g.drawString(theText, X_START, Y_START);
The string is written in the current font. A default font is used if no font is specified.
The details about fonts are discussed in the next subsection.
 
 
Search WWH ::




Custom Search