Question: below is code can enlarge and shrink a circle , u can run and see the output in netbean or esclipse. Modify this code of

below is code can enlarge and shrink a circle, u can run and see the output in netbean or esclipse.

Modify this code of the JavaFX Example, so that it can enlarge and shrink a rectangle.

-----------------------------------------------------------------------------------------------------------------------------------------------------------

package controlcircle;

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

public class ControlCircle extends Application { private CirclePane circlePane = new CirclePane();

@Override // Override the start method in the Application class public void start(Stage primaryStage) { // Hold two buttons in an HBox HBox hBox = new HBox(); hBox.setSpacing(10); hBox.setAlignment(Pos.CENTER); Button btEnlarge = new Button("Enlarge"); Button btShrink = new Button("Shrink"); hBox.getChildren().add(btEnlarge); hBox.getChildren().add(btShrink); // Create and register the handler btEnlarge.setOnAction(new EnlargeHandler()); btShrink.setOnAction(new ShrinkHandler()); BorderPane borderPane = new BorderPane(); borderPane.setCenter(circlePane); borderPane.setBottom(hBox); BorderPane.setAlignment(hBox, Pos.CENTER); // Create a scene and place it in the stage Scene scene = new Scene(borderPane, 200, 150); primaryStage.setTitle("ControlCircle"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } class EnlargeHandler implements EventHandler { @Override // Override the handle method public void handle(ActionEvent e) { circlePane.enlarge(); } } class ShrinkHandler implements EventHandler { @Override // Override the handle method public void handle(ActionEvent e) { circlePane.shrink(); } } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } }

class CirclePane extends StackPane { private Circle circle = new Circle(50); public CirclePane() { getChildren().add(circle); circle.setStroke(Color.BLACK); circle.setFill(Color.WHITE); } public void enlarge() { circle.setRadius(circle.getRadius() + 2); } public void shrink() { circle.setRadius(circle.getRadius() > 2 ? circle.getRadius() - 2 : circle.getRadius()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!