Code:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class practical18 extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
pane.setPadding(new Insets(30, 30, 30, 30));
Circle circle = new Circle(30, 30, 30);
pane.getChildren().add(circle);
pane.setOnKeyPressed(e -> {
switch (e.getCode()) {
case UP ->
circle.setCenterY(circle.getCenterY()
> circle.getRadius() ? circle.getCenterY() - 15
: circle.getCenterY());
case DOWN ->
circle.setCenterY(circle.getCenterY()
< pane.getHeight() - circle.getRadius()
? circle.getCenterY() + 15 : circle.getCenterY());
case LEFT ->
circle.setCenterX(circle.getCenterX()
> circle.getRadius() ? circle.getCenterX() - 15
: circle.getCenterX());
case RIGHT ->
circle.setCenterX(circle.getCenterX()
< pane.getWidth() - circle.getRadius()
? circle.getCenterX() + 15 : circle.getCenterX());
}
});
Scene scene = new Scene(pane, 300, 300);
primaryStage.setTitle("practical18");
primaryStage.setScene(scene);
primaryStage.show();
pane.requestFocus();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Comments
Post a Comment