FileDocCategorySizeDatePackage
HorizontallyCenteredText.javaAPI DocExample1103Mon Apr 05 10:28:18 BST 1999None

HorizontallyCenteredText

public class HorizontallyCenteredText extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

    Frame frame = new ApplicationFrame("HorizontallyCenteredText v1.0") {
      public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

        g2.setFont(new Font("Serif", Font.PLAIN, 48));
        
        paintHorizontallyCenteredText(g2, "come", 100, 75);
        paintHorizontallyCenteredText(g2, "and", 100, 125);
        paintHorizontallyCenteredText(g2, "play", 100, 175);
      }
      
      protected void paintHorizontallyCenteredText(Graphics2D g2, String s,
          float centerX, float baselineY) {
        FontRenderContext frc = g2.getFontRenderContext();
        Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
        float width = (float)bounds.getWidth();
        g2.drawString(s, centerX - width / 2, baselineY);
      }
    };
    frame.setVisible(true);