将场景大小属性绑定到Stage JavaFX

时间:2014-01-20 08:40:10

标签: user-interface window javafx javafx-2

我需要将Scene的大小调整为Stage/Window的当前大小。

目前,当我调整Stage/Window Scene调整大小时,也不会调整大小。 Scene包含StackPane

我找了一种方法来绑定但没有找到任何东西,场景的大小参数的setter似乎也不可用。有没有人知道如何完成这项工作?

编辑:这是我的班级代码atm。

public class SimpleSun extends Application {

// Controls

Label lblPos = new Label();
Stage primaryStage;
List<Visibility> vislist;
Scene scene;
Rectangle rect;
Circle circle;
Line line1;
Line line2;

// Variables
private double sunDiameter = 700;
private Integer center = 400;
int fovrad = 80;
int fovpxl = calculatePixelsFromRad(fovrad);

/**
 * @param args
 *            the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

@SuppressWarnings("static-access")
@Override
public void start(final Stage stage) {
    // Initialize Containers
    this.primaryStage = stage;
    primaryStage.setTitle("0.1-alpha -- Sun Simulation");
    BorderPane root = new BorderPane();
    scene = new Scene(root, 800, 800);
    StackPane sp = new StackPane();

    // Paint Graphics
    rect = new Rectangle(center - fovpxl / 2, center - fovpxl / 2, fovpxl,
            fovpxl);
    circle = new Circle(400, 400, 350);
    line1 = new Line(0, 0, 0, scene.getHeight());
    line2 = new Line(0, 0, scene.getHeight(), 0);

    // Insert Controls
    sp.getChildren().addAll(circle, line1, line2, lblPos, rect);
    sp.setAlignment(lblPos, Pos.TOP_LEFT);
    lblPos.setStyle("margin-top:10px");
    root.setCenter(sp);
    primaryStage.setScene(scene);

    // Binding Circle size to stage & Scene size to Stage
    circle.radiusProperty().bind(
            primaryStage.widthProperty().divide(2).subtract(50));
    circle.centerXProperty().bind(primaryStage.widthProperty().divide(2));
    circle.centerYProperty().bind(primaryStage.widthProperty().divide(2));



    // Assign Handlers
    line1.setOnMouseClicked(mouseHandler);
    line1.setOnMouseMoved(mouseHandler);
    line2.setOnMouseClicked(mouseHandler);
    line2.setOnMouseMoved(mouseHandler);
    circle.setOnMouseClicked(mouseHandler);
    circle.setOnMouseMoved(mouseHandler);
    rect.setOnMouseClicked(mouseHandler);
    rect.setOnMouseMoved(mouseHandler);
    scene.widthProperty().addListener(cListener);
    scene.heightProperty().addListener(cListener);

    // Properties
    circle.setFill(Color.YELLOW);
    circle.setStroke(Color.BLACK);
    rect.setFill(null);
    rect.setStroke(Color.BLACK);

    primaryStage.show();
}

EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() {

    public void handle(MouseEvent mouseEvent) {
        if (mouseEvent.getEventType() == MouseEvent.MOUSE_CLICKED) {
            int x = (int) mouseEvent.getSceneX();
            int y = (int) mouseEvent.getSceneY();

            DoubleMatrix1D pCoords = calculateXYRad(x, y);

            final ISource p = new GaussianRadialSource(pCoords.get(0),
                    pCoords.get(1), 1, 1, 0);

            MainSimulation.runSim(p, fovrad);

            // DEBUG
            // String message = "Source Position: " + mouseEvent.getSceneX()
            // + " / " + mouseEvent.getSceneY();
            // System.out.println(message);
        } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_MOVED) {
            int x = (int) mouseEvent.getSceneX();
            int y = (int) mouseEvent.getSceneY();
            if (mouseEvent.getSource().getClass().equals(Circle.class)) {
                ((Circle) mouseEvent.getSource())
                        .setCursor(Cursor.CROSSHAIR);

            } else if (mouseEvent.getSource().getClass().equals(Line.class)) {
                ((Line) mouseEvent.getSource()).setCursor(Cursor.CROSSHAIR);
            } else if (mouseEvent.getSource().getClass()
                    .equals(Rectangle.class)) {
                ((Rectangle) mouseEvent.getSource())
                        .setCursor(Cursor.CROSSHAIR);
            }
            DoubleMatrix1D pCoords = calculateXYRad(x, y);
            // Adjust label to new cursor position
            lblPos.setText((pCoords.get(0) + " :: " + pCoords.get(1)));
            // DEBUG
             System.out.println("x: " + pCoords.get(0) + ", y: "
             + pCoords.get(1));
        }
    }

};

ChangeListener<? super Number> cListener = new ChangeListener<Number>() {
    public void changed(ObservableValue<? extends Number> scenewidth,
            Number oldValue, Number newValue) {
        // System.out.println(primaryStage.getHeight());
        // Adjust Size of Window instead of binding
        if (primaryStage.getHeight() < primaryStage.getWidth()) {
            primaryStage.setWidth(primaryStage.getHeight());
            center = (int) (primaryStage.getHeight() / 2);
            sunDiameter = primaryStage.getHeight() - 100;
        }
        if (primaryStage.getWidth() < primaryStage.getHeight()) {
            primaryStage.setHeight(primaryStage.getWidth());
            center = (int) (primaryStage.getWidth() / 2);
            sunDiameter = primaryStage.getWidth() - 100;
        }
        // Adjust Center and Sun Diameter to new Window Size
        repaintSimpleSun();
        System.out.println(primaryStage.getWidth() + "  " + primaryStage.getHeight());
        System.out.println(scene.getWidth() + " " + scene.getHeight());
        System.out.println(center + " " + sunDiameter);
    }
};

private DoubleMatrix1D calculateXYRad(int x, int y) {
    double X = ((x - center) * (Units.SUN_HALF_DIAMETER_RAD * 2.0) / sunDiameter);
    double Y = ((y - center) * (Units.SUN_HALF_DIAMETER_RAD * 2.0) / sunDiameter);
    return DoubleFactory1D.dense.make(new double[] { X, Y });
}

private int calculatePixelsFromRad(int rad) {
    int pixels = (int) ((sunDiameter) / (Units.SUN_HALF_DIAMETER_RAD * 2) * rad);
    // System.out.println(pixels);
    return pixels;

}

private void repaintSimpleSun() {
    fovpxl = calculatePixelsFromRad(fovrad);
    rect.setHeight(fovpxl);
    rect.setWidth(fovpxl);
    rect.setX(center - (fovpxl / 2));
    rect.setY(center - (fovpxl / 2));
    line1.setEndY(primaryStage.getWidth());
    line2.setEndX(primaryStage.getHeight());
}
}

0 个答案:

没有答案