Anti-Aliasing in Java >=1.3

It's really simple. Just add the a few lines of code at the top of your paintComponent method:

protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2d = (Graphics2D) g;
  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  . . .
  . . .
}

The first is for shapes, the second is for text, and the third is to go for high quality (over speed).

Actually in 1.5 you can just pass -Dswing.aatext=true to the VM, but this didn't work with one of my applications for some reason.

Tags:

Add new comment