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