Функция привязки работает только для начального значения - PullRequest
0 голосов
/ 23 апреля 2020

Я пытаюсь использовать функцию 'bind', но, похоже, она не работает. То, что я хочу, это связать позицию 'RectangleFx' (которая простирается от javafx.scene.shape.Box) с позицией Rectangle (которая является объектом), так что если координаты положения прямоугольника изменятся, на экране коробка будет двигаться. Проблема в том, что когда я использую 'bind', он только дает RectangleFX начальное значение positione и не обновляет его, когда я изменяю значение 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);
  }
}

1 Ответ

0 голосов
/ 23 апреля 2020

Я нашел проблему. Я просто связывал неправильные значения ...

...