简体   繁体   English

从类对象获取窗格的宽度和高度时出现问题-Javafx

[英]Problems getting the width and height of a pane from a class object - Javafx

Ok so I understand the binding principle on resizing objects in a pane as it is being resized, this problem is a bit different. 好的,所以我了解在调整窗格中对象大小时的绑定原理,这个问题有点不同。 I am confused, I am trying to create a class that extends Pane that creates in my main a line that has it's startX and startY coordinates bound to the center of a Pane. 我很困惑,我试图创建一个扩展Pane的类,该类在我的主行中创建一行,该行的startX和startY坐标绑定到Pane的中心。 The problem is that when using getWidth() / 2 or getHeight() /2 the coordinate are being placed somewhere up and to the left of the starting coordinates (0, 0) when I press my arrow key's which when pressed creates another line that is drawn in the given direction pressed and starting from the end of the last line drawn. 问题是,当使用getWidth()/ 2或getHeight()/ 2时,当我按箭头键时,该坐标被放置在起始坐标(0,0)的左上方,当按下时会创建另一行从给定的方向开始绘制,并从最后绘制的行的末尾开始。

Like I said when I use getWidth() / 2 and getHeight / 2 as the startX and startY coordinates of my new line, in return the line gets placed in a negative coordinate, placing it off the screen above and to the left of the starting (0, 0) coordinates of the pane. 就像我说的那样,当我使用getWidth()/ 2和getHeight / 2作为新行的startX和startY坐标时,作为回报,该行被放置在一个负坐标中,将其放置在屏幕上方和左边的左边窗格的(0,0)坐标。

Below is part of my code which contains the default constructor which I am having the problem with, on the non-default constructor I give the ability to manually enter the starting coordinates and when I do this, the line is placed exactly where I want it. 以下是我的代码的一部分,其中包含我遇到问题的默认构造函数,在非默认构造函数上,我可以手动输入起始坐标,并且在执行此操作时,该行将精确地放置在所需的位置。

public class LineDrawingObject extends Pane {
    // ArrayList to store the Line Object's
    ArrayList<Line> lines = new ArrayList<>();
    Line line;
    private Color lineColor;
    private double lineLength;
    private int lineCount = 0;
    private double startX;
    private double startY;
    private double endX;
    private double endY;

    /** Default Constructor */
    public LineDrawingObject() {
        this.lineLength = 20;
        line = new Line(this.getWidth() / 2, this.getHeight() / 2,
            (this.getWidth() / 2), (this.getHeight() / 2) - this.lineLength);
        this.lineColor = Color.BLACK;
        line.setStroke(this.lineColor);
        this.lineCount++;
        this.lines.add(line);
        getChildren().add(line);
    }

Edit: Figured I might need to add more information 编辑:弄清楚我可能需要添加更多信息

also I wanted to add that my pane size is set in new Scene(pane, 250, 250) so the center coordinates would be (125, 125)....Would using the getWidth and getHeight methods on the pane return an invalid size if it hasn't yet been drawn? 我还想补充一点,我的窗格大小是在新的Scene(pane,250,250)中设置的,所以中心坐标将是(125,125)....将在窗格上使用getWidth和getHeight方法将返回无效的大小如果尚未绘制? I tried setting the preferred size within my start method but it didn't seem to work. 我尝试在启动方法中设置首选大小,但似乎没有用。 If that is the case how would I remedy this problem? 如果是这样,我该如何解决这个问题?

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 * Created by John on 7/24/2014.
 */
public class DrawLines extends Application {
    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
        // Create a pane
        Pane pane = new Pane();

        // Create object to draw lines upon KeyEvent
        LineDrawingObject lineDrawingObject = new LineDrawingObject(20, Color.BLACK,
            pane.getWidth() / 2, pane.getWidth() / 2);
        pane.getChildren().add(lineDrawingObject);
        lineDrawingObject.setOnKeyPressed(e -> {
            lineDrawingObject.paintLine(e.getCode());
        });

        // Create a scene and place it in the pane
        Scene scene = new Scene(pane, 250, 250);
        primaryStage.setTitle("DrawLines"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage

        // Allow object to receive key input
        lineDrawingObject.requestFocus();
    }
}

and here is the LineDrawing Object: 这是LineDrawing对象:

import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;

import java.util.ArrayList;

/** This object will draw lines inside of a Pane when an arrow key is
 *  pressed and will draw it in that direction from the current line */
public class LineDrawingObject extends Pane {
    // ArrayList to store the Line Object's
    ArrayList<Line> lines = new ArrayList<>();
    Line line;
    private Color lineColor;
    private double lineLength;
    private int lineCount = 0;
    private double startX;
    private double startY;
    private double endX;
    private double endY;

    /** Default Constructor */
    public LineDrawingObject() {
        this.lineLength = 20;
        line = new Line(this.getWidth() / 2, this.getHeight() / 2,
            (this.getWidth() / 2), (this.getHeight() / 2) - this.lineLength);
        this.lineColor = Color.BLACK;
        line.setStroke(this.lineColor);
        this.lineCount++;
        this.lines.add(line);
        getChildren().add(line);
    }

    /** Secondary Constructor, allows you to control the line length and color */
    public LineDrawingObject(double lineLength, Color lineColor, double startX, double startY) {
        this.lineLength = lineLength;
        line = new Line(startX, startY,
                startX, startY - this.lineLength);
        this.lineColor = lineColor;
        line.setStroke(this.lineColor);
        this.lineCount++;
        this.lines.add(line);
        getChildren().add(line);
    }

    public ArrayList<Line> getLines() {
        return lines;
    }

    public void setLines(ArrayList<Line> lines) {
        this.lines = lines;
    }

    public Line getLine() {
        return this.line;
    }

    public void setLine(Line line) {
        this.line = line;
    }

    public Color getLineColor() {
        return this.lineColor;
    }

    public void setLineColor(Color lineColor) {
        this.lineColor = lineColor;
    }

    public double getLineLength() {
        return this.lineLength;
    }

    public void setLineLength(double lineLength) {
        this.lineLength = lineLength;
    }

    public int getLineCount() {
        return this.lineCount;
    }

    public void setLineCount(int lineCount) {
        this.lineCount = lineCount;
    }

    public double getStartX() {
        return this.startX;
    }

    public void setStartX(double startX) {
        this.startX = startX;
    }

    public double getStartY() {
        return this.startY;
    }

    public void setStartY(double startY) {
        this.startY = startY;
    }

    public double getEndX() {
        return this.endX;
    }

    public void setEndX(double endX) {
        this.endX = endX;
    }

    public double getEndY() {
        return this.endY;
    }

    public void setEndY(double endY) {
        this.endY = endY;
    }

    public void paintLine(KeyCode keyCode) {
        // Set line start coordinates to the end of the last line
        setStartX(line.getEndX());
        setStartY(line.getEndY());

        // Set line end coordinates
        switch (keyCode) {
            case UP: goUp(); break;
            case LEFT: goLeft(); break;
            case DOWN: goDown(); break;
            case RIGHT: goRight(); break;
        }

        // Create line
        line = new Line(getStartX(), getStartY(), getEndX(), getEndY());
        line.setStroke(lineColor);
        this.lines.add(line);
        getChildren().add(line);
    }

    public void goLeft() {
        setEndX(getStartX() - this.lineLength);
        setEndY(getStartY());
    }

    public void goRight() {
        setEndX(getStartX() + this.lineLength);
        setEndY(getStartY());
    }

    public void goUp() {
        setEndX(getStartX());
        setEndY(getStartY() - this.lineLength);
    }

    public void goDown() {
        setEndX(getStartX());
        setEndY(getStartY() + this.lineLength);
    }
}

Using Default Constructor 使用默认构造函数 使用默认构造函数

Using Custom Coordinates 使用自定义坐标 使用自定义坐标

You are invoking getWidth() and getHeight() in the constructor, which is necessarily executed before the Pane is added to any live scene. 您正在构造函数中调用getWidth()getHeight() ,这必须在将Pane添加到任何实时场景之前执行。 Hence these are going to return 0 (because the pane hasn't been laid out yet). 因此,这些将返回0(因为尚未布置窗格)。

Instead, bind the coordinates of the line to values based on the widthProperty and heightProperty : 而是将线的坐标绑定到基于widthPropertyheightProperty

line = new Line();
line.startXProperty().bind(widthProperty().divide(2));
line.startYProperty().bind(heightProperty().divide(2));
line.endXProperty().bind(widthProperty().divide(2));
line.endYProperty().bind(heightProperty().divide(2).subtract(lineLength));

To remedy this problem I decided to just do it a simpler way and have a preset size attribute set for the constructor and additional constructors for allowing you to set the size yourself. 为了解决这个问题,我决定以一种更简单的方式进行操作,并为构造函数设置一个预设的size属性,并允许您自己设置大小的其他构造函数。

在此处输入图片说明

import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;

import java.util.ArrayList;

/** This object will draw lines inside of a Pane when an arrow key is
 *  pressed and will draw it in that direction from the current line */
public class LineDrawingObject extends Pane {
    // ArrayList to store the Line Object's
    ArrayList<Line> lines = new ArrayList<>();
    Line line;
    private Color lineColor;
    private double lineLength;
    private double startX;
    private double startY;
    private double endX;
    private double endY;

    /** Default Constructor */
    public LineDrawingObject() {
        // Set a default size for this Pane
        this.setWidth(350);
        this.setHeight(350);
        // Set Line properties
        this.startX = getWidth() / 2;
        this.startY = getHeight() / 2;
        this.lineLength = 20;
        this.lineColor = Color.BLACK;
        // Create line and set it's Stroke
        line = new Line(this.startX, this.startY,
                this.startX, this.startY - this.lineLength);
        line.setStroke(this.lineColor);
        // Add line to ArrayList and Pane
        lines.add(line);
        getChildren().add(line);
    }

    /** Second Constructor, allows you to control the line length, color and center it */
    public LineDrawingObject(double lineLength, Color lineColor) {
        // Set a default size for this Pane
        this.setWidth(350);
        this.setHeight(350);
        // Set line properties
        this.startX = getWidth() / 2;
        this.startY = getHeight() / 2;
        this.lineLength = lineLength;
        this.lineColor = lineColor;
        // Create line and set it's Stroke
        line = new Line(this.startX, this.startY,
                this.startX, this.startY - this.lineLength);
        line.setStroke(this.lineColor);
        // Add line to ArrayList and Pane
        lines.add(line);
        getChildren().add(line);
    }

    /** Third Constructor, allows you to control the line length, color, and pane size */
    public LineDrawingObject(double lineLength, Color lineColor,
                             double paneWidth, double paneHeight) {
        // Set a default size for this Pane
        this.setWidth(paneWidth);
        this.setHeight(paneHeight);
        // Set line properties
        this.startX = getWidth() / 2;
        this.startY = getHeight() / 2;
        this.lineLength = lineLength;
        this.lineColor = lineColor;
        // Create line and set it's Stroke
        line = new Line(this.startX, this.startY,
                this.startX, this.startY - this.lineLength);
        line.setStroke(this.lineColor);
        // Add line to ArrayList and Pane
        lines.add(line);
        getChildren().add(line);
    }

    /** Third Constructor, allows you to control the line length and color, startX, and startY */
    public LineDrawingObject(double lineLength, double startX, double startY, Color lineColor) {
        // Set line properties
        this.startX = startX;
        this.startY = startY;
        this.lineLength = lineLength;
        this.lineColor = lineColor;
        // Create line and set it's Stroke
        line = new Line(this.startX, this.startY,
                this.startX, this.startY - this.lineLength);
        line.setStroke(this.lineColor);
        // Add line to ArrayList and Pane
        lines.add(line);
        getChildren().add(line);
    }

    /** Get the list of lines */
    public ArrayList<Line> getLines() {
        return lines;
    }

    /** Get the current line object */
    public Line getLine() {
        return this.line;
    }

    /** Manually set the current line object */
    public void setLine(Line line) {
        this.line = line;
    }

    /** Get the current line object's color */
    public Color getLineColor() {
        return this.lineColor;
    }

    /** Set the current line object's color */
    public void setLineColor(Color lineColor) {
        this.lineColor = lineColor;
    }

    /** Get length of the lines */
    public double getLineLength() {
        return this.lineLength;
    }

    /** Set a new length for the lines */
    public void setLineLength(double lineLength) {
        this.lineLength = lineLength;
    }

    /** Get the count of the number of line's currently in existence */
    public int getLineCount() {
        return this.lines.size();
    }

    /** Get the startX coordinates */
    public double getStartX() {
        return this.startX;
    }

    /** Set the startX coordinates */
    public void setStartX(double startX) {
        this.startX = startX;
    }

    /** Get the startY coordinates */
    public double getStartY() {
        return this.startY;
    }

    /** Set the startY coordinates */
    public void setStartY(double startY) {
        this.startY = startY;
    }

    /** Get the endX coordinates */
    public double getEndX() {
        return this.endX;
    }

    /** Set the endX coordinates */
    public void setEndX(double endX) {
        this.endX = endX;
    }

    /** Get the endY coordinates */
    public double getEndY() {
        return this.endY;
    }

    /** Set the endY coordinates */
    public void setEndY(double endY) {
        this.endY = endY;
    }

    /** Paint the next line based on the key pressed */
    public void paintLine(KeyCode keyCode) {
        // Set line start coordinates to the end of the last line
        setStartX(line.getEndX());
        setStartY(line.getEndY());

        // Set line end coordinates
        switch (keyCode) {
            case UP: goUp(); break;
            case LEFT: goLeft(); break;
            case DOWN: goDown(); break;
            case RIGHT: goRight(); break;
        }

        // Create line
        line = new Line(getStartX(), getStartY(), getEndX(), getEndY());
        line.setStroke(lineColor);
        this.lines.add(line);
        getChildren().add(line);
    }

    /** Set the end coordinates to the left of the last line */
    public void goLeft() {
        setEndX(getStartX() - this.lineLength);
        setEndY(getStartY());
    }

    /** Set the end coordinates to the right of the last line */
    public void goRight() {
        setEndX(getStartX() + this.lineLength);
        setEndY(getStartY());
    }

    /** Set the end coordinates above the last line */
    public void goUp() {
        setEndX(getStartX());
        setEndY(getStartY() - this.lineLength);
    }

    /** Set the end coordinates below the last line */
    public void goDown() {
        setEndX(getStartX());
        setEndY(getStartY() + this.lineLength);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM