我试图通过创建他们已去过的路径来跟踪用户的移动。我已经找到了该程序的大多数方面,但我仍然坚持如何在窗格上保持用户移动。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class DrawLines extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Text text = new Text(150, 150, ".");
pane.getChildren().add(text);
text.setOnKeyPressed(e -> {
switch (e.getCode()) {
case DOWN: text.setY(text.getY() + 10); break;
case UP: text.setY(text.getY() - 10); break;
case LEFT: text.setX(text.getX() - 10); break;
case RIGHT: text.setX(text.getX() + 10); break;
default:
if (Character.isLetterOrDigit(e.getText().charAt(0)))
text.setText(e.getText());
}
});
// Create a scene and place the pane in the stage
Scene scene = new Scene(pane, 300, 300);
primaryStage.setTitle("Draw a Line!");
primaryStage.setScene(scene);
primaryStage.show();
text.requestFocus();
}
答案 0 :(得分:1)
您需要创建一个新的Text
并将其添加到窗格中以进行每次用户移动。只需创建一个新方法来处理这个问题,并为每个用户移动调用它。
private void leaveTrail(Pane pane, Text text){
Text newText = new Text(text.getText());
pane.getChildren().add(newText);
newText.setY(text.getY());
newText.setX(text.getX());
}
一个完整的工作示例:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class DrawLines extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Text text = new Text(150, 150, ".");
pane.getChildren().add(text);
text.setOnKeyPressed(e -> {
switch (e.getCode()) {
case DOWN:
leaveTrail(pane, text);
text.setY(text.getY() + 10);
break;
case UP:
leaveTrail(pane, text);
text.setY(text.getY() - 10);
break;
case LEFT:
leaveTrail(pane, text);
text.setX(text.getX() - 10);
break;
case RIGHT:
leaveTrail(pane, text);
text.setX(text.getX() + 10);
break;
default:
if (Character.isLetterOrDigit(e.getText().charAt(0)))
text.setText(e.getText());
}
});
// Create a scene and place the pane in the stage
Scene scene = new Scene(pane, 300, 300);
primaryStage.setTitle("Draw a Line!");
primaryStage.setScene(scene);
primaryStage.show();
text.requestFocus();
}
private void leaveTrail(Pane pane, Text text){
Text newText = new Text(text.getText());
pane.getChildren().add(newText);
newText.setY(text.getY());
newText.setX(text.getX());
}
public static void main(String[] args) {
launch(args);
}
}
示例图片