/
Shalva
/
Lab2_EventHandler
Обзор
Документация
Войти
/
Shalva
/
Lab2_EventHandler
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/com/example/lab3/MainApp.java
78 строк
2 KB
Kvaratskheliia Shalva
Добавлен исходный код проекта Lab2_EventHandler
14 июн 2026, 17:05
14 июн 2026, 17:05
3080437
Код
Авторство
О чём код?
package com.example.lab3; import javafx.animation.AnimationTimer; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; import java.util.Random; public class MainApp extends Application { private static final int WIDTH = 900; private static final int HEIGHT = 700; private static final int BALL_RADIUS = 45; private int score = 0; @Override public void start(Stage primaryStage) { Pane root = new Pane(); root.setStyle("-fx-background-color: #EAEAEA"); Scene scene = new Scene(root, WIDTH, HEIGHT); Text scoreText = new Text("ОЧКИ: 0"); scoreText.setFont(new Font("Arial", 30)); scoreText.setX(20); scoreText.setY(30); root.getChildren().add(scoreText); CreateBall ball = new CreateBall(BALL_RADIUS, Color.CORAL, WIDTH / 2.0, HEIGHT / 2.0); root.getChildren().add(ball); ball.setOnMousePressed(event -> { score++; scoreText.setText("ОЧКИ: " + score); Random randomColor = new Random(); ball.setFill(Color.rgb(randomColor.nextInt(256), randomColor.nextInt(256), randomColor.nextInt(256))); ball.setDx(ball.getDx()); ball.setDy(ball.getDy()); }); Random random = new Random(); AnimationTimer timer = new AnimationTimer() { @Override public void handle(long l) { double nextX = ball.getLayoutX() + ball.getDx(); double nextY = ball.getLayoutY() + ball.getDy(); if (nextX <= BALL_RADIUS || nextX >= WIDTH - BALL_RADIUS) { ball.setDx(-ball.getDx()); ball.setDy(ball.getDy() + (random.nextDouble() - 0.5)); } if (nextY <= BALL_RADIUS || nextY >= HEIGHT - BALL_RADIUS) { ball.setDy(-ball.getDy()); ball.setDx(ball.getDx() + (random.nextDouble() - 0.5)); } ball.setLayoutX(ball.getLayoutX() + ball.getDx()); ball.setLayoutY(ball.getLayoutY() + ball.getDy()); } }; timer.start(); primaryStage.setTitle("ИГРА: ЛОВИ ШАРИК"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }