如何使像素完美Line2D - Graphics2D

时间:2022-06-09 12:09:55

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,

G'day,我有JPanel,上面有一些Line2D对象。问题是,当我画这条线时,它不会像我想要的那样出现。线条不平滑,很难用词来解释,所以我发布了一张图片,

如何使像素完美Line2D  -  Graphics2D

Zoomed Area,

缩放区域,

如何使像素完美Line2D  -  Graphics2D

How to make them look more polished rather than wrinkly.

如何让它们看起来更光滑而不是皱纹。

Thanks

谢谢

1 个解决方案

#1


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).

(当然,图形是你的Graphics2D实例)。

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.

但是,文字有点复杂;这个提示有几个值,你可以根据(除其他外)你正在绘制文本的屏幕类型来使用。您应该阅读RenderingHints.KEY_TEXT_ANTIALIASING API文档以获取这些详细信息。

#1


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).

(当然,图形是你的Graphics2D实例)。

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.

但是,文字有点复杂;这个提示有几个值,你可以根据(除其他外)你正在绘制文本的屏幕类型来使用。您应该阅读RenderingHints.KEY_TEXT_ANTIALIASING API文档以获取这些详细信息。