2

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?

jojo2100
  • 23
  • 1
  • 5

1 Answers1

4

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.

Roland
  • 18,114
  • 12
  • 62
  • 93
  • 2
    Maybe the built-in JavaFX Canvas Graphics Performace Optimizer realizes that drawing 4000 times an identical arc can be replaced by a single one here :-) – mipa Aug 10 '15 at 06:55
  • Thank you. I've tried your source code and it's fast for 4000 arcs. The problem is, that I was wrong with the number of arcs. I draw not only 4000 arcs, I have to draw 70,000 arcs. And for this, your source code is also slow. I think there is no better solution? – jojo2100 Aug 11 '15 at 17:30
  • Please try your test program with 70,000 arcs. You will see, the code is finished after 25 milliseconds, but the Java rendering has a very long delay. – jojo2100 Aug 11 '15 at 17:38
  • @mipa yesterday: no, there's no optimizer like that – Roland Aug 12 '15 at 04:50
  • 1
    @jojo2100: you should phrase your question correctly then. 4000 and 70000 is a HUGE difference. The arcs are written into a GrowableDataBuffer and painted later. That's why you have that delay. – Roland Aug 12 '15 at 04:52
  • @Roland: I know, it is my fault. I have not realized this huge number of arcs before. So you mean, that the memory allocation of this buffer takes some time? Is there an possibility to get my application responsive, like with an task on a seperate thread? – jojo2100 Aug 12 '15 at 17:21
  • @jojo2100: Not the memory allocation, the drawing. You are facing the same problem that I have with [the particles](http://stackoverflow.com/questions/31613669/performance-for-particle-system). I doubt that you'll get it more responsive. I tried by splitting my app into several canvases, doing it parallel and then merging them. Didn't give any performance improvement. – Roland Aug 12 '15 at 17:38