現在我所得到的都是點,但沒有連接它們的線...... 我應該重新創建在切換之前初始化的原始「線」嗎?或者我應該不是要創建新的線條,而是重新修改原始線條?需要幫助繪製使用箭頭鍵的線段
public class DrawLineWithArrowKeys extends Application {
double bX = 150;
double bY = 150;
double eX = 150;
double eY = 150;
double segment = 20;
@Override
public void start(Stage primaryStage) {
Pane myPane = new Pane();
Line line = new Line(bX,bY,eX,eY);
line.setStrokeWidth(2);
line.setStroke(Color.RED);
myPane.getChildren().add(line);
line.setOnKeyPressed(e -> {
if (e.getCode().isArrowKey())
{
switch (e.getCode()) {
case DOWN:
bY += segment;
eY = bY;
Line line2 = new Line(bX,bY,eX,eY);
line2.setStrokeWidth(2);
line2.setStroke(Color.GREEN);
myPane.getChildren().add(line2);
break;
case UP:
bY -= segment;
eY = bY;
Line line3 = new Line(bX,bY,eX,eY);
line3.setStrokeWidth(2);
line3.setStroke(Color.GREEN);
myPane.getChildren().add(line3);
break;
case LEFT:
bX -= segment;
eX = bX;
Line line4 = new Line(bX,bY, eX, eY);
line4.setStrokeWidth(2);
line4.setStroke(Color.GREEN);
myPane.getChildren().add(line4);
break;
case RIGHT:
bX += segment;
eX = bX;
Line line5 = new Line(bX,bY,eX,eY);
line5.setStrokeWidth(2);
line5.setStroke(Color.GREEN);
myPane.getChildren().add(line5);
break;
default:
break;
}
}
});
Scene scene = new Scene(myPane, 300, 300);
primaryStage.setTitle("Draw Line with Arrow Keys");
primaryStage.setScene(scene);
primaryStage.show();
line.requestFocus();
}
@Uluk BIY,上述修改後的代碼。 – FlyingFinger1125