如何将由Graphics绘制的String放在java中的椭圆形顶部

imu*_*ion 2 java string graphics

我需要写一个String椭圆形,所以椭圆形的文字显示在它的中间.文本由g.drawString();方法绘制,椭圆形由g.fillOval();.怎么可能把它String放在上面?

aio*_*obe 5

在此输入图像描述

import java.awt.*;
import javax.swing.*;

public class FrameTest {
    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);
    }
}
Run Code Online (Sandbox Code Playgroud)