好的,所以我了解在调整窗格大小时调整窗格中对象大小的绑定原则,这个问题有点不同。我很困惑,我正在尝试创建一个扩展 Pane 的类,该类在我的 main 中创建一条线,它的 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);
}
编辑:想我可能需要添加更多信息
我还想补充一点,我的窗格大小是在 new 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);
}
}
使用默认构造函数
使用自定义坐标