Question: * * * JavaFX * * * * I can't get my button to 'roll' the dice images, and I had the images displayed but

***JavaFX****I can't get my button to 'roll' the dice images, and I had the images displayed but now it keeps giving me errors. Help???
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class RollingDice extends Application {
@Override
public void start(Stage primaryStage) throws FileNotFoundException{
DicePane dice = new DicePane();
VBox pane = new VBox();
pane.setSpacing(50);
pane.setAlignment(Pos.CENTER);
Button btRoll = new Button("Roll");
pane.getChildren().add(btRoll);
btRoll.setOnAction(new DiceRoll());
BorderPane borderPane = new BorderPane();
borderPane.setCenter(dice);
borderPane.setBottom(pane);
Scene scene = new Scene(borderPane,500,300);
primaryStage.setTitle("Roll Dice");
primaryStage.setScene(scene);
primaryStage.show();
}
class DiceRoll implements EventHandler{
@Override
public void handle (ActionEvent e){
DicePane.roll();
}
public static void main(String[] args){
launch(args);
}
}
class DicePane extends StackPane{
private static ImageView imageView;
public DicePane() throws FileNotFoundException{
getChildren().add(imageView);
setAlignment(Pos.CENTER);
roll();
}
public static void roll() throws FileNotFoundException{
int randomDie =((int)Math.random()*5)+1;
switch(randomDie){
case 1: InputStream oneGraphic = new FileInputStream("src/dice/one.png");
Image oneSide = new Image(oneGraphic);
imageView = new ImageView(oneSide);
break;
case 2: InputStream twoGraphic = new FileInputStream("src/dice/two.png");
Image twoSide = new Image(twoGraphic);
imageView = new ImageView(twoSide);
break;
case 3: InputStream threeGraphic = new FileInputStream("src/dice/three.png");
Image threeSide = new Image(threeGraphic);
imageView = new ImageView(threeSide);
break;
case 4: InputStream fourGraphic = new FileInputStream("src/dice/for.png");
Image fourSide = new Image(fourGraphic);
imageView = new ImageView(fourSide);
break;
case 5: InputStream fiveGraphic = new FileInputStream("src/dice/five.png");
Image fiveSide = new Image(fiveGraphic);
imageView = new ImageView(fiveSide);
break;
case 6: InputStream sixGraphic = new FileInputStream("src/dice/six.png");
Image sixSide = new Image(sixGraphic);
imageView = new ImageView(sixSide);
break;
}
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!