使用翻译转换和JavaFX的Java OOP [英] Java OOP using Translate Transition and JavaFX

查看:96
本文介绍了使用翻译转换和JavaFX的Java OOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The Translate Transition does not output. It uses a method in the main class. I believe it is not working because it is used as an object. There has to be a different code to implement. It uses a method in a main and then puts it into a tester. However, I do not know how to use it because it uses a constructor/object as well. Then, the object turns and changes into a node which I changed it. I do not know how the Translate Transition method is attached to the object and displays it into the javafx console. Please help solve the problem for positive feedback as it shows.

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

    }

}

推荐答案

在注释中,您说这不起作用".问题是trans.setNode(root),它试图使rootcalculateTt()的实现已将this.shape指定为目标节点.而是在AnxiousShapes中添加一个合适的访问器,并使用构造的转换.以下更改如下所示:

In a comment, you say, "this does not work." The problem is trans.setNode(root), which attempts to make root "The target node of this TranslateTransition." Your implementation of calculateTt() already specifies this.shape as the target node. Instead, add a suitable accessor to AnxiousShapes and use the transition as constructed; the following changes are illustrated below:

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

这篇关于使用翻译转换和JavaFX的Java OOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆