Java 如何使用AnimationTimer?

Java 如何使用AnimationTimer?,java,animation,javafx,Java,Animation,Javafx,因此,我尝试使用JavaFX创建具有清晰动画的心形形状。 这是我的密码: public class Main extends Application { ArrayList<Circle> circles = new ArrayList<>(); ArrayList<Line> lines = new ArrayList<>(); Group g; int counter = 0; int factor =

因此,我尝试使用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);
    }
}
但这不是我想要的)。 所以,我的问题是:

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();
            }
        }
    };

他正在工作。它只从根目录中删除,而不是从屏幕上删除,我想知道原因。

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

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);
    }
}
import java.util.ArrayList;
导入javafx.animation.AnimationTimer;
导入javafx.application.application;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Circle;
导入javafx.scene.shape.Line;
导入javafx.stage.stage;
公共类主扩展应用程序{
私有最终ArrayList圆圈=新ArrayList();
private final arraylistline=new ArrayList();
专用整数计数器=0,总计=100;
private int factor=2;//因子1不画线
private int deletedlinesconter=0;//j不是一个好的名称选择
私有AnimationTimer draw,deleteLines;//为什么是静态的?
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效开始(阶段primaryStage){
g组=新组();
场景s=新场景(g,600600);
s、 设置填充(颜色:黑色);
初级阶段.第二纪(s);
primaryStage.show();
//不需要
//圆圈c=新圆圈(300300300);
//c、 设置填充(颜色:黑色);
//c、 设定行程(颜色为黑色);
//g、 getChildren().add(c);
拉丝圈(g);
draw=新建动画计时器(){
@凌驾
公共无效句柄(长){
如果(计数器==总数){
deleteLines.start();
stop();//停止绘制,直到通过deleteLines重新启动
}否则{
抽绳(g);//抽绳(g);
计数器++;
}
}
};
deleteLines=新建动画计时器(){
@凌驾
公共无效句柄(长){
if(deletedlinesconternode==lines.get(deletedLinesCenter));
删除LinesCenter++;
}否则{
//准备下次抽签
总计++;
计数器=0;
因子++;
DeletedLinesCenter=0;
行。清除();
g、 getChildren().removeAll(圆);
圆圈。清除();
拉丝圈(g);
停止();
draw.start();
}
}
};
draw.start();
}
专用空位抽绳(g组){
行l=新行(圆.get(计数器总数的%).getCenterX(),
circles.get(计数器总数的%).getCenterY(),
circles.get(计数器*系数%total).getCenterX(),
circles.get(计数器*系数%total).getCenterY();
l、 设定行程(颜色:紫色);
g、 getChildren().add(l);
行。添加(l);
}
//此方法仅保持添加同一行的总次数
//专用作废提款线(g组){
//对于(int i=0;i


非常感谢。对不起,我的编码不好,再次感谢你告诉我我的错误。下次我会小心的。我很高兴能帮上忙。
 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);
    }
}