Anti-aliasing – Creating anti-alias text in Java

The Java Graphics 2D package makes anti-aliasing extremely easy.

Anti-aliasing is made easy by the setRenderingHint method of the Graphics2D class.  Example:

BufferedImage = new BufferedImage(imageWidth, imageHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2d = bufferedimage.createGraphics();
graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

Now, any text you draw with the Graphics2D instance you created, will be anti-aliased.  This is especially nice when there is text rotation involved as it maintains the smooth text appearance that is often lost in rotation algorithms.

graphics2d.drawString("My Text", 0, 0);

Keep in mind that Graphics2D will only render the text when using the Graphics2D methods to draw the text as in the above code segment.   If you create a TextLayout class, then use the draw method of your TextLayout instance to draw text on a Graphics2D object, the text will not be rendered.