Advertisement
Guest User

Psychobagger Question New Scene

a guest
Mar 16th, 2017
928
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. /* A simple game, the mechanics not yet implemented.
  2. *
  3. * This is simply working on the title screen.
  4. */
  5.  
  6. import java.io.File;
  7. import javafx.application.Application;
  8. import javafx.application.Platform;
  9. import javafx.geometry.Insets;
  10. import javafx.geometry.Pos;
  11. import javafx.scene.Scene;
  12. import javafx.scene.control.Alert;
  13. import javafx.scene.control.Alert.AlertType;
  14. import javafx.scene.control.Button;
  15. import javafx.scene.control.ToggleButton;
  16. import javafx.scene.image.Image;
  17. import javafx.scene.image.ImageView;
  18. import javafx.scene.layout.Background;
  19. import javafx.scene.layout.BackgroundImage;
  20. import javafx.scene.layout.BackgroundSize;
  21. import javafx.scene.layout.VBox;
  22. import javafx.scene.media.Media;
  23. import javafx.scene.media.MediaPlayer;
  24. //import javafx.scene.paint.Color;
  25. import javafx.stage.Stage;
  26.  
  27. public class MenuFX extends Application {
  28. @Override
  29.  
  30. public void start (Stage primaryStage) {
  31.  
  32. // Make the window a set size...
  33. primaryStage.setResizable(false);
  34.  
  35. // Create media player
  36. // Rather than inputting the entire absolute URI, which would confine the program
  37. // to the creator's device, we create a new file, grab the URI on whatever machine
  38. // the program is running on and convert it to a string...
  39. // PORTABILITY!
  40. Media menuMusic = new Media(new File("music/menu.mp3").toURI().toString());
  41. MediaPlayer menuPlayer = new MediaPlayer(menuMusic);
  42.  
  43. // Want to see the absolute URI? Uncomment the next line
  44. //System.out.println(new File("music/menu.mp3").toURI().toString());
  45.  
  46. // Adjust the cycles and volume then start playing menu music
  47. // Lazy, but it will suffice
  48. menuPlayer.setCycleCount(999999999);
  49. menuPlayer.setVolume(0.2);
  50. menuPlayer.setAutoPlay(true);
  51.  
  52. // Create menu vbox and set the background image
  53. VBox menuVBox = new VBox(30);
  54. menuVBox.setBackground(new Background(new BackgroundImage(new Image("image/bambooBG.jpg"), null, null, null, new BackgroundSize(45, 45, true, true, true, true))));
  55.  
  56. // Create image object from logo image
  57. ImageView logoImage = new ImageView("image/tiles_logo.png");
  58.  
  59. // Create new insets and set them to the image so it's farther away from the buttons
  60. Insets insetImage = new Insets(0, 0, 200, 0);
  61. VBox.setMargin(logoImage, insetImage);
  62.  
  63. // Create start button
  64. Button startButton = new Button("Start Game");
  65.  
  66. // TODO Call other classes and game stuff
  67.  
  68. // Create help button
  69. Button helpButton = new Button("Help");
  70.  
  71. // Create alert popup and bind it to the help button if player needs help- using a lambda handler
  72. Alert helpAlert = new Alert(AlertType.INFORMATION, "This game, tiles, will test your visual and fine-muscle acuity. When you begin a round, a 5x5 grid of tiles,"
  73. + " randomly placed, will appear. A timer will always be displayed at the top of the gamescreen. During this short, intense time, you must click all of"
  74. + " the white tiles, while avoiding the black tiles. Once a round is complete, a new round begins with a new grid of tiles. If you manage to complete "
  75. + "seven rounds, the game will shift to a 6x6 grid of tiles, with the same amount of time given. You must go until you fail. \n\nWhite tiles -"
  76. + " Click all of these! \nBlack tiles - Do not click these! \nGreen tiles - These add to your timer. \nRed tiles - These subtract from your timer.");
  77. helpButton.setOnAction(e -> helpAlert.showAndWait());
  78.  
  79. // Create music toggle button
  80. ToggleButton musicButton = new ToggleButton("Music On/Off");
  81.  
  82. // Toggle functionality... pause/play
  83. // Use lambda handler block
  84. musicButton.setOnAction(e -> {
  85.  
  86. if (musicButton.isSelected()) {
  87.  
  88. menuPlayer.pause();
  89. } else {
  90.  
  91. menuPlayer.play();
  92. }
  93. });
  94.  
  95. // Create credits button
  96. Button creditsButton = new Button("Credits");
  97.  
  98. // Create alert popup and bind it to the credits button so players can see credits
  99. Alert creditsAlert = new Alert(AlertType.INFORMATION, "-CREDITS-");
  100. creditsButton.setOnAction(e -> creditsAlert.showAndWait());
  101.  
  102. // Create exit button and set it to close the program when clicked, using a lambda handler
  103. Button endButton = new Button("Exit Game");
  104. endButton.setOnAction(e -> Platform.exit());
  105.  
  106. // Add all nodes to the vbox pane and center it all
  107. // Must be in order from top to bottom
  108. menuVBox.getChildren().addAll(logoImage, startButton, helpButton, musicButton, creditsButton, endButton);
  109. menuVBox.setAlignment(Pos.CENTER);
  110.  
  111. // New scene, place pane in it
  112. Scene scene = new Scene(menuVBox, 630, 730);
  113.  
  114. // Place scene in stage
  115. primaryStage.setTitle("-tiles-");
  116. primaryStage.setScene(scene);
  117. primaryStage.show();
  118. }
  119.  
  120. // Needed to run JavaFX w/o the use of the command line
  121. public static void main(String[] args) {
  122.  
  123. launch(args);
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement