Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with what is in bold. GUI application Design an appropriate GUI application to set up and run the proposed Geometric Shape that would

Need help with what is in bold. GUI application Design an appropriate GUI application to set up and run the proposed Geometric Shape that would allow a user to create any Geometric Objects and read a text file containing Geometric object details and print its area and perimeter. You may use the sample design as given in the Lab 02 Question 5. The GUI for this assignment should create at least two new classes one being the JFrame for displaying the GUI JPanel, and the GUI JPanel with its components. The GUI needs to be able to allow Calculation of area and perimeter of different Geometric Objects based on a user selection. User should be able to select different Geometric Object and based on the Object type, user is asked to input radius, width, length or sides etc. Then the GUI application calculates and display area and perimeter. Reading of Geometric Object information from a file as GeometricOjects.txt and printing out a report of all Geometric Objects information with area and perimeter. Your GUI application should also contain a button to read a file GeometricObjects.txt. The program should read each line, calculate area and perimeter of the object and display the information in TextArea. Your program must work for any data file in this format as given below, regardless of how many entries. Format of the data file should be as the following: Object Type, Data Example Circle, 6.0 Rectangle, 3.5, 4.5 Triangle, 2.3, 3,4, 4,2 image text in transcribed My classes:

GeometricObject

public abstract class GeometricObject { protected String colour; protected boolean filledIn; // Construct a geometric object public GeometricObject(String colour, boolean filledIn) { this.colour = colour; this.filledIn = filledIn; } // Return colour public String getColour() { return colour; } // Set a new colour public void setColour(String colour) { this.colour = colour; } // Abstract method findArea public abstract double findArea(); // Abstract method findPerimeter() public abstract double findPerimeter(); public String toString() { return " Colour: " + colour + " and filled " + filledIn; }

}

Circle

public class Circle extends GeometricObject { private double radius; // Constructor public Circle(String colour, boolean filledIn, double radius) { super(colour, filledIn); this.radius = radius; } // Get and set methods for radius public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } // Methods to calculate area and perimeter public double findArea() { return Math.PI * radius * radius; } public double findPerimeter() { return 2 * Math.PI * radius; } // toString method public String toString() { String toScreen; toScreen = super.toString() + "Radius of a circle is " + radius; return toScreen; } }

Rectangle

public class Rectangle extends GeometricObject

{ private double width; private double length; // Constructor public Rectangle(String colour, boolean filledIn, double width, double length) { super(colour, filledIn); this.width = width; this.length = length; } // Getters and setters public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } // Calculate area and perimeter public double findArea() { return length * width; } public double findPerimeter() { return 2 * (length + width); } // Override parent toString method public String toString() { String toScreen; toScreen = super.toString() + "Width and length of a rectangle is " + width + " " + length + " respectively"; return toScreen; } }

EquilateralTriangle

public class EquilateralTriangle extends GeometricObject { private double side;

public EquilateralTriangle(String colour, boolean filledIn, double side) //Constructor { super(colour, filledIn); this.side = side; }

public double getSide() { return side; }

public void setSide(double side) { this.side = side; }

public double findArea() { return ( Math.sqrt(3) * side *side)/4; }

public double findPerimeter() { return 3 * side; }

public String toString() { return super.getColour() + "\t" + super.filledIn + "\t" + getSide(); } } //End Class

Cylinder

public class Cylinder extends GeometricObject { private double radius; private double height;

public Cylinder(String colour, boolean filledIn, double radius,double height) //Constructor { super(colour, filledIn); this.radius = radius; this.height = height; }

public double getRadius() { return radius; }

public void setRadius(double radius) { this.radius = radius; }

public double getHeight() { return height; }

public void setHeight(double height) { this.height = height; }

public double findArea() { return 2* Math.PI * radius *(height+ radius); }

public double findPerimeter() { return 2 * (2* Math.PI * radius+height); }

public String toString() { return super.getColour() + "\t" + super.filledIn + "\t" + getRadius()+ "\t" + getHeight(); } } //End Class

Panel

// Import packages import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.border.*;

public class Question5Panel extends JPanel implements ActionListener { // data declarations private JLabel shapeLabel; private JRadioButton circleButton; private JRadioButton rectangleButton; private JRadioButton equTButton; private JRadioButton cylinderButton; private ButtonGroup shape; private JPanel selectShape; private JLabel radius; private JTextField radiusIn; private JLabel rectangle; private JLabel length; private JTextField lengthIn; private JLabel width; private JTextField widthIn; private JLabel side; private JTextField sideIn; private JLabel cylinder; private JLabel height; private JTextField heightIn; private JLabel ra; private JTextField raIn; private JPanel westPanel; private JPanel circlePanel; private JPanel rectanglePanel; private JPanel equTPanel; private JPanel cylinderPanel; private JPanel outputPanel; private JTextArea output; private Circle myCircle; private Rectangle myRectangle; private Rectangle myEquTriangle; private Rectangle myCylinder;

// constructor to initiate data and set up GUI public Question5Panel() { setLayout(new BorderLayout());

// setting up north area of GUI

shapeLabel = new JLabel("Please select a shape "); shapeLabel.setFont(new Font("Arial", Font.BOLD, 16));

// organizing radio buttons and their behaviours

circleButton = new JRadioButton("Circle"); rectangleButton = new JRadioButton("Rectangle"); equTButton = new JRadioButton("EquilateralTriangle"); cylinderButton = new JRadioButton("Cylinder"); shape = new ButtonGroup(); shape.add(circleButton); shape.add(rectangleButton); shape.add(equTButton); shape.add(cylinderButton);

// adding components to panel to be located north in the GUI

selectShape = new JPanel(); selectShape.add(shapeLabel); selectShape.add(circleButton); selectShape.add(rectangleButton); selectShape.add(equTButton); selectShape.add(cylinderButton); add(selectShape, BorderLayout.NORTH);

// setting up west area of GUI

westPanel = new JPanel();

// setting up components for the circlePanel of the GUI

circlePanel = new JPanel(); radius = new JLabel("radius is: "); radiusIn = new JTextField(10); circlePanel.add(radius); circlePanel.add(radiusIn); TitledBorder circleBorder = BorderFactory.createTitledBorder("Circle"); circlePanel.setBorder(circleBorder); add(westPanel, BorderLayout.WEST);

// setting up components for the rectanglePanel of GUI

length = new JLabel("length is: "); lengthIn = new JTextField(10); width = new JLabel("width is: "); widthIn = new JTextField(10);

// adding components to the rectanglePanel of GUI

rectanglePanel = new JPanel(); rectanglePanel.add(length); rectanglePanel.add(lengthIn); rectanglePanel.add(width); rectanglePanel.add(widthIn); TitledBorder rectangleBorder = BorderFactory.createTitledBorder("Rectangle"); rectanglePanel.setBorder(rectangleBorder);

// setting up components for the equTPanel of the GUI

equTPanel= new JPanel(); side = new JLabel("side is: "); sideIn = new JTextField(10); equTPanel.add(side); equTPanel.add(sideIn); TitledBorder equtBorder = BorderFactory.createTitledBorder("EquilateralTriangle"); equTPanel.setBorder(equtBorder);

// setting up components for the cylinderPanel of GUI

height = new JLabel("height is: "); heightIn = new JTextField(10); ra = new JLabel("radius is: "); raIn = new JTextField(10);

// adding components to the cylinderPanel of GUI

cylinderPanel = new JPanel(); cylinderPanel.add(height); cylinderPanel.add(heightIn); cylinderPanel.add(ra); cylinderPanel.add(raIn); TitledBorder cylinderBorder = BorderFactory.createTitledBorder("Cylinder"); cylinderPanel.setBorder(cylinderBorder); add(westPanel, BorderLayout.WEST);

// adding the circlePanel, rectanglePanels, equTPanel, and cylinderPanel into the westPanel

westPanel.setLayout(new GridLayout(2, 1)); westPanel.setPreferredSize(new Dimension(400, 15)); westPanel.add(circlePanel); westPanel.add(rectanglePanel); westPanel.add(equTPanel); westPanel.add(cylinderPanel);

// setting up center area of GUI

outputPanel = new JPanel(); output = new JTextArea(15, 25); outputPanel.add(output); add(outputPanel, BorderLayout.CENTER);

// add listeners to radio buttons

circleButton.addActionListener(this); rectangleButton.addActionListener(this); equTButton.addActionListener(this); cylinderButton.addActionListener(this);

// add listeners to all textfields

radiusIn.addActionListener(this); lengthIn.addActionListener(this); widthIn.addActionListener(this); sideIn.addActionListener(this); heightIn.addActionListener(this); raIn.addActionListener(this);

} // end constructor

//*************************************************************************** // Students to complete the functionality of the GUI through actionPerformed //***************************************************************************

public void actionPerformed(ActionEvent ae) { System.out.println(ae.getActionCommand()); String data = "";

if (ae.getActionCommand().equals("Circle")) { data = getData(0); output.setText(data); }

else if (ae.getActionCommand().equals("Rectangle")) { data = getData(0, 0); System.out.println(data); output.setText(data); }

else if (ae.getActionCommand().equals("EquilateralTriangle")) { data = getData(0); output.setText(data); }

else if (ae.getActionCommand().equals("Cylinder")) { data = getData(0, 0); System.out.println(data); output.setText(data); }

if (circleButton.isSelected()) { System.out.println("--");

try { int i=Integer.parseInt(radiusIn.getText()); data=getData(i); System.out.println(); output.setText(data); }

catch (Exception e) {

} }

if (rectangleButton.isSelected()) { try { try { int i=Integer.parseInt(lengthIn.getText()); int j=Integer.parseInt(widthIn.getText()); System.out.println(i+","+j); data=getData(i,j); output.setText(data); }

catch (Exception e) {

} }

catch (Exception e) {

}

}

if (equTButton.isSelected()) { System.out.println("--");

try { int a=Integer.parseInt(sideIn.getText()); data=getData1(a); System.out.println(); output.setText(data); }

catch (Exception e) {

}

}

if (cylinderButton.isSelected()) { try { try { int a=Integer.parseInt(heightIn.getText()); int b=Integer.parseInt(raIn.getText()); System.out.println(a+","+b); data=getData1(a,b); output.setText(data); }

catch (Exception e) {

}

}

catch (Exception e) {

}

}

}

private String getData(int i, int j) { Rectangle rectangle=new Rectangle("black",false,i,j); return "rectangle has length of " + i + " with a width of " + j + " " + "and a perimeter of " + rectangle.findPerimeter() + " and a area of " + rectangle.findArea(); }

private String getData(int i) { Circle circle = new Circle("black",false,i); return "circle has radius of " + i + " " + "and a perimeter of " + circle.findPerimeter() + " and a area of " + circle.findArea(); }

private String getData1(int a, int b) { Cylinder cylinder=new Cylinder("black",false,a,b); return "cylinder has height of " + a+ " with a radius of " + b+ " " + "and a perimeter of " + cylinder.findPerimeter() + " and a area of " + cylinder.findArea(); }

private String getData1(int a) { EquilateralTriangle eqt=new EquilateralTriangle("black",false,a); return " Equilaternal triangle has side of " + a + " " + "and a perimeter of " + eqt.findPerimeter() + " and a area of " + eqt.findArea(); } } // end class

Frame

import javax.swing.* ; import java.awt.* ; import java.awt.event.*;

public class Question5Frame { public static void main(String [] args) { JFrame myFrame = new JFrame("Lab 3: Question 5") ;

// create an instance of Question5Panel and add to frame Question5Panel myPanel = new Question5Panel( ) ; myFrame.add (myPanel );

// set up functionality of frame myFrame.setSize(500, 310 ); myFrame.setVisible(true); myFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ;; }//end main } // end class

Format of the data file should be as the following: Object Type Data Example Circle, 6.0 Rectangle 3. 5, 4.5 Triangle 2.3, 3, 4, 4, 2

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

Beginning PostgreSQL On The Cloud Simplifying Database As A Service On Cloud Platforms

Authors: Baji Shaik ,Avinash Vallarapu

1st Edition

1484234464, 978-1484234464

More Books

Students also viewed these Databases questions

Question

Identify how culture affects appropriate leadership behavior

Answered: 1 week ago