英文:
How to perform sequential animation
问题
I'll provide translations for the code and some context:
我正在使用JavaFX创建一个基本的动画,我有5个矩形排成一行,还有两个圆圈,一个是红色的,一个是蓝色的。红色圆圈位于第一个矩形上,蓝色圆圈位于第五个矩形上。
我想要实现的想法是:如果我点击第一个矩形,我希望红色圆圈通过平移移动到第五个矩形(通过翻译),一旦到达那里,位于第五个矩形上的蓝色圆圈将移动到第一个矩形上,换句话说,它们交换位置。
我在我的逻辑中使用了AnimationTimer类,但问题是,当按下鼠标事件发生时,两个圆圈的动画是同步的,这不是我想要的。我希望蓝色圆圈的动画在红色圆圈的动画完成后开始。我想了解为什么会发生这种情况?是否涉及多个线程?如果是这样,当我运行程序时,红色圆圈会在中间卡住,而蓝色圆圈会移出边界并隐藏,但是如果我注释掉一个圆圈的位置更新代码(update方法),应用程序将以正确的方式运行。希望能得到解答,非常感谢。
**另一个问题是:**如何使我的动画看起来更流畅,因为它会停顿一小会然后再继续移动。
If you have specific questions about parts of the code or need further explanations, feel free to ask.
英文:
I'm creating a basic animation with javaFX, I have 5 rectangles in a row, I have two cirlces, red and blue. The red cricle is set on the first rectangle and the blue one is set on the fifth rectangle.
The idea is: If I click the first rectangle I want the red circle to travel to the fifth rectangle (by a translation) and once it gets there the blue rectangle (which is on the fifth rectangle) travels to the first one, in another word they exchange positions. I used the AnimationTimer class in my logic but the porblem is that the animation of both cirlces is synchronized when the pressedMouse event happens and that's not what I want, what I want is that the animation of the blue circle starts once the red circle's one is finished. I want to understand why is that happening? Is it kinda multiple thread? In case it is, when I run the program the red circle gets stuck in the middle however the blue one travels out of the bounds and hides, however if I comment one of the cirlce's position code for update (the update method) the app runs correctly, I hope I get an answer and I'm so thankful.
Another question is: How to make my animation looks smoother because it stops for a fraction of a second and moves again.
here is my code:
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;  
public class animation extends Application {
        
        AnchorPane root = new AnchorPane();
        
        //create a matrix from rectangle class to stock 5 rectangle objects
        rectangle rect [] = new rectangle [5];
        
        //isMoving gets the true value when the animation starts
        private boolean isMoving = false;
        
        private int traveledDistance = 30;
    @Override
    public void start(Stage primaryStage) {
        //add 5 rectangles on the anchorpane
        //rect[0], rect[2] and rect[4] have BURLYWOOD color
        //rect[1], rect[3] have DARKBLUE color
        for(int i = 0; i<5; i++)
        {
            
           if(i%2 == 0)
            {
             rect[i] = new rectangle();
             rect[i].setFill(Color.BURLYWOOD);
                       
            }
            else
            {
                rect[i] = new rectangle();
                rect[i].setFill(Color.DARKBLUE);
            }
             //set all 5 rectangles as empty
             rect[i].setRectEmpty(true);
             //set all the 5 rectangles one after the other along the x axis
             rect[i].setTranslateX(i*60);
             //add the 5 rectangles to the parent 
             root.getChildren().add(rect[i]);
        }
        //instantiation of two circles (c and d) from cirlce class
        circle c = new circle(Color.RED);
        c.setName("redCircle");
        
        circle d = new circle(Color.BLUE);
        d.setName("blueCircle");
        
        //set the position of the red circle centered relatively to rect[0] 
        //rect[0] is no longer empty as it contains the red circle
        c.setTranslateX(30);
        c.setTranslateY(30);
        rect[0].setCircle(c);
        rect[0].setCircleName(c.getName());
        rect[0].setRectEmpty(false);
        root.getChildren().add(c);
        
        //set the position of the blue circle centered relatively to rect[4] 
        d.setTranslateX(4*60 +30);
        d.setTranslateY(30);
        rect[4].setCircle(d);
        rect[4].setCircleName(d.getName());
        root.getChildren().add(d);
        displayedScene(primaryStage);
        
        //when the parent is clicked
        root.setOnMousePressed(new EventHandler<MouseEvent>(){
            @Override
            public void handle(MouseEvent event) {
                //get the index of the clicked rectangle
                int index = (int) event.getX()/60;
                //if the clicked rectangle contains the red circle inside
                if(!rect[index].isRectEmpty() && rect[index].getCircleName().equals("redCircle"))
                {
                    Circle circle = rect[index].getCircle();
                    //update the postion of the red circle so that it occupies the last rectangle (rect[4])
                        update(index,5, circle);
                    //update the position of the blue circle so that it occupies the first rectangle(rect[0])
                        update(5,0, rect[4].getCircle());
                       
                }
            }               
                    
        });       
    }
    
    //update method uses the AnimationTimer class
    public void update(int initialPos, int lastPos, Circle circle)
    {
       AnimationTimer timer = new AnimationTimer() {
       @Override
       public void handle(long now) {
             
           updateCirclePosition(initialPos, lastPos, circle);
           if(!isMoving)
           {
               this.stop();
           }   
        }
        };
        timer.start();
    }
 
    public void updateCirclePosition(int initialPos, int lastPos, Circle circle)
    {
       int dx = 2;
       if(initialPos>lastPos)
       {
           dx = -1*dx;
       }
       isMoving = true;
       int distance = Math.abs((lastPos - initialPos)*60);
       
       if(traveledDistance<distance-30)
       {
          circle.setTranslateX(circle.getTranslateX() + dx); 
          traveledDistance +=Math.abs(dx);
       }
       else{
           isMoving = false;
           traveledDistance = 30;
       }
    }
     
    //load the Stage
    public void displayedScene(Stage primaryStage)
    {
      Scene scene = new Scene(root, 300, 60);
        primaryStage.setScene(scene);
        primaryStage.show();
        
    }
    public static void main(String[] args) {
        launch(args);
    }
   // circle class extends Circle
    
        public class circle extends Circle
    {
        private String name;
        public circle(Paint color) {
            
            super(30, color);             
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }       
    }
}
And here is rectangle class:
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
//rectangle class extends Rectangle
public class rectangle extends Rectangle {
    private Circle circle;
    private String circleName;
    private boolean  rectEmpty;
    
    public rectangle() {
        super(60, 60);        
    }
    public Circle getCircle() {
        return circle;
    }
    public void setCircle(Circle circle) {
        this.circle = circle;
    }
    public boolean isRectEmpty() {
        return rectEmpty;
    }
    public void setRectEmpty(boolean rectEmpty) {
        this.rectEmpty = rectEmpty;
    }
    public String getCircleName() {
        return circleName;
    }
    public void setCircleName(String circleName) {
        this.circleName = circleName;
    }       
}

答案1
得分: 1
以下是演示所请求功能的MRE(最小可重现示例):
以下是通过`animateCircles()`执行的圆圈动画。它使用`TranslateTransition`来将一个圆从一个位置平移到另一个位置。
[setOnFinished](https://docs.oracle.com/javase/8/javafx/api/javafx/animation/Animation.html#setOnFinished-javafx.event.EventHandler-) 用于启动下一个动画。
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Animation extends Application {
    private static final double SQUARE_SIZE = 60, RADIUS = SQUARE_SIZE /2, ANIMATION_TIME = 1;
    private final Pane root = new Pane();
    private final Rectangle rect [] = new Rectangle [5];
    private final Circle circles [] = new Circle[2];
    private boolean isMoving = false, isSwapped = false;
    @Override
    public void start(Stage primaryStage) {
        for(int i = 0; i<rect.length; i++) {
            rect[i] = new Rectangle(SQUARE_SIZE, SQUARE_SIZE, i%2 == 0 ? Color.BURLYWOOD :  Color.DARKBLUE);
            //将5个矩形依次设置在x轴上
            rect[i].setTranslateX(i*SQUARE_SIZE);
            root.getChildren().add(rect[i]);
        }
        circles[0] = new Circle(RADIUS,Color.RED);
        circles[1] = new Circle(RADIUS,Color.BLUE);
        //将红色圆圈的位置设置为位于rect[0]的中心
        Point2D center = centerOf(rect[0]);
        circles[0].setTranslateX(center.getX());
        circles[0].setTranslateY(center.getY());
        //将蓝色圆圈的位置设置为位于rect[4]的中心
        center = centerOf(rect[4]);
        circles[1].setTranslateX(center.getX());
        circles[1].setTranslateY(center.getY());
        root.getChildren().add(circles[0]);
        root.getChildren().add( circles[1]);
        Scene scene = new Scene(root, SQUARE_SIZE*rect.length, SQUARE_SIZE);
        primaryStage.setScene(scene);
        primaryStage.show();
        root.setOnMousePressed(event -> animateCircles());
    }
    //返回矩形的中心点
    private Point2D centerOf(Rectangle rect) {
        Bounds bounds = rect.getBoundsInParent();
        double x = bounds.getMinX() + 0.5 * bounds.getWidth();
        double y = bounds.getMinY() + 0.5 * bounds.getHeight();
        return new Point2D(x, y);
    }
    private void animateCircles() {
        if(isMoving) return;
        TranslateTransition translateCircle0 = new TranslateTransition(Duration.seconds(ANIMATION_TIME), circles[0]);
        translateCircle0.setToX( isSwapped ?  centerOf(rect[0]).getX()  : centerOf(rect[4]).getX());
        TranslateTransition translateCircle1 = new TranslateTransition(Duration.seconds(ANIMATION_TIME), circles[1]);
        translateCircle1.setToX( isSwapped ?  centerOf(rect[4]).getX() : centerOf(rect[0]).getX());
        translateCircle0.setOnFinished(e -> {
            translateCircle1.play();
        });
        translateCircle1.setOnFinished(e -> {
            isMoving = false;
            isSwapped = ! isSwapped;
        });
        isMoving = true;
        translateCircle0.play();
    }
    public static void main(String[] args) {
        launch(args);
    }
}
或者,您可以使用SequentialTransition实现animateCircles():
private void animateCircles() {
    if(isMoving) return;
    TranslateTransition translateCircle0 = new TranslateTransition(Duration.seconds(ANIMATION_TIME), circles[0]);
    translateCircle0.setToX( isSwapped ?  centerOf(rect[0]).getX()  : centerOf(rect[4]).getX());
    TranslateTransition translateCircle1 = new TranslateTransition(Duration.seconds(ANIMATION_TIME), circles[1]);
    translateCircle1.setToX( isSwapped ?  centerOf(rect[4]).getX() : centerOf(rect[0]).getX());
    SequentialTransition sequentialTransition = new SequentialTransition(translateCircle0, translateCircle1);
    isMoving = true;
    sequentialTransition.play();
    sequentialTransition.setOnFinished(e -> {
        isMoving = false;
        isSwapped = ! isSwapped;
    });
}
英文:
The following is an mre demonstrating the requested functionality.<br/>
Circles animation is done by animateCircles(). It uses TranslateTransition to translate a circle from one position to the other.<br/>
setOnFinished is used to start the next animation.<br/>
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Animation extends Application {
private static final double SQUARE_SIZE = 60, RADIUS = SQUARE_SIZE /2, ANIMATION_TIME = 1;
private final Pane root = new Pane();
private final Rectangle rect [] = new Rectangle [5];
private final Circle circles [] = new Circle[2];
private boolean isMoving = false, isSwapped = false;
@Override
public void start(Stage primaryStage) {
for(int i = 0; i<rect.length; i++) {
rect[i] = new Rectangle(SQUARE_SIZE, SQUARE_SIZE, i%2 == 0 ? Color.BURLYWOOD :  Color.DARKBLUE);
//set all the 5 rectangles one after the other along the x axis
rect[i].setTranslateX(i*SQUARE_SIZE);
root.getChildren().add(rect[i]);
}
circles[0] = new Circle(RADIUS,Color.RED);
circles[1] = new Circle(RADIUS,Color.BLUE);
//set the position of the red circle centered to rect[0]
Point2D center = centerOf(rect[0]);
circles[0].setTranslateX(center.getX());
circles[0].setTranslateY(center.getY());
//set the position of the blue circle centered  to rect[4]
center = centerOf(rect[4]);
circles[1].setTranslateX(center.getX());
circles[1].setTranslateY(center.getY());
root.getChildren().add(circles[0]);
root.getChildren().add( circles[1]);
Scene scene = new Scene(root, SQUARE_SIZE*rect.length, SQUARE_SIZE);
primaryStage.setScene(scene);
primaryStage.show();
root.setOnMousePressed(event -> animateCircles());
}
//return the center point 
private Point2D centerOf(Rectangle rect) {
Bounds bounds = rect.getBoundsInParent();
double x = bounds.getMinX() + 0.5 * bounds.getWidth();
double y = bounds.getMinY() + 0.5 * bounds.getHeight();
return new Point2D(x, y);
}
private void animateCircles() {
if(isMoving) return;
TranslateTransition translateCircle0 = new TranslateTransition(Duration.seconds(ANIMATION_TIME), circles[0]);
translateCircle0.setToX( isSwapped ?  centerOf(rect[0]).getX()  : centerOf(rect[4]).getX());
TranslateTransition translateCircle1 = new TranslateTransition(Duration.seconds(ANIMATION_TIME), circles[1]);
translateCircle1.setToX( isSwapped ?  centerOf(rect[4]).getX() : centerOf(rect[0]).getX());
translateCircle0.setOnFinished(e-> {
translateCircle1.play();
});
translateCircle1.setOnFinished(e-> {
isMoving = false;
isSwapped = ! isSwapped;
});
isMoving = true;
translateCircle0.play();
}
public static void main(String[] args) {
launch(args);
}
}
Alternatively you could implement animateCircles() using SequentialTransition:
private void animateCircles() {
if(isMoving) return;
TranslateTransition translateCircle0 = new TranslateTransition(Duration.seconds(ANIMATION_TIME), circles[0]);
translateCircle0.setToX( isSwapped ?  centerOf(rect[0]).getX()  : centerOf(rect[4]).getX());
TranslateTransition translateCircle1 = new TranslateTransition(Duration.seconds(ANIMATION_TIME), circles[1]);
translateCircle1.setToX( isSwapped ?  centerOf(rect[4]).getX() : centerOf(rect[0]).getX());
SequentialTransition sequentialTransition = new SequentialTransition(translateCircle0, translateCircle1);
isMoving = true;
sequentialTransition.play();
sequentialTransition.setOnFinished(e-> {
isMoving = false;
isSwapped = ! isSwapped;
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论