You need to interface with the Canvas on the JavaFX-Thread, or even better don't use a Thread use Timeline with a KeyFrame.
Tom On 16.03.16 23:25, ZHANG Erkang wrote: > Hello everyone, > > > I'm trying to display a million ovals in a canvas dynamicly every second. I > use a thread as the rendering thread, but my problem is, after a few > secondes, the canvas freezes and stops displaying. I guess that the buffer is > full so it can't display anymore, but how can I clear up the buffer? The test > source code is as follow: > > > public class Main extends Application { > > public void start(Stage primaryStage) { > primaryStage.setTitle("Drawing Operations Test"); > Group root = new Group(); > Canvas canvas = new Canvas(800, 800); > GraphicsContext gc = canvas.getGraphicsContext2D(); > drawShapes(gc); > root.getChildren().add(canvas); > primaryStage.setScene(new Scene(root)); > primaryStage.show(); > Task task2 = new Task<Void>() > { > public synchronized Void call() throws Exception { > while (true) { > Thread.sleep(1000); > Canvas canvas = gc.getCanvas(); > canvas.getGraphicsContext2D().clearRect(0, 0, > canvas.getHeight(), canvas.getWidth()); > drawShapes(canvas.getGraphicsContext2D()); > > } > } > }; > Thread t = new Thread(task2); > t.start(); > } > > private void drawShapes(GraphicsContext gc) { > gc.setFill(Color.GREEN); > gc.setStroke(Color.BLUE); > gc.setLineWidth(1); > double widthOval=1; > double heightOval = 1; > for (int i = 0; i < 500; ++i) { > for (int j = 0; j < 500; ++j) { > > if (Math.random() < 0.5) { > gc.fillOval(i * widthOval, j * heightOval, widthOval, > heightOval); > } > else { > gc.strokeOval(i * widthOval, j * heightOval, widthOval, > heightOval); > } > } > } > } > > public static void main(String[] args) { > launch(args); > } > } > > > > Can anyone help me? > > Thanks in advance, > > Pierre > -- Thomas Schindl, CTO BestSolution.at EDV Systemhaus GmbH Eduard-Bodem-Gasse 5-7, A-6020 Innsbruck http://www.bestsolution.at/ Reg. Nr. FN 222302s am Firmenbuchgericht Innsbruck