I want to show 5 numbers that each number will show orderly after the before show for seconds but the result is its waits until the function end to show the numbers. here is my code.
public class Main extends Application{
public static void main(String[] args) {
// TODO Auto-generated method stub
launch();
}
@Override
public void start(Stage arg0) throws Exception {
// TODO Auto-generated method stub
StackPane p = new StackPane();
Canvas c = new Canvas(400,400);
Button btn = new Button("start");
btn.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent arg0) {
// TODO Auto-generated method stub
draw(c);
}
});
p.getChildren().addAll(c,btn);
Scene s = new Scene(p);
arg0.setTitle("test canvas");
arg0.setScene(s);
arg0.show();
}
private void draw(Canvas c) {
// TODO Auto-generated method stub
GraphicsContext gc = c.getGraphicsContext2D();
Font f = Font.font("Time New Roman",FontWeight.BOLD,42);
gc.setFont(f);
gc.setFill(Color.RED);
for(int i = 0;i<5;i++) {
gc.fillText(""+i, 60+i*40, 100);
try {
Thread.sleep(i*200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
**the number will show after user clicked the button below