How to make pixel perfect Line2D in - Graphics2D

2020-08-23 02:06发布

问题:

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,

Zoomed Area,

How to make them look more polished rather than wrinkly.

Thanks

回答1:

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.