Answer to Question #179893 in Java | JSP | JSF for Hari Chandana T

Question #179893

Write a java program using Javafx to do the following:

1)Draw a circle shape and move forward when user presses the button

2)Draw a rectangle shape and size becomes bigger if user presses on button


1
Expert's answer
2021-04-09T15:11:06-0400
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane pane = new Pane();
        Label info = new Label("D - move the circle forward\nE - make the rectangle bigger");
        info.setLayoutX(10);
        info.setLayoutY(10);
        Circle circle = new Circle(400, 300, 50, Color.BLUE);
        Rectangle rectangle = new Rectangle(100, 100, 200, 100);
        rectangle.setFill(Color.PINK);
        Scene scene = new Scene(pane);
        scene.setOnKeyPressed((KeyEvent ke) -> {
            switch (ke.getCode()) {
                case E:
                    rectangle.setHeight(rectangle.getHeight() + 5);
                    rectangle.setWidth(rectangle.getWidth() + 5);
                    break;
                case D:
                    circle.setCenterX(circle.getCenterX() + 5);
                    break;
            }
        });
        pane.getChildren().addAll(rectangle, circle, info);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800);
        primaryStage.setHeight(600);
        primaryStage.show();
    }

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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog