gpt4 book ai didi

java - 如何使用动画定时器?

转载 作者:行者123 更新时间:2023-12-02 08:40:50 24 4
gpt4 key购买 nike

因此,我尝试使用 JavaFX 创建具有清晰动画的心形形状。这是我的代码:

public class Main extends Application {
ArrayList<Circle> circles = new ArrayList<>();
ArrayList<Line> lines = new ArrayList<>();
Group g;
int counter = 0;
int factor = 1;
int total = 100;
int j = 0;
static AnimationTimer draw;
static AnimationTimer deleteLines;

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
g = new Group();
Scene s = new Scene(g, 600, 600);
s.setFill(Color.BLACK);
primaryStage.setScene(s);
primaryStage.show();

Circle c = new Circle(300, 300,300);
c.setFill(Color.BLACK);
c.setStroke(Color.BLACK);
drawCircle(g);
g.getChildren().addAll(c);

draw = new AnimationTimer(){
@Override
public void handle(long now) {

if(counter == total){
total++;
counter = 0;
deleteLines.start();
lines.clear();
g.getChildren().removeAll(circles);
circles.clear();
factor++;
drawCircle(g);
} else {
drawLines(g);
counter++;
}
}
};
deleteLines = new AnimationTimer() {
@Override
public void handle(long now) {
if (j < lines.size()){
draw.stop();
g.getChildren().removeIf(node -> node == lines.get(j));
j++;
} else {
j = 0;
this.stop();
draw.start();
}
}
};
draw.start();
}

private void drawLines(Group g) {
for (int i = 0; i < total; i++) {
Line l = new Line(circles.get(counter % total).getCenterX(), circles.get(counter % total).getCenterY(), circles.get(counter * factor % total).getCenterX(), circles.get(counter * factor % total).getCenterY());
l.setStroke(Color.PURPLE);
g.getChildren().add(l);
lines.add(l);
}
}

private void drawCircle(Group g) {
for (int i = 0; i < total; i++) {
Circle dot = new Circle(Math.cos(2*Math.PI/ total * i + Math.PI / 2)* 300 + 300,Math.sin(2*Math.PI/ total * i + Math.PI / 2 )* 300 + 300, 5);
dot.setFill(Color.PURPLE);
circles.add(dot);
}
g.getChildren().addAll(circles);
}
}

我的目标是一一显示所有行,然后再一一完全删除它们(我是用

 g.getChildren().removeAll(lines); 

但这不是我想要的)。所以,这是我的问题:

deleteLines = new AnimationTimer() {
@Override
public void handle(long now) {
if (j < lines.size()){
draw.stop();
g.getChildren().removeIf(node -> node == lines.get(j));
j++;
} else {
j = 0;
this.stop();
draw.start();
}
}
};

在这一部分中,

 g.getChildren().removeIf(node -> node == lines.get(j));

正在工作。它仅从根目录中删除,而不是在屏幕上删除,我想知道为什么。

最佳答案

我认为以下内容符合您的要求。请注意评论:

import java.util.ArrayList;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Main extends Application {

private final ArrayList<Circle> circles = new ArrayList<>();
private final ArrayList<Line>lines = new ArrayList<>();
private int counter = 0, total = 100;
private int factor = 2; //factor 1 does not draw lines
private int deletedLinesCounter = 0; //j is not a good choice of name
private AnimationTimer draw, deleteLines; //why static ?

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {

Group g = new Group();
Scene s = new Scene(g, 600, 600);
s.setFill(Color.BLACK);
primaryStage.setScene(s);
primaryStage.show();

//not needed
//Circle c = new Circle(300, 300,300);
//c.setFill(Color.BLACK);
//c.setStroke(Color.BLACK);
//g.getChildren().add(c);

drawCircle(g);

draw = new AnimationTimer(){
@Override
public void handle(long now) {

if(counter == total){
deleteLines.start();
stop(); //stop draw until it restarts by deleteLines
} else {
drawLine(g); // drawLines(g);
counter++;
}
}
};

deleteLines = new AnimationTimer() {
@Override
public void handle(long now) {
if (deletedLinesCounter < lines.size()){
//draw.stop(); // no need to stop over again. stop in draw
g.getChildren().removeIf(node -> node == lines.get(deletedLinesCounter));
deletedLinesCounter++;
} else {
//prepare next draw
total++;
counter = 0;
factor++;
deletedLinesCounter = 0;
lines.clear();
g.getChildren().removeAll(circles);
circles.clear();
drawCircle(g);
stop();
draw.start();
}
}
};

draw.start();
}

private void drawLine(Group g) {

Line l = new Line(circles.get(counter % total).getCenterX(),
circles.get(counter % total).getCenterY(),
circles.get(counter * factor % total).getCenterX(),
circles.get(counter * factor % total).getCenterY());
l.setStroke(Color.PURPLE);
g.getChildren().add(l);
lines.add(l);
}

//this method only keeps adding the same line total times
// private void drawLines(Group g) {
// for (int i = 0; i < total; i++) {
// Line l = new Line(circles.get(counter % total).getCenterX(),
// circles.get(counter % total).getCenterY(),
// circles.get(counter * factor % total).getCenterX(),
// circles.get(counter * factor % total).getCenterY());
// l.setStroke(Color.PURPLE);
// g.getChildren().add(l);
// lines.add(l);
// }
// }

private void drawCircle(Group g) {
for (int i = 0; i < total; i++) {
Circle dot = new Circle(Math.cos(2*Math.PI/ total * i + Math.PI / 2)* 300 + 300,
Math.sin(2*Math.PI/ total * i + Math.PI / 2 )* 300 + 300, 5);
dot.setFill(Color.PURPLE);
circles.add(dot);
}
g.getChildren().addAll(circles);
}
}
<小时/>

enter image description here

关于java - 如何使用动画定时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61399975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com