| 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)DialogDialogInputStyle - use class 
                constants 
                
              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 baselinegetMaxDescent() 
                - 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 |