Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Take the program created below and turn it into a GUI application. Extra Credit:

ID: 3646394 • Letter: T

Question

Take the program created below and turn it into a GUI application.

Extra Credit:

When a shape is selected and the size of the parameters (i.e., sides, diameter, radius, etc.) is
given draw the image using that information. For example, if you specify a square with sides that
are 4 cm, draw a square with sides that are 4 cm in size. Obviously, you will need to place limits
on the parameters you can specify to make sure the images will fit on your screen.
Use the drawString() method to display the circumference calculated from each shape.

******************************************************

Here is the program, broken down into five files:

*******************************************************
ShapeDrive.java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ShapeDriver
{

/**
* @param args
* @throws IOException
*/

public static void main(String[] args) throws IOException
{

// TODO Auto-generated method stub

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String readShape = "";
int shape = 0;
Square s = null;
boolean validInput = false;
String exit = "";
while (exit != "5")
{
System.out.println("1 - Square");
System.out.println("2 - Rectangle");
System.out.println("3 - Triangle");
System.out.println("4 - Circle");
System.out.println("5 - Exit");

while (!validInput)
{
System.out.print ("Select your Shape: ");
readShape = br.readLine();
shape = Integer.parseInt(readShape);
if (shape < 5 && shape > 6)
{
validInput = true;
}
else
{
System.out.println("Invalid shape.");
}
}
validInput = false;

float len;
float wid;
float rad;

switch (shape)
{
case 1: {
System.out.println("Side Length:");
s = new Square(Integer.parseInt(br.readLine()));
break;
}
case 2: {
System.out.println("Length:");
len = Float.parseFloat(br.readLine());
System.out.println("Width:");
wid = Float.parseFloat(br.readLine());
s = new Rectangle(len, wid);
break;
}
case 3: {
System.out.println("Side Length:");
s = new Triangle(Integer.parseInt(br.readLine()));
break;
}
case 4: {
System.out.println("Radius:");
s = new Circle(Integer.parseInt(br.readLine()));
break;
}
case 5: {
return;
}
}

System.out.println();
System.out.println("Circumference: " + s.getCircum());
}
}
}

********************************************
Circle.java


public class Circle extends Square
{
float radius;
final float pi = (float) 3.14;
public Circle(float rad)
{
this.radius = rad;
}
public float getCircum()
{
return 2 * pi * radius;
}
}

********************************************
Triangle.java


public class Triangle extends Square{
//we'll just use an equalateral
public Triangle(float side)
{
super(side);
}
public float getCircum()
{
//Triangle only has 3 sides, look! Using a parent method!
return super.getCircum()/4*3;
}
}

***********************************************
Rectangle.java


public class Rectangle extends Square
{
float length;
float width;
public Rectangle(float len, float wid)
{
length = len;
width = wid;
}
public float getCircum()
{
return (length*2)+(width*2);
}
}

**********************************************
Square.java


public class Square
{
float length;
public Square(){};
public Square(float len)
{
this.length = len;
}
public float getCircum()
{
return length * 4;
}
}

Explanation / Answer

//Imports are listed in full to show what's being used //could just import javax.swing.* and java.awt.* etc.. import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JComboBox; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JList; import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class GuiApp1 { //Note: Typically the main method will be in a //separate class. As this is a simple one class //example it's all in the one class. public static void main(String[] args) { new GuiApp1(); } public GuiApp1() { JFrame guiFrame = new JFrame(); //make sure the program exits when the frame closes guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); guiFrame.setTitle("Example GUI"); guiFrame.setSize(300,250); //This will center the JFrame in the middle of the screen guiFrame.setLocationRelativeTo(null); //Options for the JComboBox String[] fruitOptions = {"Apple", "Apricot", "Banana" ,"Cherry", "Date", "Kiwi", "Orange", "Pear", "Strawberry"}; //Options for the JList String[] vegOptions = {"Asparagus", "Beans", "Broccoli", "Cabbage" , "Carrot", "Celery", "Cucumber", "Leek", "Mushroom" , "Pepper", "Radish", "Shallot", "Spinach", "Swede" , "Turnip"}; //The first JPanel contains a JLabel and JCombobox final JPanel comboPanel = new JPanel(); JLabel comboLbl = new JLabel("Fruits:"); JComboBox fruits = new JComboBox(fruitOptions); comboPanel.add(comboLbl); comboPanel.add(fruits); //Create the second JPanel. Add a JLabel and JList and //make use the JPanel is not visible. final JPanel listPanel = new JPanel(); listPanel.setVisible(false); JLabel listLbl = new JLabel("Vegetables:"); JList vegs = new JList(vegOptions); vegs.setLayoutOrientation(JList.HORIZONTAL_WRAP); listPanel.add(listLbl); listPanel.add(vegs); JButton vegFruitBut = new JButton( "Fruit or Veg"); //The ActionListener class is used to handle the //event that happens when the user clicks the button. //As there is not a lot that needs to happen we can //define an anonymous inner class to make the code simpler. vegFruitBut.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { //When the fruit of veg button is pressed //the setVisible value of the listPanel and //comboPanel is switched from true to //value or vice versa. listPanel.setVisible(!listPanel.isVisible()); comboPanel.setVisible(!comboPanel.isVisible()); } }); //The JFrame uses the BorderLayout layout manager. //Put the two JPanels and JButton in different areas. guiFrame.add(comboPanel, BorderLayout.NORTH); guiFrame.add(listPanel, BorderLayout.CENTER); guiFrame.add(vegFruitBut,BorderLayout.SOUTH); //make sure the JFrame is visible guiFrame.setVisible(true); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote