在 Paint XP 或 Paint Windows 8 中,当您书写文本时,程序会自动提高字体质量(至少对于财务打印机而言)。我想知道如何使用下面的相同 Java 代码执行此操作。
首先,看这张图片明白我的意思:
BufferedImage image = ImageIO.read(new File("blankdocument.bmp"));
Graphics g = ((BufferedImage) image).getGraphics();
Font helvetica = new Font("Lucida Sans Unicode", Font.PLAIN, 13);
g.setColor(Color.black);
g.setFont(helvetica);
g.drawString("TEXT WRITING EXAMPLE.", 5, 10);
ImageIO.write(image, "PNG", new File("testx.PNG"));
image.flush();
最佳答案
您可以打开抗锯齿。来自 Controlling Rendering Quality ,
To set or change the rendering hints attribute in the
Graphics2D
context, construct aRenderingHints
object and pass it intoGraphics2D
by using thesetRenderingHints
method. If you just want to set one hint, you can callGraphics2D
setRenderingHint
and specify the key-value pair for the hint you want to set. (The key-value pairs are defined in theRenderingHints
class.)
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setRenderingHints(rh);
关于buffer - 如何提高图形的 drawString 方法的字体质量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38177674/