绑定功能只对初始值有效。

我试图使用 “绑定 “这个功能,但似乎并不奏效,我想要的是将 “RectangleFx”(从javafx.scene.shape.Box中延伸出来的)的位置绑定到一个Rectangle(这是一个对象)的位置上。我想把 “RectangleFx”(从javafx.scene.shape.Box延伸出来的)的位置绑定到一个Rectangle(这是一个对象)的位置上,这样如果Rectangle的位置坐标发生变化,屏幕上的盒子就会移动。问题是当我使用’绑定’时,它只给RectangleFX提供了位置的初始值,而没有在我改变Rectangle的位置时更新它。”Rectangle “类是我自己写的,它不是javafx.scene.shape.Rectangle。

    public class RectangleFX extends javafx.scene.shape.Box {
        private Rectangle rectangle;

        public RectangleFX(Rectangle r) {
            super(r.getWidth(), r.getHeight(), 30);
            this.rectangle = r;
            this.translateXProperty().bind(r.getCenter().propertyX());
            this.translateYProperty().bind(r.getCenter().propertyY());
       }

       public Rectangle getRect() {
           return this.rectangle;
        }

    } 

public class Rectangle extends Body {
    Vec2 min;
    Vec2 max;

    public Rectangle(Vec2 pos, Vec2 spd, double mass, double density, double rest, Vec2 vel, Vec2 a, Vec2 b) {
        super(pos, spd, vel, mass, density, rest);
        this.min = a;
        this.max = b;
    }

    public Rectangle(Vec2 pos, double mass, double rest, Vec2 a, Vec2 b) {
        super(pos, mass * (b.getX() - a.getX()) * (b.getY() * a.getY()), mass, rest);
        this.min = a;
        this.max = b;
    }

    public double get_x_extent() {
        return this.max.getX() - this.min.getX();
    }

    public double get_y_extent() {
        return this.max.getY() - this.min.getY();
    }

    @Override
    public double get_min_x() {
        return this.min.getX();
    }

    @Override
    public double get_min_y() {
        return this.min.getY();
    }

    @Override
    public double get_max_x() {
        return this.max.getX();
    }

    @Override
    public double get_max_y() {
        return this.max.getY();
    }

    public double getWidth() {
        return Math.abs(this.get_max_x() - this.get_min_x());
    }

    public double getHeight() {
        return Math.abs(this.get_max_y() - this.get_min_y());
    }

    public Vec2 getCenter() {
        return new Vec2(1 / 2 * this.getWidth() + this.getPosition().getX(), 1 / 2 * this.getHeight() + this.getPosition().getY());
    }

    boolean contact(Rectangle a, Rectangle b) {
        if (a.max.getX() < b.min.getX() || a.min.getX() > b.max.getX())
            return false;
        if (a.max.getY() < b.min.getY() || a.min.getY() > b.max.getY())
            return false;

        return true;
    }
}

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;

public class Vec2 {
    private DoubleProperty x = new SimpleDoubleProperty();
    private DoubleProperty y = new SimpleDoubleProperty();

    public double getX() {
        return this.x.get();
    }

    public double getY() {
        return this.y.get();
    }

    public void setX(double value) {
        this.x.set(value);
    }

    public void setY(double value) {
        this.y.set(value);
    }

    public DoubleProperty propertyX() {
        return this.x;
    }

    public DoubleProperty propertyY() {
        return this.y;
    }

    public Vec2(double x, double y) {
        this.setX(x);
        this.setY(y);
    }

    public static final Vec2 null_Vector = new Vec2(0, 0);
    public static final Vec2 x_Vector = new Vec2(1, 0);
    public static final Vec2 y_Vector = new Vec2(0, 1);

    public static Vec2 addition(Vec2 v1, Vec2 v2) {
        return new Vec2(v1.getX() + v2.getX(), v1.getY() + v2.getY());
    }

    public void increase_by(Vec2 v) {
        this.setX(this.getX() + v.getX());
        this.setY(this.getY() + v.getY());
    }

    public static Vec2 substraction(Vec2 v1, Vec2 v2) {
        return new Vec2(v1.getX() - v2.getX(), v1.getY() - v2.getY());
    }

    public void decrease_by(Vec2 v) {
        this.setX(this.getX() - v.getX());
        this.setY(this.getY() - v.getY());
    }

    public static Vec2 product(Vec2 v1, Vec2 v2) {
        return new Vec2(v1.getX() * v2.getX(), v1.getY() * v2.getY());
    }

    public static Vec2 product(double d, Vec2 v) {
        return new Vec2(d * v.getX(), d * v.getY());
    }

    public void multiply_by(Vec2 v) {
        this.setX(this.getX() * v.getX());
        this.setY(this.getY() * v.getY());
    }

    public void multiply_by(double d) {
        this.setX(this.getX() * d);
        this.setY(this.getY() * d);
    }

    public static Vec2 division(Vec2 v1, Vec2 v2) {
        return new Vec2(v1.getX() / v2.getX(), v1.getY() / v2.getY());
    }

    public static Vec2 division(double d, Vec2 v) {
        if (d == 0)
            throw new ArithmeticException();

        return new Vec2(v.getX() / d, v.getY() / d);
    }

    public static Vec2 opposite(Vec2 v) {
        return new Vec2(-v.getX(), -v.getY());
    }

    public static Vec2 invert(Vec2 v) {
        return new Vec2(1 / v.getX(), 1 / v.getY());
    }

    public double length() {
        return Math.sqrt(this.length_Squared());
    }

    public double length_Squared() {
        return (dot_Product(this, this));
    }

    public Vec2 copy() {
        return new Vec2(this.getX(), this.getY());
    }

    public static boolean compare(Vec2 v1, Vec2 v2) {
        if (v1 == null) {
            if (v2 == null)
                return true;
            return false;
        } else if (v2 == null)
            return false;

        if (v1.getX() != v2.getX())
            return false;

        if (v1.getY() != v2.getY())
            return false;

        return true;
    }

    public static double dot_Product(Vec2 v1, Vec2 v2) {
        return (v1.getX() * v2.getX() + v1.getY() * v2.getY());
    }

    public static double cross_Product(Vec2 v1, Vec2 v2) {
        return (v1.getX() * v2.getY() - v1.getY() * v2.getX());
    }

    public static Vec2 cross_Product(Vec2 v, double d) {
        return new Vec2(d * v.getY(), -d * v.getX());
    }

    public static Vec2 cross_Product(double d, Vec2 v) {
        return new Vec2(-d * v.getY(), d * v.getX());
    }

    public static boolean isNull(Vec2 v) {
        if (v.getX() != 0)
            return false;

        if (v.getY() != 0)
            return false;

        return true;
    }

    public void normalize() {
        double d = length_Squared();

        this.setX(this.getX() / d);
        this.setY(this.getY() / d);
    }

    public Vec2 projection_on(Vec2 v) {
        return Vec2.substraction(this, Vec2.product(Vec2.dot_Product(this, v), this));
    }

    public Vec2 symetric_on(Vec2 v) {
        return Vec2.substraction(Vec2.product(2, this.projection_on(v)), this);
    }
}

下面是一个代码不工作的例子,当我们按NUMPAD0时,立方体应该在屏幕上移动,因为它所绑定的矩形中心的坐标发生了变化。

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;

public class Main extends Application {
  private final double HEIGHT = 800;
  private final double WIDTH = 1400;

  @Override
  public void start(Stage primaryStage) throws Exception {
    Vec2 posb = new Vec2(1000, 700);
    Vec2 minb = new Vec2(30, 0);
    Vec2 maxb = new Vec2(0, 30);

    Rectangle r = new Rectangle(posb, 30, 10, minb, maxb);
    RectangleFX b = new RectangleFX(r);


    Group boxG = new Group();
    boxG.getChildren().add(b);


    Group root = new Group(boxG);

    Camera cam = new PerspectiveCamera();
    Scene scene = new Scene(root, WIDTH, HEIGHT);
    scene.setFill(Color.BLACK);
    scene.setCamera(cam);

    primaryStage.setTitle("Test position");
    primaryStage.setScene(scene);
    primaryStage.show();

    primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
      switch (event.getCode()) {
        case NUMPAD0:
          r.getPosition().setX(r.getPosition().getX() + 10);
          break;
        default:
          break;
      }
    });

    System.out.println("test");
  }

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

解决方案:

我找到了问题所在。我发现问题了,我只是绑定了错误的值……

给TA打赏
共{{data.count}}人
人已打赏
解决方案

如何根据内存位置对集合中的对象进行排序?

2022-5-11 12:00:14

解决方案

从返回SQL结果的存储函数过程中生成JSON?

2022-5-11 12:00:16

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索