Draw using Antialiasing example | Examples Java Code Geeks

- RSS
- Email
- Follow us
- Become a fan
- LinkedIn
- G+

# Draw using Antialiasing example

Filed in: awt Tags: awt, desktop java

In this example we are going to see how to draw an image with Antialiasing enabled. The notion of antialiasing is one of the most famous among the graphics world. This will help you to make sharper graphics and make your images look very clear and avoid pixelation.

In short, in order to enable antialiasing in your drawing, you should:

- Use `Graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);` to turn antialiasin on.
- Use `Graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);` to turn antialiasing off.

Let’s see the code snippet that follows:

view source
print?

`001` | `package` `com.javacodegeeks.snippets.desktop;`

`002` |

`003` | `import` `java.awt.Component;`

`004` | `import` `java.awt.Font;`

`005` | `import` `java.awt.FontMetrics;`

`006` | `import` `java.awt.Frame;`

`007` | `import` `java.awt.Graphics;`

`008` | `import` `java.awt.Graphics2D;`

`009` | `import` `java.awt.RenderingHints;`

`010` |

`011` | `public` `class` `AntialiasingDrawing {`

`012` |

`013` | `` `public` `static` `void` `main(String[] args) {`

`014` |

`015` | `// Create a frame`

`016` |

`017` | `Frame frame =` `new` `Frame();`

`018` |

`019` | `// Add a component with a custom paint method`

`020` |

`021` | `frame.add(``new` `CustomPaintComponent());`

`022` |

`023` | `// Display the frame`

`024` |

`025` | `int` `frameWidth =` `300``;`

`026` |

`027` | `int` `frameHeight =` `300``;`

`028` |

`029` | `frame.setSize(frameWidth, frameHeight);`

`030` |

`031` | `frame.setVisible(``true``);`

`032` |

`033` | `` `}`

`034` |

`035` | `` `/**`

`036` | `` `* To draw on the screen, it is first necessary to subclass a Component`

`037` | `` `* and override its paint() method. The paint() method is automatically called`

`038` | `` `* by the windowing system whenever component's area needs to be repainted.`

`039` | `` `*/`

`040` | `` `static` `class` `CustomPaintComponent` `extends` `Component {`

`041` |

`042` | `public` `void` `paint(Graphics g) {`

`043` |

`044` | `` `// Retrieve the graphics context; this object is used to paint shapes`

`045` |

`046` | `` `Graphics2D g2d = (Graphics2D)g;`

`047` |

`048` | `` `/**`

`049` | `` `* The coordinate system of a graphics context is such that the`

`050` | `` `* origin is at the northwest corner and x-axis increases toward the`

`051` | `` `* right while the y-axis increases toward the bottom`

`052` | `` `*/`

`053` |

`054` | `` `int` `x =` `0``;`

`055` |

`056` | `` `int` `y =` `0``;`

`057` |

`058` | `` `int` `width = getSize().width-``1``;`

`059` |

`060` | `` `int` `height = getSize().height-``1``;`

`061` |

`062` | `` `// Enable antialiasing for shapes`

`063` |

`064` | `` `g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);`

`065` |

`066` | `` `// Draw an oval that fills the window`

`067` |

`068` | `` `g2d.drawOval(width/``8``,height/``8``,` `3``*width/``4``,` `3``*height/``4``);`

`069` |

`070` | `` `// Disable antialiasing for shapes`

`071` |

`072` | `` `g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);`

`073` |

`074` | `` `// Draw an oval that fills half window`

`075` |

`076` | `` `g2d.drawOval(width/``4``, height/``4``, width/``2``, height/``2``);`

`077` |

`078` | `` `// Enable antialiasing for text`

`079` |

`080` | `` `g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);`

`081` |

`082` | `` `// Set the desired font if different from default font`

`083` |

`084` | `` `Font font =` `new` `Font(``"Serif"``, Font.PLAIN,` `12``);`

`085` |

`086` | `` `g2d.setFont(font);`

`087` |

`088` | `` `FontMetrics fontMetrics = g2d.getFontMetrics();`

`089` |

`090` | `` `// Draw a string such that the top-left corner is at x, y`

`091` |

`092` | `` `g2d.drawString(``"Antialiazing is ON"``, x, y+fontMetrics.getAscent());`

`093` |

`094` | `` `// Disable antialiasing for text`

`095` |

`096` | `` `g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);`

`097` |

`098` | `` `// Draw a string below the last one`

`099` |

`100` | `` `g2d.drawString(``"Antialiazing is OFF"``, x, y+``2``*fontMetrics.getAscent());`

`101` |

`102` | `}`

`103` |

`104` | `` `}`

`105` |

`106` | `}`

This was ane example on how to draw using Antialiasing.

You might also like:

- Draw text example
- Draw using Alpha example
- Draw shapes example
- Draw using the stroking pen example
- Draw mixed style text example

Share and enjoy!

© 2010-2012 Examples Java Code Geeks. Licenced under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.