Guest User

Main.java

a guest
Nov 24th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. package application;
  2.  
  3. import controller.controller;
  4. import java.awt.event.KeyEvent;
  5. import java.util.ArrayList;
  6.  
  7. import javafx.animation.*;
  8. import javafx.application.Application;
  9. import javafx.event.ActionEvent;
  10. import javafx.event.EventHandler;
  11. import javafx.scene.*;
  12. import javafx.scene.canvas.Canvas;
  13. import javafx.scene.canvas.GraphicsContext;
  14. import javafx.scene.control.Button;
  15. import javafx.scene.image.Image;
  16. import javafx.scene.input.KeyCode;
  17. import javafx.scene.layout.BorderPane;
  18. import javafx.scene.layout.HBox;
  19. import javafx.scene.paint.Color;
  20. import javafx.stage.*;
  21. import javafx.fxml.FXMLLoader;
  22. import javafx.scene.shape.*;
  23. import javafx.util.Duration;
  24.  
  25. /**
  26. * Main function extends Application from Application Library from JavaFX
  27. */
  28. public class Main extends Application {
  29. public static Stage stage;
  30. controller c = new controller();
  31. int x = 100, y = 100, aX = 400, aY = 400;
  32. int xSize = 600, ySize = 600, canvasX = 400, canvasY = 400;
  33. Canvas canvas = new Canvas(canvasX, canvasY); // Creates canvas which will contain things
  34. int time = 900;
  35. private boolean animationOn = false;
  36. Rectangle arena = c.createArena(x, y, aX, aY);
  37. Circle cir = c.createCircle(x+150, y+150);
  38. Circle cir2 = c.createCircle(x+80, y+150);
  39. private Timeline animation;
  40. private HBox setButtons() {
  41. // create button
  42. Button btnAnimOn = new Button("Start Animation");
  43. // now add handler
  44. btnAnimOn.setOnAction(new EventHandler<ActionEvent>() {
  45. @Override
  46. public void handle(ActionEvent event) {
  47. animationOn = true;
  48. c.translate(cir, time);
  49. }
  50. });
  51.  
  52. Button btnAnimOff = new Button("Stop Animation");
  53.  
  54. btnAnimOff.setOnAction(new EventHandler<ActionEvent>() {
  55. @Override
  56. public void handle(ActionEvent event) {
  57. animationOn = false;
  58. if (animationOn == false) {
  59. c.translate(cir, time);
  60. }
  61. }
  62. });
  63.  
  64. return new HBox(btnAnimOn, btnAnimOff);
  65. }
  66.  
  67. @Override
  68. public void start(Stage primaryStage) {
  69. stage = primaryStage;
  70.  
  71. try {
  72.  
  73. // Load the layout from the FXML and add it to the scene
  74. BorderPane bp = new BorderPane();
  75. bp.setBottom(setButtons());
  76.  
  77. GraphicsContext gc = canvas.getGraphicsContext2D();
  78.  
  79.  
  80. bp.getChildren().addAll(cir, cir2, arena); // Adds all shapes and features.
  81. Scene scene = new Scene(bp, xSize, ySize);
  82.  
  83. // Set the scene to stage and show the stage to the user
  84. primaryStage.setScene(scene);
  85. primaryStage.show();
  86.  
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. }
  90.  
  91.  
  92. }
  93.  
  94. public static void main(String[] args) {
  95. launch(args);
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment