Question

In: Computer Science

Java In a JavaFX application there is a TextField control(tfRadius) for taking input of a circle’s...

Java

In a JavaFX application there is a TextField control(tfRadius) for taking input of a circle’s radius. A respective square whose diagonal is the circle's diameter can then be created. There are three other controls in this application: a button(btCalculate), two textareas(trCircleArea, trSquareArea). When the button is clicked, the circle's area is calculated and displayed on trCircleArea, furthermore, the square's area is also calculated and displayed on trSquareArea.

Please write the portions of codes which calculates the circle’s area and the square's area when the button(btCalculate) is clicked and both of the calculated areas are displayed on their respective textarea controls(trCircleArea, trSquareArea).   Assuming the codes for layout setup are completed and you are only required to complete the calculation part and handling of button click event.

Solutions

Expert Solution

Here is the answer for your question in Java Programming Language.

Kindly upvote if you find the answer helpful.

##############################################################

CODE :

Only button handling event and calculation part

//Button event handing code
btCalculate.setOnAction(e->{
//Use a try catch block to catch errors if any
try{
//Store text from tfRadius field into a double variable
double radius = Double.parseDouble(tfRadius.getText());
//Find diameter,circle area and square area
double diameter = 2*radius;
double circleArea = 3.14*radius*radius;
double squareArea = 0.5*diameter*diameter;
//Display them in respected text Areas
trCircleArea.setText("Circle Area : " + circleArea);
trSquareArea.setText("Square Area : " + squareArea);
}catch(Exception ex){
System.out.println("ERROR OCCURED");
}
  
});

####################################################################

Complete Code :


import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class CircleNSquare extends Application{

@Override
public void start(Stage primaryStage) throws Exception {
//Create required GUI Components
Label radLbl = new Label("Enter radius : ");
Label circleLbl = new Label("Circle Area : ");
Label sqaureLbl = new Label("Square Area : ");
TextField tfRadius = new TextField();
Button btCalculate = new Button("Calculate");
TextArea trCircleArea = new TextArea();
TextArea trSquareArea = new TextArea();
  
//Button event handing code
btCalculate.setOnAction(e->{
//Use a try catch block to catch errors if any
try{
//Store text from tfRadius field into a double variable
double radius = Double.parseDouble(tfRadius.getText());
//Find diameter,circle area and square area
double diameter = 2*radius;
double circleArea = 3.14*radius*radius;
double squareArea = 0.5*diameter*diameter;
//Display them in respected text Areas
trCircleArea.setText("Circle Area : " + circleArea);
trSquareArea.setText("Square Area : " + squareArea);
}catch(Exception ex){
System.out.println("ERROR OCCURED");
}
  
});
//Create a grid pane
GridPane pane = new GridPane();
//Set its insets
pane.setPadding(new Insets(10));
  
//Set horizontal and verticak gaps for the Grid pane
pane.setHgap(10);
pane.setVgap(10);
  
//Add the GUI components one by one to the pane
pane.add(radLbl, 0, 0);
pane.add(tfRadius, 1, 0);
pane.add(btCalculate, 1, 1);
pane.add(circleLbl, 0, 2);
pane.add(sqaureLbl, 0, 3);
pane.add(trCircleArea, 1, 2);
pane.add(trSquareArea, 1, 3);
  
//Create a scene with the grid pane
Scene scene = new Scene(pane);
//Attach scene to the current stage
primaryStage.setScene(scene);
//Set its title and show it
primaryStage.setTitle("Calculate Areas of Circle and Square");
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
}

#########################################################################

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

###########################################################################

OUTPUT :

Any doubts regarding this can be explained with pleasure :)


Related Solutions

In a JavaFX application there is a TextField control(tfRadius) for taking input of a circle's radius....
In a JavaFX application there is a TextField control(tfRadius) for taking input of a circle's radius. A respective square whose diagonal is the circle's diameter can then be created. There are three other controls in this application: a button(btCalculate), two textareas(trCircleArea, trSquareArea). When the button is clicked, the circle's area is calculated and displayed on trCircleArea, furthermore, the square's area is also calculated and displayed on trSquareArea. Please write portions of codes which calculates the circle's area and the square's...
In a JavaFX application there is a TextField control(tfRadius) for taking input of a circle’s radius....
In a JavaFX application there is a TextField control(tfRadius) for taking input of a circle’s radius. A respective square whose diagonal is the circle's diameter can then be created. There are three other controls in this application: a button(btCalculate), two textareas(trCircleArea, trSquareArea). When the button is clicked, the circle's area is calculated and displayed on trCircleArea, furthermore, the square's area is also calculated and displayed on trSquareArea. Please write the portions of codes which calculates the circle’s area and the...
Design JavaFX application with 7 labels and one textfield where user enters input inches.  When user enters...
Design JavaFX application with 7 labels and one textfield where user enters input inches.  When user enters his choice and presses enter key to complete input, program outputs resulting yards, feet, and inches.   Use class P5 that extends Application  with start method in it, and class P5Pane that extend GridPane. The only inctance variables for P5Pane class are inputInches where user enters input  inches, and three labels: outYards, outFeet, and outInches where program displays result of conversion.  Use the following names for instance variables:...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls:...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls: A Label control with the following text displayed: "First Number:" A Label control with the following text displayed: "Second Number:" An empty TextField control beside the First Number label. An empty TextField control beside the Second Number label. Five buttons: Button 1 labeled + Button 2 labeled - Button 3 labeled * Button 4 labeled / Button 5 labeled = An empty Label control...
In Java and using JavaFX, write a client/server application with two parts, a server and a...
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
Java Write a JavaFX application that displays a button and a number. Every time the button...
Java Write a JavaFX application that displays a button and a number. Every time the button is pushed, change the number to a random value between 1 and 100. Thank you and can you show a picture of the result as well
JAVAFX Create a JavaFx application that is an MP3 player. The size of the media player...
JAVAFX Create a JavaFx application that is an MP3 player. The size of the media player should be 800 pixels in width and 650 pixels in height. The media player should have a TextField that allows the user to type the full path of the .mp3 file to be played. The application should also use 2 Checkbox controls to show/hide the total playing time and the status (methods of MediaPlayer class). Use 2 RadioButton controls to change background colors (red...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
In java, Create a GUI which works as an accumulator 1. There is a textfield A...
In java, Create a GUI which works as an accumulator 1. There is a textfield A which allows user to enter a number 2. There is also another textfield B with value start with 0. 3. When a user is done with entering the number in textfield A and press enter, calculate the sum of the number in textfield B and the number user just entered in textfield A, and display the updated number in textfield B
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT