使用翻译过渡和JavaFX的Java OOP

时间:2019-02-03 16:32:27

标签: oop javafx transition translate

不输出转换过渡。它在主类中使用一种方法。我认为它不起作用,因为它被用作对象。必须有一个不同的代码才能实现。它在主体中使用一种方法,然后将其放入测试器中。但是,我不知道如何使用它,因为它也使用构造函数/对象。然后,该对象转动并变为我对其进行了更改的节点。 I do not know how the Translate Transition method is attached to the object and displays it into the javafx console.如图所示,请帮助解决问题以获得积极的反馈。

import javafx.animation.TranslateTransition;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Shape;
import javafx.util.Duration;

public class AnxiousShapes {

    private Shape shape;
    private double delay ;
    private int howFarToMoveX;
    private int howFarToMoveY;

    public AnxiousShapes(int type, int x, int y, int size, double delay, Color color, int hftmX, int hftmY) {
        if (type == 0) {shape = new Circle(x, y, size, color);}
        //these are the only lines you can change
        //in this main class
        //else if (type == 1){shape = new Rectangle(x,y,color);}
        //else if (type == 2){shape = new Polygon();} 
        //else if (type == 3) {shape = new Circle(x, y, size, color);}
        //else { System.out.println("Error in type");shape = new  
        //Circle(???????);}
        this.delay = delay;
        this.howFarToMoveX = hftmX;
        this.howFarToMoveY = hftmY;
    }

    // getter and setters

    public TranslateTransition calculateTt() {
        TranslateTransition tt = new TranslateTransition(Duration.seconds(this.delay), this.shape); 
        tt.setToX(this.shape.getLayoutX() + howFarToMoveX);
        tt.setToY(shape.getLayoutY() + howFarToMoveY);  
        // Let the animation run forever -- if the shape
        // tries to move "off-screen" it will return to the beginning
        tt.setCycleCount(TranslateTransition.INDEFINITE);
        return tt;
    }

    @Override
    public String toString() {
        return "AnxiousShape [shape=" + shape + ", delay=" + delay + ", howFarToMoveX=" + howFarToMoveX
                + ", howFarToMoveY=" + howFarToMoveY + "]";
    }
}


import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.Group;
import javafx.scene.Node;

import java.util.Random;

public class AnxiousShapesTester extends Application {

    @Override
    public void start(Stage stage) {

        // adding the new things
        Group root = new Group();

        stage.setTitle("Welcome to JavaFX!");

        // create the shape circle 
        AnxiousShapes circle1 = new AnxiousShapes(0, 200, 200, 50, 15, 
        Color.GREEN, 10 ,35);
        root.getChildren().add(circle1.getShape());

//      this does not work
//      TranslateTransition trans = circle1.calculateTt();
//      trans.setNode(root);
//      trans.play();
//      and I tried this and I already have the movement in constructor for 
//      delay and x and y but TranslateTransition 
//      asks for duration.millis(500)
        TranslateTransition tt = new 
        TranslateTransition(Duration.millis(500), root);
        tt.play();

        Scene scene = new Scene(root, 600, 600, Color.WHITE);
        stage.setScene(scene);
        stage.show();
    }

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

    }

}

1 个答案:

答案 0 :(得分:1)

在评论中,您说“这不起作用”。问题是trans.setNode(root),它试图使root“此TranslateTransition的目标节点。” calculateTt()的实现已将this.shape指定为目标节点。而是在AnxiousShapes中添加合适的访问器,并按构造使用转换。以下更改如下所示:

public Shape getShape() { return this.shape; }
…
AnxiousShapes circle1 = new AnxiousShapes(0, 100, 100, 100, 3, Color.GREEN, 400, 400);
root.getChildren().add(circle1.getShape());
TranslateTransition trans = circle1.calculateTt();
trans.play();

image