74675

(Java) how to update dice image when button clicked and equal to number given?

Question:

I'm trying to make a simple dice game. Currently I have the dice fully working. The last thing I want to do is using an image as dice.

Here is my code for the dice ( at // I want it to add the image for dice one called Alea_1.png

public class dice { public static int rollDice(int number, int nSides) { int num = 0; int roll = 0; Random r = new Random(); if (nSides >= 3) { for (int i = 0; i < number; i++) { roll = r.nextInt(nSides) + 1; System.out.println("Roll is: " + roll); num = num + roll; if (roll == 1) { //insert new image Alea_1.png }

My main file is berekenen.java. Currently I can display the dice on the primary stage but because it loads in at start I can't change it.

Image of my application: <a href="https://gyazo.com/14708712af9a3858c5deed4a1bc508c6" rel="nofollow">https://gyazo.com/14708712af9a3858c5deed4a1bc508c6</a>

This is the code if you need it:

public class berekenen extends Application implements EventHandler <ActionEvent> { public static int ballance = 5000; HBox hb = new HBox(); Button bopnieuw = new Button("opnieuw"); TextField tf1 = new TextField(); TextField tf2 = new TextField(String.valueOf("Ballance:" + ballance)); TextField tf3 = new TextField(); public static void main(String[] args) { launch(args); } public dice dice123 = new dice(); public void handle(ActionEvent event) { int gekozen = Integer.parseInt(tf3.getText()); int result = dice123.rollDice(1,6); if (event.getSource() == bopnieuw) { tf1.setText(result + ""); if (dice.check(gekozen,result)) { ballance = ballance + 100; System.out.println(ballance); tf2.setText(toString().valueOf("Ballance:" + ballance)); }else{ ballance = ballance - 20; System.out.println(ballance); tf2.setText(toString().valueOf("Ballance:" + ballance)); } } } public void start(Stage primaryStage) throws Exception { primaryStage.setTitle("Mijn eerste javaFX application"); primaryStage.setMaxHeight(8000); primaryStage.setMaxWidth(8000); primaryStage.getIcons().add(new Image("")); primaryStage.show(); StackPane pane = new StackPane(); Image image = new Image("Alea_1.png"); ImageView iv1 = new ImageView(); iv1.setImage(image); bopnieuw.setOnAction(this); hb.getChildren().addAll(tf1, tf2, tf3); hb.getChildren().addAll(bopnieuw); pane.getChildren().addAll(hb, iv1); Scene scene = new Scene(pane, 1000, 400); primaryStage.setScene(scene); primaryStage.show(); }

}

Answer1:

Here is some code that I used to practice different ideas. This code loads images in a way so that they can be used with multiple dice. It uses <a href="https://stackoverflow.com/questions/9966136/javafx-periodic-background-task" rel="nofollow">JavaFX periodic background task </a> (probably my favorite and most used answer on this site) to flip through the images when rolling the die.

<blockquote>

DieImages Class: Used to load Images

</blockquote> import javafx.scene.image.Image; /** * * @author blj0011 */ public class DieImages { final Image die1 = new Image(getClass().getResourceAsStream("dieRed1.png")); final Image die2 = new Image(getClass().getResourceAsStream("dieRed2.png")); final Image die3 = new Image(getClass().getResourceAsStream("dieRed3.png")); final Image die4 = new Image(getClass().getResourceAsStream("dieRed4.png")); final Image die5 = new Image(getClass().getResourceAsStream("dieRed5.png")); final Image die6 = new Image(getClass().getResourceAsStream("dieRed6.png")); final Image[] images = new Image[6]; public DieImages() { images[0] = die1; images[1] = die2; images[2] = die3; images[3] = die4; images[4] = die5; images[5] = die6; } public Image[] getImages() { return images; } } <blockquote>

Die Class:

</blockquote> import javafx.scene.image.Image; import javafx.scene.image.ImageView; /** * * @author blj0011 */ public class Die { ImageView dieFace; Image[] images; public Die(Image[] images) { this.images = images; dieFace = new ImageView(this.images[0]);//set default to image 0 } public Die(Image[] images, int dieFaceValue) { //Need to catch for values less than 1 and greater than 6! this.images = images; dieFace = new ImageView(this.images[dieFaceValue - 1]); } public ImageView getdieFace() { return dieFace; } public void setDieFace(int dieFaceValue) { //Need to catch for values less than 1 and greater than 6! dieFace.setImage(this.images[dieFaceValue - 1]); } } <blockquote>

Main

</blockquote> import java.util.Random; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javafx.util.Duration; /** * * @author blj0011 */ public class JavaFXApplication173 extends Application { @Override public void start(Stage primaryStage) { DieImages images = new DieImages(); Die die = new Die(images.getImages()); StackPane stackPane = new StackPane(die.getdieFace());//Add ImageView(Die) to stackPane VBox.setVgrow(stackPane, Priority.ALWAYS); Button btn = new Button(); btn.setText("Roll Die"); btn.setOnAction((ActionEvent event) -> { btn.setDisable(true);//Disable Button Random random = new Random(); Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(.3), (actionEvent) -> { int tempRandom = random.nextInt(6) + 1; System.out.println(tempRandom); die.setDieFace(tempRandom); })); timeline.setCycleCount(random.nextInt(20) + 1); timeline.play(); timeline.setOnFinished(actionEvent -> { btn.setDisable(false);//Enable Button }); }); VBox root = new VBox(stackPane, new StackPane(btn)); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }

In your case, you can probably do without the Timeline.

Recommend

  • How to deactivate a TextField and a Label by Combobox selection in javafx
  • java beginner if/else if problem
  • Is it possible to find the address of a non-primitive variable in Java?
  • nested c:foreach in jstl
  • Configurable Windows Service - How and where to store configuration
  • How to test method Jsoup.connect (static) using PowerMock?
  • How to stop auto-refreshing of SBT modules upon opening project in IDEA?
  • How to reuse eclipse launch file in a new project
  • How to make label backgrounds transparent in SWT
  • How to detect if user Did not allow location update
  • How can a console application sudo itself under OS X if it needs root privileges? [closed]
  • Launch.json in Visual Studio Code
  • Issue with static code in Android
  • Can I use objects created in C# within metro ui javascript code?
  • CardLayout display Next panel - java Swing
  • Django: ImportError: No module named quiz.views
  • Force iPad app to launch in portrait when landscape is supported
  • Launching a new command window from Golang in Windows
  • Android - Call default browser with and redirect to a designated url
  • Persistent stty Settings?
  • Python adding lots of things to PATH. How do I stop?
  • Display java JPanel in a JFrame
  • How to Cache Real-time Data?
  • d3 v4 drag and drop with TypeScript
  • JQuery Internet Explorer and ajaxstop
  • Atlas images wrong size on iPad iOS 9
  • Spring Data JPA custom method causing PropertyReferenceException
  • Azure Cloud Service Web Role web pages do not load
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Possible to stop flickering java tooltip in heavyweight mode?
  • what is the difference between the asp.net mvc application and asp.net web application
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Linking SubReports Without LinkChild/LinkMaster
  • apache spark aggregate function using min value
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?