I am looking for some advice or correction, since I am trying to create a program (which is a practical exercise) that consists of simulating a horse race with bets.
This race is made up of 3 horses (images), each of them has to move at the same time (on the x axis) and the horse that reaches the goal first (a point on the x axis) win Horses move at different speeds, each race randomly.
After creating the threads and executing the program, I do not see any definite error, visually my horses start to move but there comes a point where of my 3 horses, two stop and the rest keeps moving forward.
However, although my images visually do not move, the threads are still in process, because after finishing the loop that forces the images to move it shows the order of arrival of the horses (which is the last order of my threads).
I can not find any alternative or hint as to what could be happening.
This is my code:
MAIN CLASS
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("Hipodromo.fxml"));
AnchorPane root = (AnchorPane)loader.load();
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
HipodromoController controller = loader.getController();
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
HIPODROMOCONTROLLER CLASS
public class HipodromoController implements Initializable{
@FXML
private ImageView CaballoImagen1;
@FXML
private ImageView CaballoImagen2;
@FXML
private ImageView CaballoImagen3;
@FXML
private TextArea TextArea;
@FXML
private TextField txtApuesta;
@FXML
private Label lblRdoApuesta;
@FXML
private Label lblTotal;
@FXML
void empezarApuestas() {
TextArea.clear();
Hilo1 h1 = new Hilo1(CaballoImagen1, TextArea);
Hilo2 h2 = new Hilo2(CaballoImagen2, TextArea);
Hilo3 h3 = new Hilo3(CaballoImagen3, TextArea);
h1.start();
h2.start();
h3.start();
this.CaballoImagen1.setLayoutX(27);
this.CaballoImagen2.setLayoutX(27);
this.CaballoImagen3.setLayoutX(27);
}
@Override
public void initialize(java.net.URL arg0, ResourceBundle arg1) {
File file = new File("src/caballo.png");
Image image = new Image(file.toURI().toString());
CaballoImagen1.setImage(image);
CaballoImagen2.setImage(image);
CaballoImagen3.setImage(image);
}
THREAD CLASS
public class Hilo1 extends Thread{
@FXML
private ImageView CaballoImagen1;
@FXML
private TextArea TextArea;
public Hilo1(ImageView c1, TextArea tx){
this.CaballoImagen1 = c1;
this.TextArea = tx;
}
@Override
public void run(){
int comienzo = 27;
do{
int valorEntero = (int) Math.floor(Math.random()*(1-30+1)+30);
comienzo = comienzo + valorEntero;
this.CaballoImagen1.setX(comienzo);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}while(comienzo < 655);
this.TextArea.appendText("Caballo 1\n");
}
The code of the threads is the same, only differs in the image which is moved by each thread.
I appreciate the time of anyone who takes the trouble to read this whole Bible thank you very much.
Here is an image of the final result of my application