10

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

TeaCupApp
  • 11,316
  • 18
  • 70
  • 150
  • 3
    Don't know java well, and don't know swing well: but I think your looking for anti-aliasing. – Dair Sep 07 '11 at 04:59

1 Answers1

10

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.

Adrian Petrescu
  • 16,629
  • 6
  • 56
  • 82
  • Thanks I just edited your code, Concept was correct but setRenderingHits requires Map object. so I changed it to setRenderingHint and constants are in RenderingHints instead of Graphics2D. :) – TeaCupApp Sep 07 '11 at 05:12
  • Whoops, you're right, though I had accidentally overwritten your edit when I was adding some more details. I fixed it now, `setRenderingHints->setRenderingHint`. Thanks! – Adrian Petrescu Sep 07 '11 at 05:17
  • Anti-aliasing should be on by default IMO (it isn't, but someone at Oracle should make it so) – Luigi Plinge Sep 07 '11 at 14:09