import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;


/**
 * A JComponent that displays a GlyphVector and LineMetrics
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class GlyphMetricsComponent extends GlyphComponent
{

    /**
     * Explicit Value Constructor
     */
    public GlyphMetricsComponent(String text)
    {
       super(text);

       // Construct a (logical) Font
       font  = new Font("Serif", Font.PLAIN, 100);
    }


    /**
     * Render the glyphs and the metrics
     *
     * Note: There are better approaches.  This method
     * is used for illustrative purposes only.
     */
    protected void paintGlyphs(Graphics2D g2, String text)
    {
//[step1.
       FontRenderContext       frc;
       GlyphVector             glyphs;

       frc     = g2.getFontRenderContext();
       glyphs  = font.createGlyphVector(frc, text);
//]step1.

//[step2.
       LineMetrics             lm;

       lm      = font.getLineMetrics(text, frc);
//]step2.

//[step3.
       float                   ascent, descent, height, leading;

       // Get the various metrics
       ascent  = lm.getAscent();
       descent = lm.getDescent();
       height  = lm.getHeight();
       leading = lm.getLeading();
//]step3.

       Dimension               d;
       float                   baseline, pWidth, y;
       Rectangle2D             bounds;
       Shape                   shape;
       d = getSize();
       g2.setColor(Color.BLACK);

       baseline = (float)(d.height)/2.0f;
       pWidth   = (float)(d.width);

       // Draw the metrics
       y = baseline - ascent;
       g2.setColor(Color.BLUE);
       g2.draw(new Line2D.Float(0.0f, y, pWidth, y));

       y = baseline;
       g2.setColor(Color.WHITE);
       g2.draw(new Line2D.Float(0.0f, y, pWidth, y));

       g2.setColor(Color.RED);
       y = baseline + descent;
       g2.draw(new Line2D.Float(0.0f, y, pWidth, y));

       if (leading > 0) 
       {
          y = baseline + descent + leading;
          g2.setColor(Color.GREEN);
          g2.draw(new Line2D.Float(0.0f, y, pWidth, y));
       }

       // Get the outline at (0,baseline)
       shape = glyphs.getOutline(0, baseline);

       g2.setColor(Color.BLACK);
       g2.draw(shape);
       g2.fill(shape);
    }

}
