- #1
smilesofmiles
- 19
- 0
This is actually Java!
My current problem: I need my switch statement to show only the given information for each selected shape.
For example when I click "circle" on the drop down menu, I want my switch to ask for the radius only while showing my compute button and answer.
When I click "triangle" on the drop down menu, I want my switch statement to ask for the base and height ect. ect.
I eventually want to be able to click the compute button to display that selected shape. The numbers entered from the user are dummy values. My compute button would just link to a drawASquareThing(), drawACircleThing(), drawATriangleThing() method.
I need my switch statement to work before I can achieve this though hahaha.
THANK YOU FOR ANY AND ALL HELP.Edits: I added more things to my code but I still can't figure out how to make it exit the switch statements to show each drop down option separately.
I still need advice. :(
I also tried to use return statements but they didn't help.My App
My MainFrame.java
My MainPanel.java
Things I've tried:
I've also tried moving the break statements to different positions, casting my array list to a integer type and checking to see if my variables are case sensitive.
My current problem: I need my switch statement to show only the given information for each selected shape.
For example when I click "circle" on the drop down menu, I want my switch to ask for the radius only while showing my compute button and answer.
When I click "triangle" on the drop down menu, I want my switch statement to ask for the base and height ect. ect.
I eventually want to be able to click the compute button to display that selected shape. The numbers entered from the user are dummy values. My compute button would just link to a drawASquareThing(), drawACircleThing(), drawATriangleThing() method.
I need my switch statement to work before I can achieve this though hahaha.
THANK YOU FOR ANY AND ALL HELP.Edits: I added more things to my code but I still can't figure out how to make it exit the switch statements to show each drop down option separately.
I still need advice. :(
I also tried to use return statements but they didn't help.My App
Code:
/*
A GUI that uses a JComboBox to prompt
the user for the type of shape they would like to create (circle, square, or triangle),
and then uses JLabel and JTextField objects to prompt them for measurements that are appropriate
for that shape (i.e. radius for circle, side length for square, base & height for triangle).
Your GUI should only include appropriate prompts (e.g. don't ask for radius if the user selects
a triangle). You may assume that the triangle is a right triangle (Links to an external site.).
2) A display of the shape described by the user including a graphic of the shape,
labelled measurements, and the calculated area. Note: Shapes do not need to be drawn to scale.
*/
public class App {
public static void main(String[] args) {
MainFrame mFrame = new MainFrame();
}
}
My MainFrame.java
Code:
import javax.swing.JFrame;
public class MainFrame extends JFrame {
MainFrame()
{
super("Let's make shapes");
MainPanel mPanel = new MainPanel(); // Special area that adds different components
getContentPane().add(mPanel); // Contains the panel
setSize(500,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Sets behavior of the exit button! :-) Close window / close program.
setVisible(true); // Shows us the program :-)
}
}
My MainPanel.java
Code:
import java.awt.event.ActionEvent;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
public class MainPanel extends JPanel implements ActionListener{
JTextField num1;
JTextField num2;
JTextField num3;
JTextField num4;
JLabel equals;
JTextField answer;
JLabel labelTxt;
JComboBox shapeList;
JButton drawAThing;
JLabel radiusLabel;
JLabel base;
JLabel lengthLabel;
JLabel height;
JButton drawACircle;
JButton drawASquare;
JButton drawATriangle;
MainPanel(){
String [] shapes = {"circle", "square", "triangle"};
shapeList = new JComboBox(shapes);
shapeList.setSelectedIndex(2);
shapeList.addActionListener(this);
add(shapeList);
}
@Override
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if (o == shapeList){
JComboBox cb = (JComboBox)e.getSource();
String msg = (String)cb.getSelectedItem();
switch(msg){
case "circle":
radiusLabel = new JLabel();
radiusLabel.setText("Radius: ");
add(radiusLabel);
num1 = new JTextField(12);
add(num1);
drawACircle = new JButton("Draw a circle ");
drawACircle.addActionListener(this);
add(drawACircle);
if (o == drawACircle){
drawACircleThing();
}
remove(drawACircle);
this.revalidate();
this.repaint();
break;
case "square":
lengthLabel = new JLabel();
lengthLabel.setText("Side Length: ");
add(lengthLabel);
num2 = new JTextField(12);
add(num2);
drawASquare = new JButton("Draw a square ");
drawASquare.addActionListener(this);
add(drawASquare);
if (o == drawASquare){
drawASquareThing();
remove(drawASquare);
this.revalidate();
this.repaint();
}
this.revalidate();
break;
case "triangle":
base = new JLabel();
base.setText("Base: ");
add(base);
num3 = new JTextField(12);
add(num3);
height = new JLabel();
base.setText("height: ");
add(height);
num4 = new JTextField(12);
add(num4);
drawATriangle = new JButton("Draw a triangle ");
drawATriangle.addActionListener(this);
add(drawATriangle);
if (o == drawATriangle){
drawATriangleThing();
remove(drawATriangle);
this.revalidate();
this.repaint();
}
this.revalidate();
break;
default:
break;
}
}
}
@Override
public void paintComponent(Graphics g)
{
g.setColor(Color.BLUE );
g.fillRect(50, 50, 200, 200);
g.fillOval(100,100,200,200);
}
private void drawASquareThing()
{
Graphics g = getGraphics();
g.setColor(Color.yellow);
g.fillRect(150,100,330,200);
g.drawString("My Triangle", 50, 10);
}
private void drawATriangleThing()
{
Graphics g = getGraphics();
g.setColor(Color.yellow);
g.fillRect(150,100,330,200);
g.drawString("My Triangle", 50, 10);
}
public void drawACircleThing()
{
Graphics g = getGraphics();
g.setColor(Color.ORANGE);
int [] xPoints = {50, 50, 100};
int [] yPoints = {50, 100, 100};
g.drawPolygon(xPoints, yPoints, 3);
g.fillPolygon(yPoints, xPoints, 3);
}}
Code:
// Object o = e.getSource(); // Holds who clicked
//
// if (o == shapeList)
// {
// String action = e.getActionCommand();
//
// if (action.equals("circle")) {
//
// radiusLabel = new JLabel();
// radiusLabel.setText("Radius: ");
// add(radiusLabel);
//
// num1 = new JTextField(12);
// add(num1);
//
// }
// int shapeCase = shapeList.getSelectedIndex();
//
// switch (shapeCase){
// case 1:
// radiusLabel = new JLabel();
// radiusLabel.setText("Radius: ");
// add(radiusLabel);
//
// num1 = new JTextField(12);
// add(num1);
//
// break;
// case 2:
// JLabel lengthLabel = new JLabel();
// lengthLabel.setText("Side Length: ");
// add(lengthLabel);
//
// num2 = new JTextField(12);
// add(num2);
// break;
// default:
// break;
// }
//shapeSelected.setText((String) shapeList.getSelectedItem()); //You can cast this!
}
//if (o == drawAThing)
//{
// drawSquare();
//}
}
}
I've also tried moving the break statements to different positions, casting my array list to a integer type and checking to see if my variables are case sensitive.
Last edited: