Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created December 4, 2021 15:21
Show Gist options
  • Save Da9el00/c294230daca21317a96084269433bc9a to your computer and use it in GitHub Desktop.
Save Da9el00/c294230daca21317a96084269433bc9a to your computer and use it in GitHub Desktop.
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.io.File;
import java.util.Random;
public class Controller {
Random random = new Random();
@FXML
private ImageView diceImage;
@FXML
private Button rollButton;
@FXML
void roll(ActionEvent event) {
rollButton.setDisable(true);
Thread thread = new Thread(){
public void run(){
System.out.println("Thread Running");
try {
for (int i = 0; i < 15; i++) {
File file = new File("src/sample/dice/dice" + (random.nextInt(6)+1)+".png");
diceImage.setImage(new Image(file.toURI().toString()));
Thread.sleep(50);
}
rollButton.setDisable(false);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
}
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<ImageView fx:id="diceImage" fitHeight="200.0" fitWidth="200.0" layoutX="92.0" layoutY="100.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@dice/dice1.png" />
</image></ImageView>
<Button fx:id="rollButton" layoutX="379.0" layoutY="163.0" mnemonicParsing="false" onAction="#roll" text="Roll">
<font>
<Font size="35.0" />
</font></Button>
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment