SHARE
TWEET

Untitled

a guest Mar 20th, 2017 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.Color;
  2. import java.awt.Container;
  3. import java.awt.Graphics;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.JButton;
  7. import javax.swing.JComboBox;
  8. import javax.swing.JLabel;
  9. import javax.swing.JPanel;
  10. import javax.swing.JTextField;
  11.  
  12. public class MainPanel extends JPanel implements ActionListener {
  13.  
  14. JComboBox shapeList;
  15.  
  16. JTextField answer;
  17. JTextField shapeSelected;
  18. JTextField circle;
  19. JTextField square;
  20. JTextField triangle;
  21. JTextField triangle3;
  22.  
  23. JLabel circle2;
  24. JLabel square2;
  25. JLabel triangle2;
  26. JLabel triangle4;
  27. JLabel triangle5;
  28. JLabel squareAnswer;
  29. JLabel circleAnswer;
  30. JLabel triangleAnswer;
  31.  
  32. JButton calculate;
  33.  
  34. MainPanel()
  35. {
  36.     String [] shapes = {"circle", "square", "triangle"};
  37.  
  38.     shapeList = new JComboBox(shapes);
  39.     shapeList.addActionListener(this);
  40.     add(shapeList);
  41. }
  42.  
  43. private void squareDraw()
  44. {
  45.     Graphics g = getGraphics();
  46.     g.setColor(Color.blue);
  47.     g.fillRect(200, 250, 100, 100);
  48. }
  49.  
  50. private void circleDraw()
  51. {
  52.     Graphics g = getGraphics();
  53.     g.setColor(Color.blue);
  54.     g.fillOval(200, 250, 100, 100);
  55. }
  56.  
  57. private void triangleDraw()
  58. {
  59.     Graphics g = getGraphics();
  60.     g.setColor(Color.blue);
  61.     g.fillPolygon(new int[] {50, 100, 0}, new int[] {0, 100, 100}, 3);
  62. }
  63.  
  64. @Override
  65. public void actionPerformed(ActionEvent e)
  66. {
  67.     Object o = e.getSource();
  68.  
  69.     if (o == shapeList)
  70.     {  
  71.         if (shapeList.getSelectedItem().equals("circle"))
  72.         {
  73.             circle2 = new JLabel();
  74.             circle2.setText("Radius:");
  75.             add(circle2);
  76.  
  77.             circle = new JTextField(10);
  78.             add(circle);
  79.  
  80.             circleAnswer = new JLabel();
  81.             circleAnswer.setText("Area:");
  82.             add(circleAnswer);
  83.  
  84.             answer = new JTextField(10);
  85.             add(answer);
  86.  
  87.             calculate = new JButton("Calculate Area");
  88.             calculate.addActionListener(this);
  89.             add(calculate);
  90.  
  91.             this.revalidate();
  92.             this.repaint();    
  93.         }
  94.  
  95.         if (shapeList.getSelectedItem().equals("square"))
  96.         {
  97.             square2 = new JLabel();
  98.             square2.setText("Width/Height:");
  99.             add(square2);
  100.  
  101.             square = new JTextField(10);
  102.             add(square);
  103.  
  104.             squareAnswer = new JLabel();
  105.             squareAnswer.setText("Area:");
  106.             add(squareAnswer);
  107.  
  108.             answer = new JTextField(10);
  109.             add(answer);
  110.  
  111.             calculate = new JButton("Calculate Area");
  112.             calculate.addActionListener(this);
  113.             add(calculate);
  114.  
  115.             this.revalidate();
  116.             this.repaint();
  117.         }
  118.  
  119.         if (shapeList.getSelectedItem().equals("triangle"))
  120.         {
  121.             triangle4 = new JLabel();
  122.             triangle4.setText("Base:");
  123.             add(triangle4);
  124.  
  125.             triangle = new JTextField(10);
  126.             add(triangle);
  127.  
  128.             triangle5 = new JLabel();
  129.             triangle5.setText("Height:");
  130.             add(triangle5);
  131.  
  132.             triangle3 = new JTextField(10);
  133.             add(triangle3);
  134.  
  135.             triangleAnswer = new JLabel();
  136.             triangleAnswer.setText("Area:");
  137.             add(triangleAnswer);
  138.  
  139.             answer = new JTextField(10);
  140.             add(answer);
  141.  
  142.             calculate = new JButton("Calculate Area");
  143.             calculate.addActionListener(this);
  144.             add(calculate);
  145.  
  146.             this.revalidate();
  147.             this.repaint();
  148.         }
  149.     }
  150.  
  151.     if (o == calculate)
  152.     {            
  153.         if (shapeList.getSelectedItem().equals("square"))
  154.         {  
  155.             double sum = Double.parseDouble(square.getText()) * Double.parseDouble(square.getText());
  156.             answer.setText(String.valueOf(sum));
  157.  
  158.             squareDraw();
  159.         }
  160.  
  161.         if (shapeList.getSelectedItem().equals("triangle"))
  162.         {    
  163.             double sum = (Double.parseDouble(triangle.getText()) * Double.parseDouble(triangle3.getText())) / 2;
  164.             answer.setText(String.valueOf(sum));
  165.  
  166.             triangleDraw();
  167.         }
  168.  
  169.         if (shapeList.getSelectedItem().equals("circle"))
  170.         {  
  171.             double sum = (Double.parseDouble(circle.getText()) * Double.parseDouble(circle.getText())) * Math.PI;
  172.             answer.setText(String.valueOf(sum));
  173.  
  174.             circleDraw();
  175.         }
  176.     }
  177. }
  178. }
RAW Paste Data
Top