13
13
import javafx .scene .media .MediaPlayer ;
14
14
import javafx .scene .paint .Color ;
15
15
import javafx .scene .shape .Rectangle ;
16
-
16
+ import java . io . InputStream ;
17
17
import javafx .beans .value .ChangeListener ;
18
18
import javafx .beans .value .ObservableValue ;
19
19
import javafx .geometry .Pos ;
20
20
import javafx .scene .control .Label ;
21
21
import javafx .scene .control .Slider ;
22
22
import javafx .scene .layout .BorderPane ;
23
-
24
- import java .io .FileInputStream ;
23
+ import javafx .scene .layout .Region ;
25
24
import java .io .FileNotFoundException ;
25
+ import java .util .Objects ;
26
+ import javafx .scene .layout .StackPane ;
26
27
27
28
public class DinosaurMenu extends FXGLMenu {
28
29
private MediaPlayer mainMenuSound ;
@@ -42,30 +43,39 @@ public DinosaurMenu() {
42
43
var quitButton = new Button ("Quit" );
43
44
44
45
Slider volumeSlider = new Slider (0 , 1 , 1 );
45
- volumeSlider .setShowTickLabels (true );
46
- volumeSlider .setShowTickMarks (true );
47
46
volumeSlider .setBlockIncrement (0.01 );
48
47
49
- Label volumeLabel = new Label ( "Volume: 100%" );
48
+ volumeSlider . getStylesheets (). add ( Objects . requireNonNull ( getClass (). getResource ( "/styles/styles.css" )). toExternalForm () );
50
49
50
+ //Sets the volume label
51
+ Label volumeLabel = new Label ("100%" );
51
52
volumeSlider .valueProperty ().addListener (new ChangeListener <Number >() {
52
53
@ Override
53
54
public void changed (ObservableValue <? extends Number > observable , Number oldValue , Number newValue ) {
54
55
mainMenuSound .setVolume (newValue .doubleValue ());
55
- volumeLabel .setText (String .format ("Volume: %.0f%%" , newValue .doubleValue () * 100 ));
56
+ volumeLabel .setText (String .format ("%.0f%%" , newValue .doubleValue () * 100 ));
56
57
}
57
58
});
58
59
59
60
60
61
try {
61
62
62
-
63
- FileInputStream fileInputStream = new FileInputStream ("../dinosaur-exploder/src/main/resources/assets/textures/dinomenu.png" );
64
- FileInputStream mutemusic_button = new FileInputStream ("../dinosaur-exploder/src/main/resources/assets/textures/silent.png" );
65
- FileInputStream audioOnButton = new FileInputStream ("../dinosaur-exploder/src/main/resources/assets/textures/playing.png" );
63
+ //Using InputStream for efficient fetching of images
64
+ InputStream menuImage = getClass ().getClassLoader ().getResourceAsStream ("assets/textures/dinomenu.png" );
65
+ if (menuImage == null ) {
66
+ throw new FileNotFoundException ("Resource not found: assets/textures/dinomenu.png" );
67
+ }
68
+ InputStream muteButton = getClass ().getClassLoader ().getResourceAsStream ("assets/textures/silent.png" );
69
+ if (muteButton == null ) {
70
+ throw new FileNotFoundException ("Resource not found: assets/textures/silent.png" );
71
+ }
72
+ InputStream soundButton = getClass ().getClassLoader ().getResourceAsStream ("assets/textures/playing.png" );
73
+ if (soundButton == null ) {
74
+ throw new FileNotFoundException ("Resource not found: assets/textures/playing.png" );
75
+ }
66
76
67
77
// image for dino in main menu
68
- Image image = new Image (fileInputStream );
78
+ Image image = new Image (menuImage );
69
79
ImageView imageView = new ImageView (image );
70
80
imageView .setFitHeight (250 );
71
81
imageView .setFitWidth (200 );
@@ -74,22 +84,22 @@ public void changed(ObservableValue<? extends Number> observable, Number oldValu
74
84
imageView .setPreserveRatio (true );
75
85
76
86
//adding image to manually mute music
77
- Image mute = new Image (mutemusic_button );
87
+ Image mute = new Image (muteButton );
78
88
79
89
80
- Image audioOn = new Image (audioOnButton );
90
+ Image audioOn = new Image (soundButton );
81
91
ImageView imageViewPlaying = new ImageView (audioOn );
82
- imageViewPlaying .setFitHeight (40 );
83
- imageViewPlaying .setFitWidth (50 );
84
- imageViewPlaying .setX (490 );
92
+ imageViewPlaying .setFitHeight (50 );
93
+ imageViewPlaying .setFitWidth (60 );
94
+ imageViewPlaying .setX (470 );
85
95
imageViewPlaying .setY (20 );
86
96
imageViewPlaying .setPreserveRatio (true );
87
97
88
98
89
99
startButton .setMinSize (50 , 50 );
90
100
startButton .setPrefSize (140 ,60 );
91
101
92
- quitButton .setMinSize (140 , 50 );
102
+ quitButton .setMinSize (140 , 60 );
93
103
94
104
title .setTranslateY (100 );
95
105
title .setTranslateX (getAppWidth () / 2 - 145 );
@@ -106,24 +116,34 @@ public void changed(ObservableValue<? extends Number> observable, Number oldValu
106
116
root .setTop (title );
107
117
BorderPane .setAlignment (title , Pos .CENTER );
108
118
119
+
109
120
BorderPane volumePane = new BorderPane ();
110
121
volumePane .setLeft (volumeLabel );
122
+ BorderPane .setAlignment (volumeLabel , Pos .CENTER );
111
123
volumePane .setCenter (volumeSlider );
112
- volumePane .setStyle ("-fx-padding: 10;" );
124
+ volumeSlider .setStyle ("-fx-padding: 10px;" );
125
+ volumeSlider .setTranslateY (25 );
126
+ volumeSlider .setTranslateX (10 );
127
+ volumeLabel .setTranslateX (20 );
128
+ volumeLabel .setTranslateY (20 );
129
+ volumeLabel .setStyle ("-fx-text-fill: #61C181;" );
130
+
131
+
113
132
114
133
root .setCenter (volumePane );
115
134
root .setBottom (new BorderPane (startButton , null , quitButton , null , null ));
116
135
BorderPane .setAlignment (startButton , Pos .CENTER );
117
136
BorderPane .setAlignment (quitButton , Pos .BOTTOM_CENTER );
118
137
138
+
119
139
startButton .setOnAction (event -> {
120
140
fireNewGame ();
121
141
mainMenuSound .stop ();
122
142
});
123
143
124
144
imageViewPlaying .setOnMouseClicked (mouseEvent -> {
125
145
if (mainMenuSound .isMute ()){
126
- mainMenuSound .setMute (false );
146
+ mainMenuSound .setMute (false ); //False later
127
147
imageViewPlaying .setImage (audioOn );
128
148
} else {
129
149
mainMenuSound .setMute (true );
@@ -133,8 +153,9 @@ public void changed(ObservableValue<? extends Number> observable, Number oldValu
133
153
134
154
quitButton .setOnAction (event -> fireExit ());
135
155
156
+
136
157
getContentRoot ().getChildren ().addAll (
137
- bg , title , startButton , quitButton , imageView , imageViewPlaying , volumeSlider , volumeLabel
158
+ bg , title , startButton , quitButton , imageView , imageViewPlaying , volumeLabel , volumeSlider
138
159
);
139
160
}
140
161
catch (FileNotFoundException e ){
0 commit comments