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;

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;



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.

  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

    Worked perfectly without any modifications 😀 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.drawImage(image, 0, 0, null);

    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. April 17, 2015 at 3:14 am
  2. August 21, 2016 at 11:13 pm
  3. May 13, 2019 at 9:46 am

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 )

Connecting to %s

%d bloggers like this: