0

有人可以在这里检查我的语法吗?我将“Times New Roman”、“Arial”、“Verdana”传递给fontName并使用 8、12、15 等fontSize。它永远不会改变这里的字体。我这样做是为了在图像上写一些文字。

Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
g2d.drawImage(photo, 0, 0, null);
g2d.setColor(Color.white);
Font font = new Font(fontName, Font.PLAIN, fontSize);
g2d.setFont(font);
g2d.drawString(text,x,y);
4

2 回答 2

2

我终于发现系统上没有我列表中的字体,所以我不得不使用 getAllFonts() 方法并只传递列表中的那些字体。

于 2011-03-30T20:19:44.893 回答
0

你应该这样做

BufferedImage img = new BufferedImage(
    w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(photo, 0, 0, null);
g2d.setPaint(Color.red);
//example :     g2d.setFont(new Font("Serif", Font.BOLD, 15));
g2d.setFont(new Font(fontName, Font.BOLD, size));
String s = "Hello, world!";
// assuming x & y is set using graphic's font metrics
g2d.drawString(s, x, y);
g2d.dispose();

摘自太阳文档

获取图形

public Graphics getGraphics() 此方法返回一个 Graphics2D,但在这里是为了向后兼容。createGraphics 更方便,因为它被声明为返回 Graphics2D。

这并不意味着您不应该使用getGraphicsAPI。只是上面的代码对我有用:)

于 2011-03-30T19:46:16.037 回答