Home : Course Map : Chapter 6 : Java : Supplements :
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

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.

The Font and FontMetrics classes are described in the Chapter 6: Java: Text Draw section. Below is an Applet that performs the same text drawing as that of the JApplet version in that section.

import java.applet.*;
import java.awt.*;

/** Demonstration of text drawing in AWT. **/

public class TextApplet extends Applet
{

 public void paint (Graphics g ) {
  String msg = "Set text in center";

  // Set the context color for lines and text
  g.setColor (Color.red);

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

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

  // How many pixels wide is the string
  int msgWidth = 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 msgX = getSize ().width/2 - msgWidth/2;

  // Use the vertical height of this font to find
  // the vertical starting coordinate

  int msgY = getSize ().height/2 - descent/2 + ascent/2;

  g.drawString (msg,msgX,msgY);

 }
// paint
} // class TextApplet

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 stringwidth 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. 27, 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.