Java Graphics How to - Draw String on top of an oval








Question

We would like to know how to draw String on top of an oval.

Answer

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
/*  w  w w  .  j  a  va2  s .  c o  m*/
import javax.swing.JComponent;
import javax.swing.JFrame;

public class Main {
  public static void main(String args[]) {
    JFrame f = new JFrame();
    f.add(new JComponent() {
      public void paintComponent(Graphics g) {

        // Some parameters.
        String text = "Some Label";
        int centerX = 150, centerY = 100;
        int ovalWidth = 200, ovalHeight = 100;

        // Draw oval
        g.setColor(Color.BLUE);
        g.fillOval(centerX - ovalWidth / 2, centerY - ovalHeight / 2,
            ovalWidth, ovalHeight);

        // Draw centered text
        FontMetrics fm = g.getFontMetrics();
        double textWidth = fm.getStringBounds(text, g).getWidth();
        g.setColor(Color.WHITE);
        g.drawString(text, (int) (centerX - textWidth / 2),
            (int) (centerY + fm.getMaxAscent() / 2));

      }
    });

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(300, 300);
    f.setVisible(true);
  }
}