我需要在图片的中心写文字。要写的文本并不总是相同的。
我使用的代码在这里:
// Here I first draw the image
g.drawImage(img, 22, 15, 280, 225, null);
// I get the text
String text = photoText.getText();
// Set the text color to black
g.setColor(Color.black);
// I draw the string
g.drawString(text, 79.5F, 220.0F);
问题是文本不在图像的中心,我该怎么办?
我只需要在水平居中绘制文本。
最佳答案
使用 JLabel
的工作较少,但是 FontMetrics
, 如图所示 here , 将让您直接管理几何图形。
关于Java - 在图像的中心绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7323538/