Home > Java, programming > Quick hit – antialiasing in Java Graphics2D

Quick hit – antialiasing in Java Graphics2D


If you’re trying to use antialiasing in Java, chances are you’ll do some searching and come to the conclusion that you have to set the rendering hints for the Graphics object in order to eliminate your jaggies. Probably like so:

Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

And you’ll probably wonder why your graphics are still jagged. That’s because you haven’t instructed Java what type of interpolation to use for the antialiasing; by default you’ll get Nearest Neighbor, which is fast but won’t get you far in the image department. You can instead choose to use bilinear interpolation (slower) or bicubic interpolation (slowest).

Here’s how to enable the alternate interpolation modes:

Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);

or

g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC)

You make a trade off between speed and image quality; to my eyes bilinear looks fine. Compare for yourself:

Thanks to Chris Cambpell’s blog for showing me this.

About these ads
  1. Will
    September 11, 2013 at 9:58 pm

    Are you kidding me? I just tossed that code in there and everything started working perfectly without even a single syntax error. What the hell. Thanks a bunch!

    • i82much
      September 11, 2013 at 11:22 pm

      Glad this worked for you. I remember being very frustrated when it didn’t work out of the box as it seemed like it should have.

  2. Eduardo R
    September 19, 2013 at 4:27 pm

    Great!!
    Worked perfectly without any modifications :D You made my day haha thanks

  3. vishalkpp9
    December 23, 2013 at 4:04 am

    Hey, the code looks okay but I do not see any improvement at my end. My method is as follows:

    public static BufferedImage getAntiAliasedImage(BufferedImage image) {

    BufferedImage bimage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2 = bimage.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
    RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    g2.drawImage(image, 0, 0, null);
    g2.dispose();

    return bimage;
    }

    Do you see any problem here?

    Thanks in advance! :)

  4. September 21, 2014 at 12:00 am

    At the risk of coming accross as an idiot….Would this help the stuttering and awkward uneven movement I am getting in my swing animations ? -Obviously I ‘m going to try it now anyways ;d

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: