0

enter image description here

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

ProgramZa
  • 97
  • 1
  • 1
  • 4
  • See [my answer](https://stackoverflow.com/a/60685975/6395627) to the duplicate. You'll want to use the `javafx.animation` package. – Slaw May 14 '22 at 08:16
  • See [Concurrency in JavaFX](https://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm) for a better understanding of what's going on. JavaFX is built with animation at the core, you really need to take the time to learn and understand it, start with [Documentation](https://docs.oracle.com/javase/8/javase-clienttechnologies.htm), look at the "Effects, Animation and Media" section – MadProgrammer May 14 '22 at 08:19

0 Answers0