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

后端 未结 1 1847
挽巷
挽巷 2021-01-14 19:04

I am new to javafx, and I\'m programming a game using its rendering functions, specifically, GraphicsContext.fillArc() and the like in eclipse.

相关标签:
1条回答
  • 2021-01-14 19:35

    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);
    }
    
    0 讨论(0)
提交回复
热议问题