Question

I have some problem with string antialiasing in Java. If I use this code

g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

on normal (not rotated string), all is fine. But, when I try use antialiasing on rotated image, where I draw string - this string rendering without antialiasing feature.

This is example of my code, where antialiasing doesn't work:

Graphics2D g = originalImage.createGraphics();

AffineTransform affineTransform2 = new AffineTransform();
affineTransform2.setToIdentity();
affineTransform2.translate(125, 325);
affineTransform2.rotate(Math.toRadians(345));

BufferedImage positionImage = new BufferedImage(100, 40, BufferedImage.TYPE_INT_ARGB);
Graphics2D positionImageG2D = positionImage.createGraphics();
positionImageG2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
positionImageG2D.setFont(getFont(30f));
positionImageG2D.drawString("Some Text",100, 40);
positionImageG2D.dispose();

g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.drawImage(positionImage, affineTransform2, null); 
Was it helpful?

Solution

You don't draw text to g (last line), so whatever value you pass to KEY_TEXT_ANTIALIASING won't have any effect.

You are drawing a rotated image with already antialiased text. Try setting KEY_INTERPOLATION to VALUE_INTERPOLATION_BICUBIC or VALUE_INTERPOLATION_BILINEAR, which should effect scaling and rotation of images.

Another thing you could try, is to draw the text (rotated) directly onto g, without the temporary image. Should also work, but you might have to adjust positioning relative to the rotation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top