-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayRandomDice.java
More file actions
114 lines (98 loc) · 3.53 KB
/
Copy pathDisplayRandomDice.java
File metadata and controls
114 lines (98 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.io.FileInputStream;
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.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class DisplayRandomDice extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage pStage)
{
try
{
String[] dieImages = {"DieFace1.gif","DieFace2.gif","DieFace3.gif","DieFace4.gif","DieFace5.gif","DieFace6.gif"};
//int d1Val , d2Val, playerTotal = 0;
VBox root = new VBox();
root.setSpacing(40);
HBox heading = new HBox();
HBox dieFaces = new HBox();
HBox buttons = new HBox();
HBox footer = new HBox();
Scene scene = new Scene(root, 600, 400);
Text headingText = new Text();
headingText.setText("Display Random Dice: Lab 8");
headingText.setFont(Font.font(STYLESHEET_CASPIAN, FontWeight.BLACK, FontPosture.ITALIC, 40));
heading.getChildren().add(headingText);
heading.setAlignment(Pos.CENTER);
int random = (int)(Math.random()*6);
Image[] dieFaceImages = new Image[6];
for(int x = 0; x < 6; x++)
{
FileInputStream inputFile = new FileInputStream(dieImages[x]);
dieFaceImages[x] = new Image(inputFile);
}
random = (int)(Math.random() * 6);
ImageView imageViewDie1 = new ImageView(dieFaceImages[random]);
random = (int)(Math.random() * 6);
ImageView imageViewDie2 = new ImageView(dieFaceImages[random]);
random = (int)(Math.random() * 6);
ImageView imageViewDie3 = new ImageView(dieFaceImages[random]);
dieFaces.getChildren().add(imageViewDie1);
dieFaces.getChildren().add(imageViewDie2);
dieFaces.getChildren().add(imageViewDie3);
dieFaces.setAlignment(Pos.CENTER);
Button rollButton = new Button("Roll");
EventHandler<ActionEvent> rollButtonPushed = new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent e)
{
int newRandom = 0;
newRandom = (int)(Math.random()* 6);
imageViewDie1.setImage(dieFaceImages[newRandom]);
newRandom = (int)(Math.random()* 6);
imageViewDie2.setImage(dieFaceImages[newRandom]);
newRandom = (int)(Math.random()* 6);
imageViewDie3.setImage(dieFaceImages[newRandom]);
}
};
rollButton.setOnAction(rollButtonPushed);
buttons.getChildren().add(rollButton);
buttons.setAlignment(Pos.CENTER);
Text footerText = new Text();
footerText.setText("Roll your life away, it's 2020, and we have a vaccine on the way...Yay or Nay?");
footerText.setFont(Font.font(STYLESHEET_CASPIAN, FontWeight.NORMAL, FontPosture.ITALIC, 15));
footer.getChildren().add(footerText);
footer.setAlignment(Pos.CENTER);
root.setBackground(new Background(new BackgroundFill(Color.GREEN,null, null)));
root.getChildren().add(heading);
root.getChildren().add(dieFaces);
root.getChildren().add(buttons);
root.getChildren().add(footer);
pStage.setScene(scene);
pStage.show();
}
catch(Exception e)
{
System.out.println("Error: Image files can't be found");
}
}
}