3

我有一个String,我想把它画到一个图像上。我能够画点和画线,但是,即使在阅读了2D 图形教程的文本部分之后,我也无法弄清楚如何将 aString画到我的绘图上。

除非我看错了教程(但每当我搜索有关 Java 和使用Graphicsor绘制字符串时,我都会得到这个教程Graphics2D),我仍然很难过。

4

2 回答 2

8

看看下面的方法。

g.drawString();

drawString()方法将满足您的需求。

使用示例:

protected void paintComponent(Graphics g){
    g.setColor(Color.BLACK);
    g.drawString(5, 40, "Hello World!");
}

String请记住,坐标是关于您正在绘制的左下角的。

于 2009-07-30T12:12:47.360 回答
3

如果您想玩弦的形状(例如:填充:红色和笔划:蓝色):

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);
于 2009-07-30T12:28:15.977 回答