我是Java的新手,并尝试使用Java2D Graphics创建图像。但是输出却是抗锯齿的。我尝试了多种方法来纠正它,但是没有用。字符变得失真或锯齿。public BufferedImage createNameOnButton(String label) { int messageWidth = 0; Font font = new Font("Arial", Font.PLAIN, 11); BufferedImage bi = new BufferedImage( 10, 10, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = (Graphics2D) bi.getGraphics(); g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2d.setRenderingHint( RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); g2d.setFont(font); bi = g2d.getDeviceConfiguration() .createCompatibleImage(500, 30, Transparency.BITMASK); FontMetrics fm = bi.getGraphics().getFontMetrics(font); int messageHeight = fm.getHeight() - fm.getDescent(); for (char ch : label.toCharArray()) { messageWidth += fm.charWidth(ch); } bi = bi.getSubimage(50, 0, messageWidth + 10, fm.getHeight()); Graphics g = bi.getGraphics(); g.setColor(Color.black); AttributedString as = new AttributedString(label); as.addAttribute(TextAttribute.FONT, font); g.drawString(as.getIterator(), 5, messageHeight); g2d.dispose(); g.dispose(); return bi;}谁能帮我纠正错误?
3 回答
慕仙森
TA贡献1827条经验 获得超7个赞
正如Traroth所提到的,这很可能是由于
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
相反,这应该是
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
g2d.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
哈士奇WWW
TA贡献1799条经验 获得超6个赞
我发现仅此行就可以解决问题:
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
之后,只需使用drawString()即可。
添加回答
举报
0/150
提交
取消