我有一个String
,我想把它画到一个图像上。我能够画点和画线,但是,即使在阅读了2D 图形教程的文本部分之后,我也无法弄清楚如何将 aString
画到我的绘图上。
除非我看错了教程(但每当我搜索有关 Java 和使用Graphics
or绘制字符串时,我都会得到这个教程Graphics2D
),我仍然很难过。
我有一个String
,我想把它画到一个图像上。我能够画点和画线,但是,即使在阅读了2D 图形教程的文本部分之后,我也无法弄清楚如何将 aString
画到我的绘图上。
除非我看错了教程(但每当我搜索有关 Java 和使用Graphics
or绘制字符串时,我都会得到这个教程Graphics2D
),我仍然很难过。
看看下面的方法。
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);