我有一个extends JPanel
的课程。通过在类中添加以下方法来设置JPanel的大小:
@Override
public Dimension getPreferredSize() {
return new Dimension(WIDTH, HEIGHT);
}
该课程的相关领域是:
int position = 0;
int randomSize = 0;
int randomPositionX = 0;
int randomPositionY = 0;
public final static int MAX_SIZE = 100;
public final static int MIN_SIZE = 10;
public final static int WIDTH = 500;
public final static int HEIGHT = 500;
private Random rand = new Random();
String alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
我选择一个随机大小(最大值为100,最小值为10)和一个方法的随机坐标。我还为字符串alphabets
选择了一个随机索引:
randomSize = MIN_SIZE + (rand.nextInt(MAX_SIZE - MIN_SIZE) + 1);
randomPositionX = rand.nextInt(WIDTH - randomSize);
randomPositionY = rand.nextInt(HEIGHT - randomSize);
position = rand.nextInt(alphabets.length());
我尝试绘制字母和圆圈:
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(randomPositionX, randomPositionY, randomSize, randomSize);
g.setFont(new Font("TimesRoman", Font.PLAIN, randomSize));
g.setColor(Color.RED);
g.drawString(alphabets.charAt(position) + "", randomPositionX + (randomSize/2), randomPositionY + (randomSize/2));
}
但它不会在圆圈的中心画出字母。简而言之,有问题的一行是
g.drawString(alphabets.charAt(position) + "", randomPositionX + (randomSize/2), randomPositionY + (randomSize/2));
我如何实现我的目标?
MCVE按要求:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Font;
import java.util.Random;
class MCVE{
static int randomSize = 0;
static int randomPositionX = 0;
static int randomPositionY = 0;
static int position = 0;
public final static int MAX_SIZE = 100;
public final static int MIN_SIZE = 10;
public final static int WIDTH = 500;
public final static int HEIGHT = 500;
private static Random rand = new Random();
static String alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String[] argv){
randomSize = MIN_SIZE + (rand.nextInt(MAX_SIZE - MIN_SIZE) + 1);
randomPositionX = rand.nextInt(WIDTH - randomSize);
randomPositionY = rand.nextInt(HEIGHT - randomSize);
position = rand.nextInt(alphabets.length());
JFrame frame = new JFrame("Test");
JPanel panel = new JPanel(){
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(randomPositionX, randomPositionY, randomSize, randomSize);
g.setFont(new Font("TimesRoman", Font.PLAIN, randomSize));
g.setColor(Color.RED);
g.drawString(alphabets.charAt(position) + "", randomPositionX + (randomSize/2) - 15, randomPositionY + (randomSize/2) + 15);
}
};
frame.add(panel);
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
问题:字母没有画在圆圈的中心。
答案 0 :(得分:1)
图形drawString
在指定的位置绘制文本左下角的字符。你需要做一些数学计算,以便按照你想要的方式集中它们。
首先,我们需要从我们正在使用的FontMetrics
对象中获取Graphics
。据推测,您已经将字体更改为您想要的字体。
FontMetrics metrics = g.getFontMetrics(g.getFont());
现在,我们需要找到我们将要绘制的文本的大小:
Rectangle2D bounds = metrics.getStringBounds(text, g);
接下来,使用这些边界来偏移正在绘制的文本:
int offsetX = -bounds.getWidth()/2;
int offsetY = -bounds.getHeight()/2;
g.translate(offsetX, offsetY);
g.drawString(text, x, y);
g.translate(-offsetX, -offsetY);
或者,将偏移量直接捆绑到绘图调用中
int offsetX = -bounds.getWidth()/2;
int offsetY = -bounds.getHeight()/2;
g.drawString(text, x+offsetX, y+offsetY);