I have created a class called threads with a run() method that is supposed to print a rectangle. I cannot add this thread to the Pane because .add() does not accept threads. How can I successfully upload this thread onto my JavaFX screen? (The idea behind this is that each rectangle generated will be like a new monster, in which these monsters will attack an obstacle, lowering the health of it).
public class Threads implements Runnable {
@Override
public void run(){
Rectangle rect = new Rectangle((int) (Math.random() * 1000), (int) (Math.random() * 1000),100,100);
rect.setFill(Color.color(Math.random(), Math.random(), Math.random()));
}
}
public class SquareThreads extends Application {
@Override
public void start(Stage primaryStage) {
Pane root = new Pane();
Thread t1 = new Thread(new Threads ());
t1.start();
root.getChildren().add(t1);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}