Advertisement
Nick-O-Rama

BouncingBall2

Sep 25th, 2015
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. package bouncingball2;
  2.  
  3. import java.util.Random;
  4. import javafx.animation.KeyFrame;
  5. import javafx.animation.Timeline;
  6. import javafx.application.Application;
  7. import javafx.event.ActionEvent;
  8. import javafx.event.EventHandler;
  9. import javafx.scene.Group;
  10. import javafx.scene.Scene;
  11. import javafx.scene.control.Label;
  12. import javafx.scene.input.MouseButton;
  13. import javafx.scene.paint.Color;
  14. import javafx.scene.shape.Circle;
  15. import javafx.scene.shape.Rectangle;
  16. import javafx.stage.Stage;
  17. import javafx.util.Duration;
  18.  
  19. public class BouncingBall2 extends Application {
  20.  
  21.     private Random rand = new Random();
  22.     private Rectangle rect;
  23.     private Circle circle;
  24.     private Label label;
  25.     private Label count;
  26.     private int eats;
  27.     private int incX = rand.nextInt(3) + 1, incY = rand.nextInt(2) + 1;
  28.  
  29.     @Override
  30.     public void start(Stage stage) throws Exception {
  31.         Group root = new Group();
  32.         circle = new Circle(200, 200, 25);
  33.         circle.setFill(Color.RED);
  34.         rect = new Rectangle(rand.nextInt(395), rand.nextInt(395), 70, 70);
  35.         label = new Label("Eats: ");
  36.         count = new Label("0");
  37.         count.setTranslateX(32);
  38.         rect.setFill(Color.BLUE);
  39.         root.getChildren().add(label);
  40.         root.getChildren().add(count);
  41.         root.getChildren().add(rect);
  42.         root.getChildren().add(circle);
  43.         Scene scene = new Scene(root, 400, 400);
  44.         stage.setScene(scene);
  45.         stage.show();
  46.         EventHandler<ActionEvent> eventHandler = e -> {
  47.             if (circle.getCenterX() > 400 || circle.getCenterX() < 0) {
  48.                 incX *= -1;
  49.             }
  50.  
  51.             if (circle.getCenterY() > 400 || circle.getCenterY() < 0) {
  52.                 incY *= -1;
  53.             }
  54.             circle.setCenterX(circle.getCenterX() + incX);
  55.             circle.setCenterY(circle.getCenterY() + incY);
  56.            
  57.             if (circle.getCenterX() >= rect.getX()-21.5 && circle.getCenterX() <= rect.getX() + 91.5 &&
  58.                     circle.getCenterY() >= rect.getY()-21.5 && circle.getCenterY() <= rect.getY() + 91.5){
  59.                 eats++;
  60.                 count.setText(eats + "");
  61.                 circle.setCenterX(rand.nextInt(390));
  62.                 circle.setCenterY(rand.nextInt(390));
  63.             }
  64.                
  65.         };
  66.         rect.setOnKeyPressed(e -> {
  67.             switch (e.getCode()) {
  68.                 case DOWN:
  69.                     if (rect.getY() < 300) {
  70.                         rect.setY(rect.getY() + 10);
  71.                     }
  72.                     break;
  73.                 case UP:
  74.                     if (rect.getY() > 0) {
  75.                         rect.setY(rect.getY() - 10);
  76.                     }
  77.                     break;
  78.                 case LEFT:
  79.                     if (rect.getX() > 0) {
  80.                         rect.setX(rect.getX() - 10);
  81.                     }
  82.                     break;
  83.                 case RIGHT:
  84.                     if (rect.getX() < 250) {
  85.                         rect.setX(rect.getX() + 10);
  86.                     }
  87.                     break;
  88.             }
  89.         });
  90.         rect.requestFocus();
  91.         Timeline animation = new Timeline(new KeyFrame(Duration.millis(19), eventHandler));
  92.         animation.setCycleCount(Timeline.INDEFINITE);
  93.         animation.play();
  94.         circle.setOnMousePressed(e->{
  95.             if (e.getButton().equals(MouseButton.PRIMARY)){
  96.                 if (animation.getStatus().equals(animation.getStatus().RUNNING))
  97.                     animation.pause();
  98.                 else
  99.                     animation.play();
  100.             }
  101.             else
  102.                 animation.setRate(animation.getRate() + 1);
  103.  
  104.         });
  105.     }
  106.  
  107.     public static void main(String[] args) {
  108.         launch(args);
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement