15.2 (Rotate a rectangle) Write a program that rotates a rectangle 15 degrees right when the Rotate button is clicked, as shown in Figure 15.24b.
package section_15;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class _02 extends Application {
public void start(Stage primaryStage) {
Rectangle rectangle = new Rectangle();
rectangle.setWidth(100);
rectangle.setHeight(40);
rectangle.setStroke(Color.BLACK);
rectangle.setFill(Color.WHITE);
Button btRotate = new Button("Rotate");
btRotate.setOnAction(e -> rectangle.setRotate(rectangle.getRotate() + 15));
BorderPane pane = new BorderPane();
pane.setCenter(rectangle);
pane.setBottom(btRotate);
BorderPane.setAlignment(rectangle, Pos.CENTER);
BorderPane.setAlignment(btRotate, Pos.CENTER);
pane.setPadding(new Insets(5, 5, 5, 5));
Scene scene = new Scene(pane, 200, 200);
primaryStage.setTitle("_15_02");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}