Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. package com.akadatsky;
  2.  
  3. import javafx.scene.canvas.GraphicsContext;
  4. import javafx.scene.paint.Color;
  5.  
  6. import java.util.Random;
  7.  
  8. public class Ball implements Shape {
  9.  
  10. public static final int SHAPE_SIZE = 50;
  11. private final GraphicsContext gc;
  12. private final double SCREEN_X;
  13. private final double SCREEN_Y;
  14. private double x;
  15. private double y;
  16. private double xSpeed;
  17. private double ySpeed;
  18.  
  19. public Ball(GraphicsContext gc) {
  20. this.gc = gc;
  21. SCREEN_X = gc.getCanvas().getWidth();
  22. SCREEN_Y = gc.getCanvas().getHeight();
  23.  
  24. Random random = new Random();
  25.  
  26. xSpeed = random.nextInt(10) - 5;
  27. ySpeed = random.nextInt(10) - 5;
  28.  
  29. x = random.nextInt(400) + 20;
  30. y = random.nextInt(400) + 20;
  31. }
  32.  
  33. public void move() {
  34. x += xSpeed;
  35. y += ySpeed;
  36.  
  37. if (x + SHAPE_SIZE > SCREEN_X) {
  38. xSpeed = - (Math.abs(xSpeed));
  39. }
  40. if (y + SHAPE_SIZE > SCREEN_Y) {
  41. ySpeed = - (Math.abs(ySpeed));
  42. }
  43. if (x < 0) {
  44. xSpeed = Math.abs(xSpeed);
  45. }
  46. if (y < 0) {
  47. ySpeed = Math.abs(ySpeed);
  48. }
  49. }
  50.  
  51. public void draw() {
  52. gc.setFill(Color.RED);
  53. gc.fillOval(x, y, SHAPE_SIZE, SHAPE_SIZE);
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement