わかりましたので、サイズ変更されているペイン内のオブジェクトのサイズ変更に関するバインディングの原則を理解しています。この問題は少し異なります。混乱しています。ペインの中心にバインドされた startX 座標と startY 座標を持つラインをメインに作成するペインを拡張するクラスを作成しようとしています。問題は、 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 メソッドを使用すると、無効なサイズが返されますまだ描かれていない場合は?start メソッド内で優先サイズを設定しようとしましたが、うまくいかないようでした。その場合、どうすればこの問題を解決できますか?
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);
}
}
デフォルトのコンストラクターの使用
カスタム座標の使用