簡體   English   中英

從類對象獲取窗格的寬度和高度時出現問題-Javafx

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

好的,所以我了解在調整窗格中對象大小時的綁定原理,這個問題有點不同。 我很困惑,我試圖創建一個擴展Pane的類,該類在我的主行中創建一行,該行的startX和startY坐標綁定到Pane的中心。 問題是,當使用getWidth()/ 2或getHeight()/ 2時,當我按箭頭鍵時,該坐標被放置在起始坐標(0,0)的左上方,當按下時會創建另一行從給定的方向開始繪制,並從最后繪制的行的末尾開始。

就像我說的那樣,當我使用getWidth()/ 2和getHeight / 2作為新行的startX和startY坐標時,作為回報,該行被放置在一個負坐標中,將其放置在屏幕上方和左邊的左邊窗格的(0,0)坐標。

以下是我的代碼的一部分,其中包含我遇到問題的默認構造函數,在非默認構造函數上,我可以手動輸入起始坐標,並且在執行此操作時,該行將精確地放置在所需的位置。

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

編輯:弄清楚我可能需要添加更多信息

我還想補充一點,我的窗格大小是在新的Scene(pane,250,250)中設置的,所以中心坐標將是(125,125)....將在窗格上使用getWidth和getHeight方法將返回無效的大小如果尚未繪制? 我嘗試在啟動方法中設置首選大小,但似乎沒有用。 如果是這樣,我該如何解決這個問題?

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

這是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);
    }
}

使用默認構造函數 使用默認構造函數

使用自定義坐標 使用自定義坐標

您正在構造函數中調用getWidth()getHeight() ,這必須在將Pane添加到任何實時場景之前執行。 因此,這些將返回0(因為尚未布置窗格)。

而是將線的坐標綁定到基於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));

為了解決這個問題,我決定以一種更簡單的方式進行操作,並為構造函數設置一個預設的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