Search code examples
performancecanvasjavafx

JavaFX Canvas Rendering Performance


I work on a JavaFX application and have to draw a lot (4000) of arc's. I'm using the JavaFX Canvas method strokeArc of the graphic context.

The application performs very weak, but not the calls of the methods strokeArc, I think the delay occurs later, maybe at the rendering. The results is, that the application does take a delay about 5 seconds to show the arc's on the gui.

Is there a faster way to draw the arc's in JavaFX?


Solution

  • No, there isn't. Canvas is the fastest.

    You didn't provide a MCVE. I just tried a simple application. Works pretty fast, 4000 arcs drawn in 6ms. The application window shows up immediately. Here's the code:

    import javafx.application.Application;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.canvas.Canvas;
    import javafx.scene.canvas.GraphicsContext;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.ArcType;
    import javafx.stage.Stage;
    
    public class ArcTest extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
    
            Group root = new Group();
            Canvas canvas = new Canvas(300, 250);
    
            GraphicsContext gc = canvas.getGraphicsContext2D();
    
            root.getChildren().add(canvas);
            primaryStage.setScene(new Scene(root));
            primaryStage.show();
    
            drawShapes(gc);
    
        }
    
        private void drawShapes(GraphicsContext gc) {
    
            long time = System.currentTimeMillis();
    
            gc.setFill(Color.GREEN);
            gc.setStroke(Color.BLUE);
            gc.setLineWidth(5);
            for( int i=0; i < 4000; i++) {
                gc.strokeArc(10, 160, 30, 30, 45, 240, ArcType.OPEN);
            }
    
            System.out.println( System.currentTimeMillis() - time);
        }
    }
    

    Unless you provide some code with more information (BlendMode, etc) it's hard to help you.