与Graphics2D的Java模糊的背景

时间:2015-08-23 14:41:25

标签: java graphics2d

我正在使用Java来创建带有文本的图像。我设置背景的颜色并填充它。然后我使用LineBreakMeasurer放置文本。问题是(如附图所示)文本周围的图像背景模糊(如果放大)。这对我来说是个问题。有可能解决这个问题吗?

final BufferedImage image = new BufferedImage(600, 400, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = image.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setColor(new Color(255,0,0));
    g2d.fillRect (0, 0, image.getWidth(), image.getHeight());

...

AttributedString attributedString = getAttributedString(text, fontSize, new Color(1.0f,1.0f,1.0f,0.9f));
    AttributedCharacterIterator characterIterator = attributedString
            .getIterator();
    FontRenderContext fontRenderContext = g2d.getFontRenderContext();
    LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator,
            fontRenderContext);
    while (measurer.getPosition() < characterIterator.getEndIndex()) {
        TextLayout textLayout = measurer.nextLayout(width);
        int currentX = x + (int)(width - textLayout.getVisibleAdvance()) / 2;
        y += textLayout.getAscent();

        textLayout.draw(g2d, currentX, y);
    }

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您指的是字母周围的文物,问题不在于您的绘画代码。这是将图像保存为JPEG的结果。使用非损耗格式保存,如PNG。您也可以尝试减少JPEG的压缩,但保存为PNG可能更容易。