2015-10-17 31 views
2

我一直在试图制作一个程序来显示一个圆,并且可以使用按钮来移动它,但是我一直无法弄清楚如何告诉Java按下某个按钮时移动圆的方向。我试过setX和setY,但显然它们不是Circle的原生。这是我到目前为止:如何在JavaFX中移动形状?

import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.StackPane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.stage.Stage; 

public class Ball extends Application { 
    private Circle circle = new Circle(); 

    @Override 
    public void start(Stage primaryStage) { 
     HBox pane = new HBox(); 
     pane.setSpacing(10); 
     pane.setAlignment(Pos.CENTER); 
     Button btUp = new Button("UP"); 
     Button btDown = new Button("DOWN"); 
     Button btLeft = new Button("LEFT"); 
     Button btRight = new Button("RIGHT"); 

     pane.getChildren().add(btUp); 
     pane.getChildren().add(btDown); 
     pane.getChildren().add(btRight); 
     pane.getChildren().add(btLeft); 

     btUp.setOnAction(e -> circle.setY(circle.getY() - 10)); 
     btDown.setOnAction(e -> circle.setY(circle.getY() + 10)); 
     btLeft.setOnAction(e -> circle.setX(circle.getX() - 10)); 
     btRight.setOnAction(e -> circle.setX(circle.getX() + 10)); 

     BorderPane borderPane = new BorderPane(); 
     borderPane.setCenter(circle); 
     borderPane.setBottom(pane); 
     BorderPane.setAlignment(pane, Pos.CENTER); 

     Scene scene = new Scene(borderPane, 200, 200);  
     primaryStage.setTitle("Ball"); // Set the stage title 
     primaryStage.setScene(scene); // Place the scene in the stage 
     primaryStage.show(); 

     circle.requestFocus(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

考虑将[javafx]标记添加到标记列表中,并且更好地讨论javafx而不是java,因为有多种方法可能会误解这个问题。 –

+0

使用setCenterX(),setCenterY() – WillShackleford

+0

这里是[在JavaFX画布上移动形状]的解决方案(http://stackoverflow.com/questions/21396055/moving-shapes-in-javafx-canvas)。如果使用场景图(而不是Canvas),则使用场景图(如同在你的问题中,也是完全有效的方法),解决方案将略有不同(我目前没有一个现成的例子)。 – jewelsea

回答

2

您的代码的第一个问题是圆圈(或缺少它)。

除非您计划稍后定义它,否则不应使用无参数圆。一个解决方案是定义半径和在一行填充颜色:

private Circle circle = new Circle(50.0f, Color.RED); 

至于移动的圈子,你需要跟踪不断变化的圆圈位置,从而与您的实例变量补充一点:

private double newY, newX = 0; 

我会告诉你如何设置向下按钮(Left,Right和Up的代码非常类似于向下)。您setOnAction方法更改为:

btnDown.setOnAction(btnDownActionEvent); 

然后主要方法后定义btnDownActionEvent(或任何你想要的):

EventHandler<ActionEvent> btnDownActionEvent = new EventHandler<ActionEvent>() 
{ 
    @Override 
    public void handle(ActionEvent event) 
    { 
     System.out.println("Pressed Down"); 
     newY = circle.getCenterY() + 10 + newY; 

     circle.setTranslateX(newX); 
     circle.setTranslateY(newY); 
    } 
}; 

此外,您还可以删除circle.requestFocus();

0

创建两个变量:一个用于X坐标和Y坐标。

int newY, newX = 0;

现在使用关于场景setOnKeyPressed当按下箭头键来移动圆圈,使用IF逻辑。

scene.setOnKeyPressed(e ->{ 
    if(e.getCode() == KeyCode.RIGHT){ 
     newX = newX + 10; 
     circle.setTranslateX(newX); 
    } 
    else if(e.getCode() == KeyCode.LEFT){ 
     newX = newX - 10; 
     circle.setTranslateX(newX); 
    } 
    else if(e.getCode() == KeyCode.UP){ 
     newY = newY - 10; 
     circle.setTranslateY(newY); 
    } 
    else if(e.getCode() == KeyCode.DOWN){ 
     newY = newY + 10; 
     circle.setTranslateY(newY); 
    } 
}); 

请务必在上面的代码之前创建场景,否则会得到未定义的错误。