datalogo
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Text Properties

Not only can you print text messages in a graphic display, you can also control the font, style and size of the printed text. The available font names are system dependent.

A font is text of a particular size and style. In Java, a font is characterized by a font name, a style, and a size (which is always an integer). You may see a reference to a "Dialog" font, which is the font typically used in dialog boxes.

Fonts:

We will be controlling the font name, the font style (PLAIN, BOLD, ITALIC), and the font size.  Check out these examples:

Font timesBold16 = new Font("Times New Roman", Font.BOLD, 16);

Font arialBolditalic12 = new Font("Arial", Font.BOLD + Font.ITALIC, 12);

The names of the fonts available for graphical use will depend upon the fonts available on the computer running the program.  Here is a little Java program that will tell you which font names are available on the computer you are using. Results appear in the Console window. The list is usually quite long.

import java.awt.GraphicsEnvironment;
public class fontTest3
{
     public static void main (String [] args)
     {
          GraphicsEnvironment ge =                    GraphicsEnvironment.getLocalGraphicsEnvironment();
          String allFonts[] = ge.getAvailableFontFamilyNames();
          for (int i = 0; i < allFonts.length; i++)
               System.out.println(allFonts[i]);
     }
}


Sample coding using Font class:

Font courierBold40 = new Font("Courier", Font.BOLD, 40);
g.setColor(Color.red);
g.setFont(courierBold40);
g.drawString("Java Rules!!!", 120, 120);

fontTest

Here is the full code to produce the window shown above:

fonttest40

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area, JavaMathBits.com.
Help us keep these resources free. Thank you.