I'm trying to write a code to place a text in the middle of a circle such that the center of the string is on the center of the circle. But the text appears to start from the center , if the font size of the string and the diameter of the circle are large. Here is my code,
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
//extending Applet class is necessary
public class Main extends Applet{
public void paint(Graphics g)
{
g.setColor(Color.yellow);
int diameter=500;
int xpos=100,ypos=100;
g.fillOval(xpos,ypos,diameter,diameter);
Font f1 = new Font("Arial",Font.BOLD,24);
g.setColor(Color.black);
g.setFont(f1);
String s="Text inside a circle";
g.drawString(s,xpos+(diameter/2)-(s.length()/2),ypos+(diameter/2));
}
}
This is the output I'm getting:
But I want text to be in the middle.