我有一个字符串,想将其绘制到图像上。我能够绘制点和线,但是即使阅读了2D图形教程的文本部分,我也无法弄清如何将一个字符串绘制到我的图形上。除非我看错了教程(但这是我在搜索任何关于使用
Graphics
或Graphics2D
绘制字符串的Java内容时得到的教程),否则我仍然被卡住了。Graphics
或Graphics2D
绘制字符串的Java内容时得到的教程),否则我仍然被卡住了。请查看以下方法。
g.drawString();
drawString()
方法将实现您所需的功能。
下面是一个使用示例:
protected void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.drawString(5, 40, "Hello World!");
}
String
的左下角。Graphics2D yourGraphicsContext=(...);
Font f= new Font("Dialog",Font.PLAIN,14);
FontRenderContext frc = yourGraphicsContext.getFontRenderContext();
TextLayout tl = new TextLayout(e.getTextContent(), f, frc);
Shape shape= tl.getOutline(null);
//here, you can move your shape with AffineTransform (...)
yourGraphicsContext.setColor(Color.RED);
yourGraphicsContext.fill(shape);
yourGraphicsContext.setColor(Color.BLUE);
yourGraphicsContext.draw(shape);
drawString()
。 - jjnguy