Home : Course Map : Chapter 6 : Java :
Drawing Text
JavaTech
Course Map
Chapter 6

Introduction
AWT
Swing
Containers
  Demo 1
UI Components
  Demo 2
UI Layout
  Demo 3   Demo 4
Text Display
  Demo 5
Drawing
  Demo 6   Demo 7
Draw Polygons
  Demo 8   Demo 9
Colors
Text Draw 
  Demo 10
Images
  Demo 11
Exercises

    Supplements
AWT
  Demo 1
Drawing
  Demo 2
Text Drawing
  Demo 3
UI Components
  Demo 4

Java2D
Shapes & Areas
  Demo 1   Demo 2
Stroke & Paint
  Demo 3
Transforms
  Demo 4
Gradients&Textures
  Demo 5   Demo 6
Text
  Demo 7   Demo 8
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

Text can be presented in several ways. We review text display components, e.g. JTextField and JTextArea, in the Text Display section. Here we discuss drawing text on a JPanel.

Often in a drawing you want to include text such as the labels of the axes on a graph and the graph's title. With Java2D that came with version 1.2, very elaborate text capabilities became available. However, here we will discuss just the basics that work with the Graphics class.

The first step in drawing text involves choosing the font for the text. You can use the default or specify which of fonts you desire.

A font is specified by an instance of the Font class. The Font constructor uses the following information:

  • Font name:
    • Serif (generic for TimesRoman)
    • SansSerif (generic for Helvetica)
    • Monospaced (generic for Courier)
    • Dialog
    • DialogInput
  • Style - use class constants
    • PLAIN
    • BOLD
    • ITALIC
  • Point size

So, for example,

  public void paintComponent (Graphics g) {
    g.setFont (new Font ("Monospaced",Font.PLAIN, 24);
    g.drawString ("Test", 10, 10);
    ... other drawing commands ...
  }

where an instance of the Monospaced font is created with PLAIN style and 24 pt size. It is made the current font for the graphcis context.

You might assume that the Font instance provides information about the detailed pixel description of the text. However, the FontMetrics class instead provides such information. An instance of this class is obtained from the graphics context.

The FontMetrics class provides various methods to obtain measures of the size of characters and strings:

  • getHeight() - total line height
  • getMaxAscent() - height above the baseline
  • getMaxDescent() - total
  • stringWidth()- width of a string

See the FontMetrics class description in the API Specifications for more about these and its other methods.

The FontMetrics information helps with positioning the strings according to their size, as seen in this demo:

... similar to DrawApplet.java in Drawing example ...

/** JPanel subclass to demonstrate drawing text. **/
public class DrawTextPanel extends JPanel
{
  public void paintComponent (Graphics g) {

    // First paint background
    super.paintComponent (g);

    // Add your drawing instructions here
    g.setColor (Color.red);
    String msg = "Set text in center";

    // Create the font and pass it to the  Graphics context
    g.setFont (new Font ("Monospaced",Font.BOLD,24));

    // Get measures needed to center the message
    FontMetrics fm = g.getFontMetrics ();

    // How many pixels wide is the string
    int msg_width = fm.stringWidth (msg);

    // How far above the baseline can the font go?
    int ascent = fm.getMaxAscent ();

    // How far below the baseline?
    int descent= fm.getMaxDescent ();

    // Use the string width to find the starting point
    int msg_x = getSize ().width/2 - msg_width/2;

    // Use the vertical height of this font to find
    // the vertical starting coordinate
    int msg_y = getSize ().height/2 - descent/2 + ascent/2;

    g.drawString (msg, msg_x, msg_y);

  } // paintComponent

} // class DrawTextPanel

Here we create a Font object for the Monospace type with the given style and size. Then we pass it to the graphics context. We then obtain from the graphics context a FontMetrics object for the current font setting.

With the FontMetrics object we then obtain the string width and the vertical dimensions neccessary to calculate where to draw the string. Note that the drawString() method uses the horizontal and vertical coordinates passed in the arguments for the left baseline point of the string.

 

Latest update: Oct. 26, 2004

            Tech
Java Tech Graphics
Starting to Plot
  Demo 1
Drawing Panel
  Demo 2
Histogram Display

  Demo 3
Exercises

           Physics
Display Text Data
  Demo 1
Plot Data
  Demo 2
Find Max/Min
  Demo 3
Exercises

  
  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.