r/JavaFX • u/jxkcjxff • May 16 '21
I made this! Made a program using JavaFX to roll some dice! I'm at a really beginner level with JavaFX so I was pretty proud of this :)
2
u/jxkcjxff May 16 '21
How I got this to work was with an array of images and a method to show a random image from the array. Since array's count from 0 to detect when the numbers are equal to 7 I made it so that the if statements add one to their current array number.
It was really fun to make and I am planning on making a bunch of stuff using scene builder once I've learned the basics a bit more B).
2
u/jxkcjxff May 16 '21 edited May 16 '21
package sample;I like to keep my main methods as minimal as possible :)
package sample;
import javafx.scene.Scene;import javafx.scene.paint.Color; import javafx.stage.Stage; import java.io.FileNotFoundException;
public class stageWindow {
Bits b = new Bits();
public stageWindow(int x, int y) throws FileNotFoundException {
Stage stage = new Stage();
Scene scene = new Scene(b.root, x, y);
scene.setFill(Color.GREEN);
new Bits();
b.root.getChildren().add(b.butRoll);
b.root.getChildren().add(b.winlab);
stage.setScene(scene);
stage.setTitle("Dice Roll");
stage.show();
stage.setResizable(false);
}}
This is where I set up the stage as well as add some of the small things needed in my program.
package sample;import javafx.scene.Group; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.paint.Color; import javafx.scene.text.Font;
import java.io.FileInputStream; import java.io.FileNotFoundException;
public class Bits {
FileInputStream[] DiceImg = new FileInputStream[6];
Image[] Dice = new Image[6];
public void diceIn() throws FileNotFoundException {
DiceImg[0] = new FileInputStream("A:\\DiceRoll\\src\\sample\\Dice\\DiceOne.png");
DiceImg[1] = new FileInputStream("A:\\DiceRoll\\src\\sample\\Dice\\DiceTwo.png");
DiceImg[2] = new FileInputStream("A:\\DiceRoll\\src\\sample\\Dice\\DiceThree.png");
DiceImg[3] = new FileInputStream("A:\\DiceRoll\\src\\sample\\Dice\\DiceFour.png");
DiceImg[4] = new FileInputStream("A:\\DiceRoll\\src\\sample\\Dice\\DiceFive.png");
DiceImg[5] = new FileInputStream("A:\\DiceRoll\\src\\sample\\Dice\\DiceSix.png");
for(int i = 0; i < Dice.length; i++){
Dice[i] = new Image(DiceImg[i]);
}
}
ImageView img = new ImageView();
ImageView img2 = new ImageView();
public void Dice() {
img.setImage(null);
img.setX(80);
img.setY(20);
img.setPreserveRatio(true);
img2.setImage(null);
img2.setX(110);
img2.setY(20);
img2.setPreserveRatio(true);
}
public Group root = new Group(img, img2);
Button butRoll = new Button();
Label winlab = new Label();
Bits() throws FileNotFoundException {
Dice();
diceIn();
rollingButt();
diceResult();
}
public void rollingButt(){
butRoll.setText("Roll the Dice?");
butRoll.setOnAction(actionEvent -> {
int i = randomNumber();
int i2 = randomNumber();
img.setImage(Dice[i]);
img2.setImage(Dice[i2]);
System.out.println(i + 1);
System.out.println(i2 + 1);
System.out.println((i + 1) + (i2 + 1));
if((i + 1) + (i2 + 1) == 7){
winlab.setText("You Win!");
}
else{
winlab.setText("");
}
});
butRoll.setLayoutX(60);
butRoll.setLayoutY(100);
butRoll.setTextFill(Color.YELLOW);
butRoll.setStyle("-fx-background-color: #ff0000");
}
public void diceResult(){
winlab.setLayoutX(60);
winlab.setLayoutY(150);
winlab.setTextFill(Color.RED);
winlab.setFont(Font.font("Arial", 20));
}
public int randomNumber(){
return (int) (Math.random() * 6);
}}
Lastly, the "Bits" class that adds all the bits and bobs as well as all the logic and stuff.
Feel free to give me any advice for this program, I am happy to hear any ideas and such on what you thought of it and feel free to mess with the code if you like x.
1
u/jxkcjxff May 16 '21
I'm sorry for the mess of a code block. Reddit messed it all up. I tried fixing it but it just didn't work.
6
u/johnmc325 May 16 '21
Congratulations on completing your project. I'm not sure you provided all the code but I notice you had a problem formatting. You might like to consider using pastebin for sharing code, especially if it's on the large side.
You might want to look into standard Java coding practices around naming conventions. You use capitals when you should use lower case letters and lower case when captials should be used. This is not intended to be a critique of your coding style but if you are looking for feedback of that sort then take a look at Code Review.
I note you like to keep your main method lean, you might also want to look at how you can break your code into separate classes. The Bits class is a bit of a monster, dealing with UI and logic. When you look at SceneBuilder you might pick up some ideas on how to separate the two.
Finally, in the spirit of trying to help you build on what is a great achievement you might like to take a look at my youtube site. I only mention this as it has some step-by-step examples of JavaFX projects using SceneBuilder that may help you take your next step.
Good luck on your journey.