Java - 在图像的中心绘制文本 [英] Java - Draw text in the center of an image

查看:133
本文介绍了Java - 在图像的中心绘制文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在图像的中心写文字。要写的文字并不总是相同的。



我使用的代码在这里:

  //这里我首先绘制图像
g.drawImage(img,22,15,280,225,null);
//我得到文本
String text = photoText.getText();
//设置文字颜色为黑色
g.setColor(Color.black);
//我绘制字符串
g.drawString(text,79.5F,220.0F);

问题是文本不在图像的中心,我该怎么办?

我只需要在水平中心绘制文字。 使用 JLabel 不太方便,但 FontMetrics ,如图此处,将让你直接管理几何。


I need to write text in the center of an image. The text to write is not always the same.

The code I'm using is here:

// 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);

The problem is that the text isn't at the center of the image, what can I do?

I only need to draw the text at the horizontal center.

解决方案

Using a JLabel is less work, but FontMetrics, shown here, will let you manage the geometry directly.

这篇关于Java - 在图像的中心绘制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆