I'm using javafx in javase8 and netbeans 8.0.2, I made randomly generated shape images and show them sequentially with timeline. But last image isn't shown. timeline.setcyclecount(12) i use java generate 12 images but doesn't show 12. image in timeline.
public class JavaFXApplication3 extends Application {
int k;
Timeline timeline;
class ResizableCanvas extends Canvas {
private void draw() {
int[] uyaran = {3, 7, 12};
boolean[] type = new boolean[12];
for (int i = 0; i < 12; i++) {
type[i] = false;
}
for (int v : uyaran) {
type[v - 1] = true;
}
double w = getWidth();
double h = getHeight();
GraphicsContext gc = getGraphicsContext2D();
gc.clearRect(0, 0, w, h);
gc.setFill(Color.RED);
System.out.println(k);
if (type[k]) {
gc.fillOval(0, 0, w, h);
}
k++;
}
}
@Override
public void start(Stage stage) throws Exception {
k = 0;
ResizableCanvas canvas = new ResizableCanvas();
timeline = new Timeline(new KeyFrame(Duration.millis(1000), ae -> canvas.draw()));
timeline.setCycleCount(12);
timeline.setOnFinished(ActionEvent -> {
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {}
stage.close();
});
timeline.play();
Pane pane = new Pane();
pane.getChildren().add(canvas);
canvas.widthProperty().bind(pane.widthProperty());
canvas.heightProperty().bind(pane.heightProperty());
stage.setScene(new Scene(pane));
stage.show();
}
}
You are calling Thread.sleep(...)
on the FX Application Thread, which blocks the thread and prevents it from updating. The final ellipse is only actually rendered when the pause is over, but of course you then close the window so you never see it.
Use a PauseTransition
to pause, and use its onFinished
handler to do something when the pause is over:
timeline.setOnFinished(ae -> {
PauseTransition pause = new PauseTransition(Duration.seconds(10));
pause.setOnFinished(event -> stage.close());
pause.play();
});