在Java中更改字体和绘制字符串的正确语法是什么?

有人可以检查我的语法吗?我将“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);
    
已邀请:
我终于发现我的列表中没有任何字体存在于系统中,因此我不得不使用getAllFonts()方法并仅从列表中传递这些字体。     
你应该这样做
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();
从太阳文档中摘录   的getGraphics      public Graphics getGraphics()这个   方法返回一个Graphics2D,但是   这里是为了向后兼容。   createGraphics更方便,   因为它被声明返回一个   Graphics2D的。 这并不意味着您不应该使用
getGraphics
API。只是上面的代码对我有用:)     

要回复问题请先登录注册