Task 2: Text Counter (USING JAVAFX) Develop a class TextCounter that extends javafx.scene.text.Text and implements Runnable. Your layout should appear similar to the image below. Next, develop a clas

 Task 2: Text Counter (USING JAVAFX)

Task 2: Text Counter (USING JAVAFX) Develop a class TextCounter that extends javafx.scene.text.Text and implements Runnable. Your layout should appear similar to the image below. Next, develop a clas

Develop a class TextCounter that extends javafx.scene.text.Text and implements Runnable. Your layout should appear similar to the image below.  Next, develop a class IntCounter that has an integer counter intialised to 0, and methods incrementCount, getCount and setCount. 

For this application, create Start, Pause and Resume buttons. When Start is pressed, the counter starts from zero. When Pause is pressed, the counter pauses and displays the current count. When Resume is pressed, the counter continues from a paused state (or does nothing if the counter is already running). You should use Platform.runLater to implement

the counter to ensure it updates correctly on screen. 


Solution

import java.util.concurrent.Executors;

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.layout.StackPane;

import javafx.scene.layout.VBox;

import javafx.scene.text.Text;

import javafx.stage.Stage;

import javafx.scene.layout.GridPane ;

import javafx.geometry.Pos;

import javafx.scene.layout.HBox ;

import javafx.geometry.VPos;

import javafx.scene.layout.Region ;

import javafx.animation.Timeline;

import javafx.scene.text.Font ;

import javafx.scene.text.FontWeight ;

import javafx.scene.layout.BorderPane;

import javafx.geometry.Insets ;

import javafx.animation.KeyFrame;

import javafx.util.Duration;

import javafx.application.Platform ;

public class TextCounter  extends Application {

static Text textCounter;

Timeline timeline;

@Override

public void start(Stage primaryStage) {

Button btn = new Button();

Button btn2 = new Button();

Button btn3 = new Button();

textCounter = new Text(  "");

Font font = Font.font("Courier New", FontWeight.BOLD, 36);

textCounter.setFont(font);

IntCounter in = new IntCounter();

timeline = new Timeline(new KeyFrame(Duration.millis(1), new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent event) {

             in.change(textCounter);

}

}));

timeline.setCycleCount(Timeline.INDEFINITE);

timeline.setAutoReverse(false);

btn.setText("Start");

btn2.setText("Pause");

btn3.setText("Resume");

   btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override

            public void handle(ActionEvent event) {

 Platform.runLater( () -> {

           timeline.play();

    }

    );

}

        });

btn2.setOnAction(new EventHandler<ActionEvent>() {

            @Override

            public void handle(ActionEvent event) {

                       Platform.runLater( () -> {

            timeline.pause();

     }

    );

            }

        });

btn3.setOnAction(new EventHandler<ActionEvent>() {

            @Override

            public void handle(ActionEvent event) {

 Platform.runLater( () -> {

           timeline.play();

     }

    );

}

});

    HBox box = new HBox();

    box.getChildren().addAll(textCounter);

    box.setSpacing(100);

    box.setPrefWidth(100);

    box.setAlignment(Pos.CENTER);

    HBox box1 = new HBox();

    box1.getChildren().addAll(btn,btn2,btn3);

    box1.setSpacing(13);

    box1.setPrefWidth(80);

     box1.setAlignment(Pos.BOTTOM_CENTER );

btn.setMinWidth(box1.getPrefWidth());

btn2.setMinWidth(box1.getPrefWidth());

btn3.setMinWidth(box1.getPrefWidth());

    BorderPane root = new BorderPane();

    root.setPadding(new Insets(20));

    root.setCenter(box);

    root.setBottom(box1);

    Scene scene = new Scene(root, 400, 350);

        primaryStage.setTitle("Text Counter ");

        primaryStage.setScene(scene);

        primaryStage.show();

    }

   public static void main(String[] args) {

       launch(args);

    }

}

class IntCounter {

private int counter = 0;

public int IncrementCount(){

return counter++ ;

}

public int getCount(){

return counter  ;

}

public void setCount(int c){

 counter = c;

}

int   millis = 0;

void change(Text text) {

if(millis == 1000) {

counter++;

millis = 0;

}

 millis++;

TextCounter.textCounter.setText( counter + ""  );

}

}// end class

📩 Need a similar solution? Email me: adel455@hotmail.com


Previous Post Next Post

Comments

Nepali Graphics - Learn design, Animation, and Progrmming