Monty374

JFX 7.7

Dec 7th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. package bounceballcontrol;
  2.  
  3. import javafx.application.Application;
  4. import javafx.stage.Stage;
  5. import javafx.scene.Scene;
  6. import javafx.scene.input.KeyCode;
  7.  
  8. public class BounceBallControl extends Application {
  9. @Override // Override the start method in the Application class
  10. public void start(Stage primaryStage) {
  11. BallPane ballPane = new BallPane(); // Create a ball pane
  12.  
  13. // Pause and resume animation
  14. ballPane.setOnMousePressed(e -> ballPane.pause());
  15. ballPane.setOnMouseReleased(e -> ballPane.play());
  16.  
  17. // Increase and decrease animation
  18. ballPane.setOnKeyPressed(e -> {
  19. if (e.getCode() == KeyCode.UP) {
  20. ballPane.increaseSpeed();
  21. }
  22. else if (e.getCode() == KeyCode.DOWN) {
  23. ballPane.decreaseSpeed();
  24. }
  25. });
  26.  
  27. // Create a scene and place it in the stage
  28. Scene scene = new Scene(ballPane, 250, 150);
  29. primaryStage.setTitle("BounceBallControl"); // Set the stage title
  30. primaryStage.setScene(scene); // Place the scene in the stage
  31. primaryStage.show(); // Display the stage
  32.  
  33. // Must request focus after the primary stage is displayed
  34. ballPane.requestFocus();
  35. }
  36.  
  37. /**
  38. * The main method is only needed for the IDE with limited
  39. * JavaFX support. Not needed for running from the command line.
  40. */
  41. public static void main(String[] args) {
  42. launch(args);
  43. }
  44. }
  45.  
  46. =======================================================================================================================================
  47.  
  48. package bounceballcontrol;
  49.  
  50. import javafx.animation.KeyFrame;
  51. import javafx.animation.Timeline;
  52. import javafx.beans.property.DoubleProperty;
  53. import javafx.scene.layout.Pane;
  54. import javafx.scene.paint.Color;
  55. import javafx.scene.shape.Circle;
  56. import javafx.util.Duration;
  57.  
  58. public class BallPane extends Pane {
  59. public final double radius = 20;
  60. private double x = radius, y = radius;
  61. private double dx = 5, dy = 2;
  62. private Circle circle = new Circle(x, y, radius);
  63. private Timeline animation;
  64.  
  65. public BallPane() {
  66. circle.setFill(Color.ORANGE); // Set ball color
  67. getChildren().add(circle); // Place a ball into this pane
  68.  
  69. // Create an animation for moving the ball
  70. animation = new Timeline(
  71. new KeyFrame(Duration.millis(50), e -> moveBall()));
  72. animation.setCycleCount(Timeline.INDEFINITE);
  73. animation.play(); // Start animation
  74. }
  75.  
  76. public void play() {
  77. animation.play();
  78. }
  79.  
  80. public void pause() {
  81. animation.pause();
  82. }
  83.  
  84. public void increaseSpeed() {
  85. animation.setRate(animation.getRate() + 0.1);
  86. }
  87.  
  88. public void decreaseSpeed() {
  89. animation.setRate(
  90. animation.getRate() > 0 ? animation.getRate() - 0.1 : 0);
  91. }
  92.  
  93. public DoubleProperty rateProperty() {
  94. return animation.rateProperty();
  95. }
  96.  
  97. protected void moveBall() {
  98. // Check boundaries
  99. if (x < radius || x > getWidth() - radius) {
  100. dx *= -1; // Change ball move direction
  101. }
  102. if (y < radius || y > getHeight() - radius) {
  103. dy *= -1; // Change ball move direction
  104. }
  105.  
  106. // Adjust ball position
  107. x += dx;
  108. y += dy;
  109. circle.setCenterX(x);
  110. circle.setCenterY(y);
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment