Issue
In my current code every time I add something like Platform.runLater it will crash due to the fact that you cannot have a Thread.sleep in the main scene. This is the last instance of my working code, any help is appreciated.
import javafx.application.Platform;
import javafx.animation.KeyFrame;
import javafx.animation.PauseTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.Group;
import javafx.scene.Scene;
import java.util.Random;
public class Testing2 extends Application {
private static final double MAX_X = 1000;
private static final double MAX_Y = 750;
private static String Converted = null;
private int MIN_X2 = 20;
private int MIN_Y2 = 20;
private int MAX_X2 = 950;
private int MAX_Y2 = 700;
private int clickCount = 0;
private Random random;
Random generator = new Random();
public int Seconds = 15;
Scene scene, scene2;
Stage window;
private final Text text2 = new Text("All Done!");
public void changeScene(Stage stage) {
StackPane root2 = new StackPane();
root2.getChildren().add(text2);
Scene scene2 = new Scene(root2, 200, 200);
stage.setScene(scene2);
}
public void init() {
random = new Random();
}
@Override
public void start(Stage primaryStage) throws Exception{
window = primaryStage;
Circle circle = new Circle(MAX_X / 2, MAX_Y / 2, 15);//(StartX, StartY, Size)
Text clickCountText = new Text("Clicks: " + clickCount);
Text Space = new Text(" ");
Text label = new Text("");
circle.setOnMouseClicked((event) -> {
circle.setCenterX(random.nextInt(MAX_X2 - MIN_X2) + MIN_X2);
circle.setCenterY(random.nextInt(MAX_Y2 - MIN_Y2) + MIN_Y2);
circle.getCenterX();
clickCount ++;
clickCountText.setText("Clicks: "+ clickCount);
});
Group layout = new Group(
circle,
new FlowPane(clickCountText, Space, label)
);
//create 2 strings, convert the number to a string, concat 3 strings and assign to variable, put variable in setText
PauseTransition pause = new PauseTransition(Duration.seconds(1));
pause.setOnFinished(event -> {
String String1 = "There are ";
String String2 = " Seconds Left";
for(int n=15; n>=0; n--){
Converted = Integer.toString(Seconds);
String String3 = String1 + Seconds + String2;
}}
);
final IntegerProperty i = new SimpleIntegerProperty(16);
Timeline timeline = new Timeline(
new KeyFrame(
Duration.seconds(1),
event -> {
i.set(i.get() - 1);
label.setText("You have: " + i.get() + " seconds remaining!");
}
)
);
timeline.setCycleCount(16);
timeline.play();
pause.play();
primaryStage.setScene(new Scene(layout, MAX_X, MAX_Y, Color.WHITE));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The program is supposed to generate a circle, and when it gets clicked it moves the circle to a random location on screen. Every time one is clicked it increases the click count by one. Once the timer hits 0 I want the scene to change to a screen that says the text, "You got __ clicks in 15 seconds! Good job!". I'm running this through eclipse so you can just paste it in and try it out.
Solution
An easy way to do it without threading is to create another timeline just the way you did the timeline
, where you can setScene
for your primaryStage
, like that:
Timeline finishTimeline = new Timeline(
new KeyFrame(
Duration.millis(1),
event -> {
StackPane s = new StackPane();
s.getChildren().add(new Label("your label here"));
Scene scene = new Scene(s, 200, 200);
primaryStage.setScene(scene);
}
)
);
note that you should play this finishTimeline when you finish the first timeline, so you can use a SequentialTransition
to play both in a sequential order:
SequentialTransition st = new SequentialTransition(timeline, finishTimeline);
st.play();
Now modify your code to be 100% precise.
Answered By - Abdallah Abdel Aziz
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)