How to apply antialiasing to javafx methods like GraphicsContext.fillArc()?

馋奶兔 提交于 2019-12-01 09:09:56
Sleafar

I encountered a similar problem while playing around with Canvas and AnimationTimer. An important key to the solution is the comment posted by @jewelsea:

"... if you comment out the animation timer and just draw the arc without it, then the arc is nicely antialiased ..."

This basically means, if we draw the arc once it is antialiased. But if we draw it in the handle() method of the AnimationTimer, it is drawn 60 times per second. The results can be seen better after adding a long sleep to the handle() method. The first time the circle is rendered on a white background and looks as desired. The second time a blue circle is rendered on top of the previous blue circle. The pixels which were colored partially blue because of antialiasing, become darker. The same happens when the circle is drawn the third time, and so on. The following image shows a magnified portion of the arc after 1, 2, 3, 4, 5, 10, and 50 iterations:

A simple solution to the problem is to fill the canvas with the background color, before rendering anything:

public void handle(long currentNanoTime) {
    gc.setFill(Color.WHITE);
    gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());

    gc.setFill(Color.BLUE);
    gc.fillArc(150, 150, 100, 100, 0, 240, ArcType.ROUND);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!