Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.akadatsky;
- import javafx.scene.canvas.GraphicsContext;
- import javafx.scene.paint.Color;
- import java.util.Random;
- public class Ball implements Shape {
- public static final int SHAPE_SIZE = 50;
- private final GraphicsContext gc;
- private final double SCREEN_X;
- private final double SCREEN_Y;
- private double x;
- private double y;
- private double xSpeed;
- private double ySpeed;
- public Ball(GraphicsContext gc) {
- this.gc = gc;
- SCREEN_X = gc.getCanvas().getWidth();
- SCREEN_Y = gc.getCanvas().getHeight();
- Random random = new Random();
- xSpeed = random.nextInt(10) - 5;
- ySpeed = random.nextInt(10) - 5;
- x = random.nextInt(400) + 20;
- y = random.nextInt(400) + 20;
- }
- public void move() {
- x += xSpeed;
- y += ySpeed;
- if (x + SHAPE_SIZE > SCREEN_X) {
- xSpeed = - (Math.abs(xSpeed));
- }
- if (y + SHAPE_SIZE > SCREEN_Y) {
- ySpeed = - (Math.abs(ySpeed));
- }
- if (x < 0) {
- xSpeed = Math.abs(xSpeed);
- }
- if (y < 0) {
- ySpeed = Math.abs(ySpeed);
- }
- }
- public void draw() {
- gc.setFill(Color.RED);
- gc.fillOval(x, y, SHAPE_SIZE, SHAPE_SIZE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement