Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make pixel perfect Line2D in - Graphics2D

G'day, I have JPanel with some Line2D objects on it. Issue is when I draw this line it doesn't appear as I want them to. Lines are not smooth, It's hard to explain in word so I am posting an Image,

enter image description here

Zoomed Area,

enter image description here

How to make them look more polished rather than wrinkly.

Thanks

like image 445
TeaCupApp Avatar asked Sep 07 '11 04:09

TeaCupApp


1 Answers

The problem is likely that you don't have antialiasing turned on your Graphics context. Try the following line before you draw:

graphics.setRenderingHint(
        RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);

(where, of course, graphics is your Graphics2D instance).

Later on when you discover that the text you're drawing is also ugly and jagged, you'll want to use

graphics.setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

However, text is a little more complicated; there's several values for this hint that you can use depending on (among other things) the type of screen you're drawing the text to. You should read the RenderingHints.KEY_TEXT_ANTIALIASING API doc for those details.

like image 167
Adrian Petrescu Avatar answered Sep 29 '22 12:09

Adrian Petrescu