Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the following Polygon GUI App in java to have the program change the shape of the polygon depending on the dimensions, for example, it

Modify the following Polygon GUI App in java to have the program change the shape of the polygon depending on the dimensions, for example, it should display a circle when sides and radius are 100.package silvestri;
import java.awt.Color;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import shapes.RegularPolygon;
import shapes.Circle;
import shapes.Rectangle;
import shapes.Shape;
import shapes.Triangle;
import silvestri.UpdateablePolygon;
public class PolygonApp extends Application {
private TextField sidesField;
private TextField radiusField;
private TextField areaField;
private TextField perimeterField;
private UpdateablePolygon polygon;
@Override
public void start(Stage primaryStage){
// Create input fields and labels
Label sidesLabel = new Label("Sides:");
Label radiusLabel = new Label("Radius:");
sidesField = createClearableTextField();
radiusField = createClearableTextField();
Label areaLabel = new Label("Area:");
Label perimeterLabel = new Label("Perimeter:");
areaField = createReadonlyTextField();
perimeterField = createReadonlyTextField();
// Create a button
Button calculateButton = new Button("Calculate");
calculateButton.setOnAction(event -> calculatePolygon());
// Create an HBox for input fields and button
HBox inputBox = new HBox(10);
inputBox.getChildren().addAll(sidesLabel, sidesField, radiusLabel, radiusField, calculateButton);
// Create a BorderPane to hold input fields, output fields, and the polygon
BorderPane root = new BorderPane();
root.setTop(inputBox);
root.setLeft(areaLabel);
root.setLeft(areaField);
root.setRight(perimeterLabel);
root.setRight(perimeterField);
polygon = new UpdateablePolygon();
polygon.setSides(6);
root.setCenter(polygon);
// Configure the scene and stage
Scene scene = new Scene(root,600,400);
primaryStage.setTitle("Polygon Statistics Solver - Tony Silvestri");
primaryStage.setScene(scene);
primaryStage.show();
// Set the initial polygon sides and radius
sidesField.setText("6");
radiusField.setText("100");
calculatePolygon();
}
private TextField createClearableTextField(){
TextField textField = new TextField();
textField.setPrefWidth(100);
textField.setOnMousePressed(event -> textField.clear());
return textField;
}
private TextField createReadonlyTextField(){
TextField textField = new TextField();
textField.setPrefWidth(100);
textField.setEditable(false);
textField.setFocusTraversable(false);
textField.setMouseTransparent(true);
return textField;
}
private void calculatePolygon(){
int sides = Integer.parseInt(sidesField.getText());
double circumRadius = Double.parseDouble(radiusField.getText());
RegularPolygon regularPolygon = new RegularPolygon(sides, circumRadius, "Black", false);
areaField.setText(String.format("%.2f", regularPolygon.getArea()));
perimeterField.setText(String.format("%.2f", regularPolygon.getPerimeter()));
}
public static void main(String[] args){
launch(args);
}
}Develop a GUI that looks exactly like the following:
Polygon Statistics Solver - Tony Silvestri
Sides
Area
Perimeter
To create the polygon in the GUI, include the attached UpdateablePolygon.java file into your project. Put the file in a
package called silvestri.
Add the classes you used in the first part of this assignment and put them into a shapes package. (The test file source
files need not be included.)
When the calculate button is clicked, have your gui use your RegularPolygon class to determine the area and perimeter
from the specified sides and radius textfields. Also use the methods provided by the UpdateablePolygon to draw a cool
polygon.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Visualization A Practical Introduction

Authors: Kieran Healy

1st Edition

0691181624, 978-0691181622

More Books

Students also viewed these Databases questions