javafx时间轴和鼠标事件(javafx timeline and mouse event)

编程入门 行业动态 更新时间:2024-10-25 06:27:59
javafx时间轴和鼠标事件(javafx timeline and mouse event)

当我运行此代码时,鼠标单击时操作将停止。 点击鼠标时,球将停止动作。 虽然我点击鼠标添加其他球,但我如何让球继续做动作。

import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Bounds; import javafx.scene.Scene; import javafx.scene.input.MouseButton; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.util.Duration; public class GamePractice extends Application { public static Circle circle; public static Pane canvas; @Override public void start(final Stage primaryStage) { canvas = new Pane(); final Scene scene = new Scene(canvas, 800, 600); primaryStage.setTitle("Game"); primaryStage.setScene(scene); primaryStage.show(); circle = new Circle(15, Color.BLUE); circle.relocate(100, 100); canvas.getChildren().addAll(circle); final Timeline loop = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() { double deltaX = (double)(Math.random()*10) + 3; double deltaY = (double)(Math.random()*10) + 3; @Override public void handle(final ActionEvent t) { circle.setLayoutX(circle.getLayoutX() + deltaX); circle.setLayoutY(circle.getLayoutY() + deltaY); final Bounds bounds = canvas.getBoundsInLocal(); final boolean atRightBorder = circle.getLayoutX() >= (bounds.getMaxX() - circle.getRadius()); final boolean atLeftBorder = circle.getLayoutX() <= (bounds.getMinX() + circle.getRadius()); final boolean atBottomBorder = circle.getLayoutY() >= (bounds.getMaxY() - circle.getRadius()); final boolean atTopBorder = circle.getLayoutY() <= (bounds.getMinY() + circle.getRadius()); if (atRightBorder || atLeftBorder) { deltaX *= -1; } if (atBottomBorder || atTopBorder) { deltaY *= -1; } } })); scene.setOnMousePressed(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { if (event.getButton() == MouseButton.PRIMARY) { if (!(canvas.getChildren().isEmpty())) { canvas.getChildren().remove(0); } } else { int red = (int)(Math.random()*256); int green = (int)(Math.random()*256); int blue = (int)(Math.random()*256); int x = (int)(Math.random()*801); int y = (int)(Math.random()*601); circle = new Circle(15, Color.rgb(red, green, blue)); circle.relocate(x, y); canvas.getChildren().addAll(circle); } } }); loop.setCycleCount(Timeline.INDEFINITE); loop.play(); } public static void main(final String[] args) { launch(args); } }

when I run this code this, the action will stop when the mouse click. The ball will stop action when I click mouse. how do I make the ball do the action continuously although I click the mouse to add other balls.

import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Bounds; import javafx.scene.Scene; import javafx.scene.input.MouseButton; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.util.Duration; public class GamePractice extends Application { public static Circle circle; public static Pane canvas; @Override public void start(final Stage primaryStage) { canvas = new Pane(); final Scene scene = new Scene(canvas, 800, 600); primaryStage.setTitle("Game"); primaryStage.setScene(scene); primaryStage.show(); circle = new Circle(15, Color.BLUE); circle.relocate(100, 100); canvas.getChildren().addAll(circle); final Timeline loop = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() { double deltaX = (double)(Math.random()*10) + 3; double deltaY = (double)(Math.random()*10) + 3; @Override public void handle(final ActionEvent t) { circle.setLayoutX(circle.getLayoutX() + deltaX); circle.setLayoutY(circle.getLayoutY() + deltaY); final Bounds bounds = canvas.getBoundsInLocal(); final boolean atRightBorder = circle.getLayoutX() >= (bounds.getMaxX() - circle.getRadius()); final boolean atLeftBorder = circle.getLayoutX() <= (bounds.getMinX() + circle.getRadius()); final boolean atBottomBorder = circle.getLayoutY() >= (bounds.getMaxY() - circle.getRadius()); final boolean atTopBorder = circle.getLayoutY() <= (bounds.getMinY() + circle.getRadius()); if (atRightBorder || atLeftBorder) { deltaX *= -1; } if (atBottomBorder || atTopBorder) { deltaY *= -1; } } })); scene.setOnMousePressed(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { if (event.getButton() == MouseButton.PRIMARY) { if (!(canvas.getChildren().isEmpty())) { canvas.getChildren().remove(0); } } else { int red = (int)(Math.random()*256); int green = (int)(Math.random()*256); int blue = (int)(Math.random()*256); int x = (int)(Math.random()*801); int y = (int)(Math.random()*601); circle = new Circle(15, Color.rgb(red, green, blue)); circle.relocate(x, y); canvas.getChildren().addAll(circle); } } }); loop.setCycleCount(Timeline.INDEFINITE); loop.play(); } public static void main(final String[] args) { launch(args); } }

最满意答案

只需为每个圆圈创建一个动画:

import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Bounds; import javafx.scene.Scene; import javafx.scene.input.MouseButton; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.util.Duration; public class GamePractice extends Application { private Pane canvas; @Override public void start(final Stage primaryStage) { canvas = new Pane(); final Scene scene = new Scene(canvas, 800, 600); primaryStage.setTitle("Game"); primaryStage.setScene(scene); primaryStage.show(); addCircle(100, 100, Color.BLUE); scene.setOnMousePressed(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { if (event.getButton() == MouseButton.PRIMARY) { if (!(canvas.getChildren().isEmpty())) { canvas.getChildren().remove(0); } } else { int red = (int)(Math.random()*256); int green = (int)(Math.random()*256); int blue = (int)(Math.random()*256); int x = (int)(Math.random()*801); int y = (int)(Math.random()*601); addCircle(x, y, Color.rgb(red, green, blue)); } } }); } private void addCircle(double x, double y, Color color) { Circle circle = new Circle(15, color); circle.relocate(x, y); canvas.getChildren().addAll(circle); final Timeline loop = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() { double deltaX = (double)(Math.random()*10) + 3; double deltaY = (double)(Math.random()*10) + 3; @Override public void handle(final ActionEvent t) { circle.setLayoutX(circle.getLayoutX() + deltaX); circle.setLayoutY(circle.getLayoutY() + deltaY); final Bounds bounds = canvas.getBoundsInLocal(); final boolean atRightBorder = circle.getLayoutX() >= (bounds.getMaxX() - circle.getRadius()); final boolean atLeftBorder = circle.getLayoutX() <= (bounds.getMinX() + circle.getRadius()); final boolean atBottomBorder = circle.getLayoutY() >= (bounds.getMaxY() - circle.getRadius()); final boolean atTopBorder = circle.getLayoutY() <= (bounds.getMinY() + circle.getRadius()); if (atRightBorder || atLeftBorder) { deltaX *= -1; } if (atBottomBorder || atTopBorder) { deltaY *= -1; } } })); loop.setCycleCount(Timeline.INDEFINITE); loop.play(); } public static void main(final String[] args) { launch(args); } }

Just create an animation for each circle:

import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Bounds; import javafx.scene.Scene; import javafx.scene.input.MouseButton; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.util.Duration; public class GamePractice extends Application { private Pane canvas; @Override public void start(final Stage primaryStage) { canvas = new Pane(); final Scene scene = new Scene(canvas, 800, 600); primaryStage.setTitle("Game"); primaryStage.setScene(scene); primaryStage.show(); addCircle(100, 100, Color.BLUE); scene.setOnMousePressed(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { if (event.getButton() == MouseButton.PRIMARY) { if (!(canvas.getChildren().isEmpty())) { canvas.getChildren().remove(0); } } else { int red = (int)(Math.random()*256); int green = (int)(Math.random()*256); int blue = (int)(Math.random()*256); int x = (int)(Math.random()*801); int y = (int)(Math.random()*601); addCircle(x, y, Color.rgb(red, green, blue)); } } }); } private void addCircle(double x, double y, Color color) { Circle circle = new Circle(15, color); circle.relocate(x, y); canvas.getChildren().addAll(circle); final Timeline loop = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() { double deltaX = (double)(Math.random()*10) + 3; double deltaY = (double)(Math.random()*10) + 3; @Override public void handle(final ActionEvent t) { circle.setLayoutX(circle.getLayoutX() + deltaX); circle.setLayoutY(circle.getLayoutY() + deltaY); final Bounds bounds = canvas.getBoundsInLocal(); final boolean atRightBorder = circle.getLayoutX() >= (bounds.getMaxX() - circle.getRadius()); final boolean atLeftBorder = circle.getLayoutX() <= (bounds.getMinX() + circle.getRadius()); final boolean atBottomBorder = circle.getLayoutY() >= (bounds.getMaxY() - circle.getRadius()); final boolean atTopBorder = circle.getLayoutY() <= (bounds.getMinY() + circle.getRadius()); if (atRightBorder || atLeftBorder) { deltaX *= -1; } if (atBottomBorder || atTopBorder) { deltaY *= -1; } } })); loop.setCycleCount(Timeline.INDEFINITE); loop.play(); } public static void main(final String[] args) { launch(args); } }

更多推荐

本文发布于:2023-07-15 05:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110630.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标   事件   时间   javafx   mouse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!