Student Name: Noorhan Sadam Alazzam……32119001015
Write complete JavaFX application that takes width and height for a given rectangle then
computes boundary or area according to user selection
Code:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class RectangleCalculator extends Application {
private TextField widthField;
private TextField heightField;
private TextField resultLabel1;
private RadioButton areaRadioButton;
private RadioButton boundaryRadioButton;
private TextField resultField;
@Override
public void start(Stage stage) throws Exception {
// Create the UI elements
Label widthLabel = new Label("Enter Width:");
widthField = new TextField();
Label heightLabel = new Label("Enter Height:");
heightField = new TextField();
areaRadioButton = new RadioButton("Area");
boundaryRadioButton = new RadioButton("Boundary");
Label resultLabel1 = new Label("result:");
resultField = new TextField();
Button computeButton = new Button("compute");
Button closeButton = new Button("close");
// Set up the radio buttons
ToggleGroup toggleGroup = new ToggleGroup();
[Link](toggleGroup);
[Link](toggleGroup);
[Link](true);
// Set up the UI layout
GridPane gridPane = new GridPane();
[Link](10);
[Link](10);
[Link](new Insets(10));
[Link](widthLabel, 0, 0);
[Link](widthField, 1, 0);
[Link](heightLabel, 0, 1);
[Link](heightField, 1, 1);
[Link](resultLabel1, 0, 3);
[Link](resultField, 1,3);
[Link](areaRadioButton, 0, 4);
[Link](boundaryRadioButton, 1, 4);
HBox hbox = new HBox(10);
HBox hbox1 = new HBox(10);
[Link](Pos.CENTER_RIGHT);
[Link]().add(computeButton);
[Link](hbox, 0, 5);
[Link]().add(closeButton);
[Link](hbox1, 2, 5);
// Set up the event handlers
[Link](event -> compute());
[Link](event -> [Link]());
// Set up the scene and show the stage
Scene scene = new Scene(gridPane);
[Link](scene);
[Link]("Rectangle Calculator");
[Link]();
}
private void compute() {
// Get the input values from the text fields
double width = [Link]([Link]());
double height = [Link]([Link]());
// Compute the result based on the selected radio button
if ([Link]()) {
double area = width * height;
[Link]([Link]("%.2f", area));
} else {
double boundary = 2 * (width + height);
[Link]([Link]("%.2f", boundary));
private Button close(Stage stage){
Button closeButton = new Button("Close Window");
[Link](event -> [Link]());
return closeButton;
public static void main(String[] args) {
launch(args);
}
OUTPUT: