3

Created by
kidscript 93 about 3 years ago

How to place text inside a circle in java applet such that middle of the text comes at the center

I'm trying to write 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: Click to see my output

But I want the text to be in the middle.

See task infos

Solutions (3)

2

jens 178 about 3 years ago
10 mBCH
Accepted solution
kidscript 93 about 3 years ago
Creator

cool :) thx

0

0

monolith 71 about 3 years ago
10 mBCH
Accepted solution

-1

mila 15 about 3 years ago
Declined solution
kidscript 93 about 3 years ago
Creator

couldn't you format that? anyway.. it didn't work

0