使用翻译转换和JavaFX的Java OOP

问题描述 投票:0回答:1

该翻译转换不输出。它使用的主类的方法。我认为这是不工作,因为它被用作一个对象。必须有一个不同的代码来实现。它使用的主要方法,然后将其放入一个测试仪。不过,我不知道如何,因为它使用一个构造函数/对象以及使用它。然后,对象轮流和变化成我改变了它的节点。 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);

    }

}
oop javafx transition translate
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.