package sample; import javafx.animation.AnimationTimer; import javafx.application.Application; import javafx.event.EventHandler; import javafx.fxml.FXMLLoader; import javafx.scene.Group; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.input.KeyEvent; import javafx.scene.shape.Circle; import javafx.stage.Stage; public class Main extends Application { float x,y,r,dx,dy; @Override public void start(Stage primaryStage) throws Exception{ x = 300; y = 300; r = 50; dx = 10; dy = 3; // Szenen Graph Group root = new Group(); Circle c = new Circle(x,y,r); root.getChildren().add(c); Scene s = new Scene (root,600,600); root.getChildren().add(new Circle(100,100,10)); primaryStage.setScene(s); primaryStage.show(); // draw new AnimationTimer () { public void handle (long currentNanoTime) { x = x+dx; y = y+dy; c.setCenterX(x); c.setCenterY(y); if (x>600-r || x<0+r) { dx = dx*-1; } if (y>600-r || y<0+r) { dy = dy*-1; } } }.start(); // Tasten Eingabe s.setOnKeyPressed(new EventHandler() { @Override public void handle(KeyEvent event) { x = 0; } }); s.setOnKeyReleased(new EventHandler() { @Override public void handle(KeyEvent event) {/* Loslassen der Tasten */} }); } public static void main(String[] args) { launch(args); } }