2012-03-09 26 views
0

它是可以通過看pdf.png在新的照片,但不能看到字符串「Hello」的新圖片爲什麼我不能用直觀的圖形方法drawString之字符串

public void generateImage() throws Exception{ 
     int width = 220; 
     int height = 50; 
     BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); 
     Graphics g = image.getGraphics(); 
     g.setColor(new Color(255,255,255)); 
     g.fillRect(0, 0, width, height); 
     Font font = new Font("宋體",Font.BOLD,10); 
     g.setFont(font); 
     BufferedImage image2 = ImageIO.read(new File("data/icon/pdf.png")); 
     g.drawImage(image2, 0, 0, 44, 42, null); 
     g.drawString("Hello", 50, 5); 
     g.dispose(); 
     File f = new File("data/icon/"+fileName+".png"); 
     FileOutputStream fos = new FileOutputStream(f); 
     ImageIO.write(image,"PNG",fos); 
     fos.close(); 
} 
+1

爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-03-09 07:37:18

回答

2
g.drawString("Hello", 50, 5); 

在這一行上操作座標值,即50和5,並測試是否出現文本「Hello」。

+0

'getFontMetrics()。getAscent()'將是_y_值的下限來嘗試。 – trashgod 2012-03-09 07:14:41

2

它看起來像你正在設置顏色爲白色,從不設置任何其他顏色。結果是極低的對比度。

+0

g.drawImage(image2,0,0,80,80,null); g.setColor(Color.red); g.drawString(「Hello」,50,5); – xxddwdj 2012-03-09 07:27:38

+0

這有幫助嗎?試試'g.drawString(「Hello」,50,12)'。另請參閱此[示例](http://stackoverflow.com/a/2658663/230513)。 – trashgod 2012-03-09 11:41:18

相關問題