Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;

import java.awt.font.LineMetrics;

public class Main {
    /** Draw 'str' centered at 'p'. */
    public static void drawCenteredText(Graphics g, Point p, String str) {
        FontMetrics fm = g.getFontMetrics();
        LineMetrics lm = fm.getLineMetrics(str, g);

        // Go to 'p', then add a/2 to get to the baseline.
        // I ignore the descent because it looks better to center without
        // regard to descenders.
        int baseY = p.y + (int) (lm.getAscent() / 2);
        int baseX = p.x - fm.stringWidth(str) / 2;

        g.drawString(str, baseX, baseY);
    }
}