简体   繁体   English

使用箭头键移动圆-Java

[英]Move a circle using arrow keys - Java

I'm experimenting with JavaFX right now teaching myself how to move text and items using the arrow keys. 我现在正在尝试JavaFX,教自己如何使用箭头键移动文本和项目。 I made a program that simply moves text around the stage if an arrow key is pressed down. 我编写了一个程序,如果按下箭头键,该程序仅在舞台上移动文本。

I would like to make a circle move around my pane instead of text. 我想绕着窗格而不是文字绕圈移动。 What changes must I make to move my circle using the arrow keys? 要使用箭头键移动圆圈,必须进行哪些更改?

public void start(Stage primaryStage) {
        Pane pane = new Pane();

        int dx = 50;
        int dy = 50;
        int radius = 125;

        Circle circle = new Circle(radius,dx,dy);
        Text text = new Text(20,20,"HI");
        circle.setFill(Color.WHITE);
        circle.setStroke(Color.BLACK);

        pane.getChildren().addAll(circle, text);

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

        Scene scene = new Scene(pane, 250, 200);
        primaryStage.setTitle("Arrow Keys");
        primaryStage.setScene(scene);
        primaryStage.show();

        text.requestFocus();
    }
scene.setOnKeyPressed(e -> {
    switch (e.getCode()) {
    case DOWN:
        circle.setCenterY(circle.getCenterY() + 10);
        break;
    case UP:
        circle.setCenterY(circle.getCenterY() - 10);
        break;
    case LEFT:
        circle.setCenterX(circle.getCenterX() - 10);
        break;
    case RIGHT:
        circle.setCenterX(circle.getCenterX() + 10);
        break;
    }
});

Personally, I would change where you are listening for the key press. 就个人而言,我将更改您在哪里聆听按键。 I would put it on the Pane, because it is always in focus in this example. 我将其放在“窗格”上,因为在此示例中始终将其作为焦点。 Other than that, you may have to remove and readd the circle if you didnt want the last one to stay, I'm not at my computer so I cant confirm this, but resulting code would look like this. 除此之外,如果您不希望最后一个停留,则可能必须删除并阅读圆圈,我不在电脑旁,所以我无法确认这一点,但是结果代码如下所示。

public void start(Stage primaryStage) {
        Pane pane = new Pane();

        int dx = 50;
        int dy = 50;
        int radius = 125;

        Circle circle = new Circle(radius,dx,dy);
        Text text = new Text(20,20,"HI");
        circle.setFill(Color.WHITE);
        circle.setStroke(Color.BLACK);

        pane.getChildren().addAll(circle, text);

        pane.setOnKeyPressed(e -> {
            pane.getChildren().remove(circle);
            switch(e.getCode()) {
            case DOWN: circle.setCenterY(circle.getCenterY() + 10);
            break;
            case UP: circle.setCenterY(circle.getCenterY() - 10);
            break;
            case LEFT: circle.setCenterX(circle.getCenterX() + 10);
            break;
            case RIGHT: circle.setCenterX(circle.getCenterX() - 10);
            break;    
            }
            pane.getChildren().add(circle);
        });

        Scene scene = new Scene(pane, 250, 200);
        primaryStage.setTitle("Arrow Keys");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM