Advertisement
Guest User

Lab6GUI.java

a guest
Feb 26th, 2022
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.07 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.InputMismatchException;
  6.  
  7. import javax.swing.ButtonGroup;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12. import javax.swing.JRadioButton;
  13. import javax.swing.JTextField;
  14.  
  15. public class Lab6GUI implements ActionListener {
  16.     static int width;
  17.     static int height;
  18.     static int radius;
  19.     static int length;
  20.    
  21.     static int maxWidth = 300;
  22.     static int maxHeight = 300;
  23.     static int maxLength = 300;
  24.     static int maxRadius = 400;
  25.    
  26.     static int minWidth = 50;
  27.     static int minHeight = 50;
  28.     static int minLength = 50;
  29.     static int minRadius = 50;
  30.    
  31.     static boolean dimensionError;
  32.    
  33.     static int type;
  34.    
  35.     JLabel titleLabel;
  36.     JLabel radioLabel;
  37.     JLabel widthLabel;
  38.     JLabel heightLabel;
  39.     JLabel radiusLabel;
  40.     JLabel lengthLabel;
  41.  
  42.     JRadioButton rectangleRadioButton;
  43.     JRadioButton boxRadioButton;
  44.     JRadioButton circleRadioButton;
  45.     JRadioButton cylinderRadioButton;
  46.    
  47.     ButtonGroup radioButtonGroup;
  48.  
  49.     JTextField widthTextField;
  50.     JTextField lengthTextField;
  51.     JTextField heightTextField;
  52.     JTextField radiusTextField;
  53.  
  54.     JPanel radioButtonPanel;
  55.     JPanel widthPanel;
  56.     JPanel lengthPanel;
  57.     JPanel heightPanel;
  58.     JPanel radiusPanel;
  59.     JPanel totalGUI;
  60.  
  61.     JButton processButton;
  62.    
  63.     public JPanel createContentPane() {
  64.         Font titleFont = new Font("Verdana", Font.BOLD, 26);
  65.        
  66.         totalGUI = new JPanel();
  67.         totalGUI.setBackground(Color.DARK_GRAY);
  68.         totalGUI.setLayout(null);
  69.        
  70.         titleLabel = new JLabel("The Figure Center");
  71.         titleLabel.setLocation(170, 20);
  72.         titleLabel.setSize(350, 100);
  73.         titleLabel.setForeground(Color.WHITE);
  74.         titleLabel.setFont(titleFont);
  75.         totalGUI.add(titleLabel);
  76.        
  77.         // Radio Button Panel
  78.        
  79.         radioButtonPanel = new JPanel();
  80.         radioButtonPanel.setBackground(null);
  81.         radioButtonPanel.setLayout(null);
  82.         radioButtonPanel.setSize(130, 120);
  83.         radioButtonPanel.setLocation(90, 125);
  84.        
  85.         radioLabel = new JLabel("Select a figure.");
  86.         radioLabel.setLocation(0, -3);
  87.         radioLabel.setSize(100, 30);
  88.         radioLabel.setForeground(Color.white);
  89.         radioButtonPanel.add(radioLabel);
  90.        
  91.         rectangleRadioButton = new JRadioButton("Rectangle");
  92.         rectangleRadioButton.setLocation(0, 20);
  93.         rectangleRadioButton.setSize(100, 30);
  94.         rectangleRadioButton.setBackground(null);
  95.         rectangleRadioButton.setForeground(Color.white);
  96.         rectangleRadioButton.setSelected(false);
  97.         rectangleRadioButton.addActionListener(this);
  98.         radioButtonPanel.add(rectangleRadioButton);
  99.        
  100.         boxRadioButton = new JRadioButton("Box");
  101.         boxRadioButton.setLocation(0, 45);
  102.         boxRadioButton.setSize(100, 25);
  103.         boxRadioButton.setBackground(null);
  104.         boxRadioButton.setForeground(Color.white);
  105.         boxRadioButton.addActionListener(this);
  106.         radioButtonPanel.add(boxRadioButton);
  107.        
  108.         circleRadioButton = new JRadioButton("Circle");
  109.         circleRadioButton.setLocation(0, 67);
  110.         circleRadioButton.setSize(100, 25);
  111.         circleRadioButton.setBackground(null);
  112.         circleRadioButton.setForeground(Color.WHITE);
  113.         circleRadioButton.setSelected(false);
  114.         circleRadioButton.addActionListener(this);
  115.         radioButtonPanel.add(circleRadioButton);
  116.        
  117.         cylinderRadioButton = new JRadioButton("Cylinder");
  118.         cylinderRadioButton.setLocation(0, 90);
  119.         cylinderRadioButton.setSize(100, 25);
  120.         cylinderRadioButton.setBackground(null);
  121.         cylinderRadioButton.setForeground(Color.white);
  122.         cylinderRadioButton.setSelected(false);
  123.         cylinderRadioButton.addActionListener(this);
  124.         radioButtonPanel.add(cylinderRadioButton);
  125.        
  126.         radioButtonGroup = new ButtonGroup();
  127.         radioButtonGroup.add(rectangleRadioButton);
  128.         radioButtonGroup.add(boxRadioButton);
  129.         radioButtonGroup.add(circleRadioButton);
  130.         radioButtonGroup.add(cylinderRadioButton);
  131.  
  132.         totalGUI.add(radioButtonPanel);
  133.  
  134.         // Width Panel
  135.        
  136.         widthPanel = new JPanel();
  137.         widthPanel.setBackground(null);
  138.         widthPanel.setLayout(null);
  139.         widthPanel.setSize(70, 50);
  140.         widthPanel.setLocation(250, 125);
  141.        
  142.         widthLabel = new JLabel("Enter Width");
  143.         widthLabel.setBackground(null);
  144.         widthLabel.setForeground(Color.WHITE);
  145.         widthLabel.setLocation(0, 0);
  146.         widthLabel.setSize(70, 25);
  147.         widthPanel.add(widthLabel);
  148.        
  149.         widthTextField = new JTextField();
  150.         widthTextField.setLocation(0, 25);
  151.         widthTextField.setSize(70, 20);
  152.         widthPanel.add(widthTextField);
  153.        
  154.         totalGUI.add(widthPanel);
  155.        
  156.         // Radius Panel
  157.        
  158.         radiusPanel = new JPanel();
  159.         radiusPanel.setBackground(null);
  160.         radiusPanel.setLayout(null);
  161.         radiusPanel.setSize(75, 50);
  162.         radiusPanel.setLocation(250, 185);
  163.        
  164.         radiusLabel = new JLabel("Enter Radius");
  165.         radiusLabel.setBackground(null);
  166.         radiusLabel.setForeground(Color.WHITE);
  167.         radiusLabel.setSize(75, 25);
  168.         radiusLabel.setLocation(0, 0);
  169.         radiusPanel.add(radiusLabel);
  170.        
  171.         radiusTextField = new JTextField();
  172.         radiusTextField.setLocation(0, 25);
  173.         radiusTextField.setSize(70, 20);
  174.         radiusPanel.add(radiusTextField);
  175.        
  176.         totalGUI.add(radiusPanel);
  177.        
  178.         // Length Panel
  179.        
  180.         lengthPanel = new JPanel();
  181.         lengthPanel.setBackground(null);
  182.         lengthPanel.setLayout(null);
  183.         lengthPanel.setSize(75, 50);
  184.         lengthPanel.setLocation(400, 125);
  185.        
  186.         lengthLabel = new JLabel("Enter Length");
  187.         lengthLabel.setForeground(Color.WHITE);
  188.         lengthLabel.setSize(75, 25);
  189.         lengthLabel.setLocation(0, 0);
  190.         lengthPanel.add(lengthLabel);
  191.        
  192.         lengthTextField = new JTextField();
  193.         lengthTextField.setSize(75, 20);
  194.         lengthTextField.setLocation(0, 25);
  195.         lengthPanel.add(lengthTextField);
  196.        
  197.         totalGUI.add(lengthPanel);
  198.        
  199.         // Height Panel
  200.        
  201.         heightPanel = new JPanel();
  202.         heightPanel.setBackground(null);
  203.         heightPanel.setLayout(null);
  204.         heightPanel.setSize(75, 50);
  205.         heightPanel.setLocation(400, 185);
  206.        
  207.         heightLabel = new JLabel("Enter Height");
  208.         heightLabel.setForeground(Color.WHITE);
  209.         heightLabel.setSize(75, 25);
  210.         heightLabel.setLocation(0, 0);
  211.         heightPanel.add(heightLabel);
  212.        
  213.         heightTextField = new JTextField();
  214.         heightTextField.setSize(75, 20);
  215.         heightTextField.setLocation(0, 25);
  216.         heightPanel.add(heightTextField);
  217.        
  218.         totalGUI.add(heightPanel);
  219.        
  220.         // Process Button
  221.        
  222.         processButton = new JButton("Click to Process");
  223.         processButton.setLocation(270, 260);
  224.         processButton.setSize(185, 50);
  225.         processButton.addActionListener(this);
  226.         totalGUI.add(processButton);
  227.        
  228.         // Make the panel invisible
  229.         heightPanel.setVisible(false);
  230.         lengthPanel.setVisible(false);
  231.         radiusPanel.setVisible(false);
  232.         widthPanel.setVisible(false);
  233.         processButton.setVisible(false);
  234.        
  235.         return totalGUI;
  236.     } // End createContentPane
  237.    
  238.     public void actionPerformed(ActionEvent e) {
  239.         dimensionError = true;
  240.        
  241.         if (e.getSource() == rectangleRadioButton) {
  242.             type = 1;
  243.            
  244.             heightPanel.setVisible(true);
  245.             widthPanel.setVisible(true);
  246.             radiusPanel.setVisible(false);
  247.             lengthPanel.setVisible(false);
  248.             processButton.setVisible(true);
  249.            
  250.             // Change clear the text and change the color to white
  251.             heightTextField.setText("");
  252.             heightTextField.setBackground(Color.WHITE);
  253.             widthTextField.setText("");
  254.             widthTextField.setBackground(Color.WHITE);
  255.             lengthTextField.setText("");
  256.             lengthTextField.setBackground(Color.WHITE);
  257.             radiusTextField.setText("");
  258.             radiusTextField.setBackground(Color.WHITE);
  259.             totalGUI.repaint();
  260.            
  261.         }
  262.         else if (e.getSource() == boxRadioButton) {
  263.             type = 2;
  264.            
  265.             heightPanel.setVisible(true);
  266.             widthPanel.setVisible(true);
  267.             lengthPanel.setVisible(true);
  268.             radiusPanel.setVisible(false);
  269.             processButton.setVisible(true);
  270.            
  271.             heightTextField.setText("");
  272.             heightTextField.setBackground(Color.WHITE);
  273.             widthTextField.setText("");
  274.             widthTextField.setBackground(Color.WHITE);
  275.             lengthTextField.setText("");
  276.             lengthTextField.setBackground(Color.WHITE);
  277.             radiusTextField.setText("");
  278.             radiusTextField.setBackground(Color.WHITE);
  279.             totalGUI.repaint();
  280.            
  281.         }
  282.         else if (e.getSource() == circleRadioButton) {
  283.             type = 3;
  284.            
  285.             heightPanel.setVisible(false);
  286.             widthPanel.setVisible(false);
  287.             lengthPanel.setVisible(false);
  288.             radiusPanel.setVisible(true);
  289.             processButton.setVisible(true);
  290.            
  291.             heightTextField.setText("");
  292.             heightTextField.setBackground(Color.WHITE);
  293.             widthTextField.setText("");
  294.             widthTextField.setBackground(Color.WHITE);
  295.             lengthTextField.setText("");
  296.             lengthTextField.setBackground(Color.WHITE);
  297.             radiusTextField.setText("");
  298.             radiusTextField.setBackground(Color.WHITE);
  299.             totalGUI.repaint();
  300.            
  301.         }
  302.         else if (e.getSource() == cylinderRadioButton) {
  303.             type = 4;
  304.            
  305.             heightPanel.setVisible(false);
  306.             widthPanel.setVisible(false);
  307.             lengthPanel.setVisible(true);
  308.             radiusPanel.setVisible(true);
  309.             processButton.setVisible(true);
  310.            
  311.             heightTextField.setText("");
  312.             heightTextField.setBackground(Color.WHITE);
  313.             widthTextField.setText("");
  314.             widthTextField.setBackground(Color.WHITE);
  315.             lengthTextField.setText("");
  316.             lengthTextField.setBackground(Color.WHITE);
  317.             radiusTextField.setText("");
  318.             radiusTextField.setBackground(Color.WHITE);
  319.             totalGUI.repaint();
  320.            
  321.         }
  322.         else if(e.getSource() == processButton) {
  323.            
  324.             // Rectangle
  325.             if (rectangleRadioButton.isSelected()) {
  326.                 dimensionError = false;
  327.                 // Width
  328.                 try {
  329.                     width = Integer.parseInt(widthTextField.getText());
  330.                     LessThanOrGreaterThanException(" Width ", width);
  331.                    
  332.                     widthTextField.setBackground(Color.WHITE);
  333.                    
  334.                 }
  335.                 catch (LessThanOrGreaterThanException ex) {
  336.                     widthTextField.setBackground(Color.YELLOW);
  337.                     System.out.println("\n***** " + ex);
  338.                     dimensionError = true;
  339.                    
  340.                 }
  341.                 catch (InputMismatchException ex) {
  342.                     widthTextField.setBackground(Color.RED);
  343.                     System.out.println("\n***** " + ex);
  344.                     dimensionError = true;
  345.                    
  346.                 }
  347.                 catch (NumberFormatException ex) {
  348.                     widthTextField.setBackground(Color.RED);
  349.                     System.out.println("\n***** " + ex);
  350.                     dimensionError = true;
  351.                    
  352.                 }
  353.                
  354.                 // Height
  355.                 try {
  356.                     height = Integer.parseInt(heightTextField.getText());
  357.                     LessThanOrGreaterThanException(" Height ", height);
  358.                    
  359.                     heightTextField.setBackground(Color.WHITE);
  360.                    
  361.                 }
  362.                 catch (LessThanOrGreaterThanException ex) {
  363.                     heightTextField.setBackground(Color.YELLOW);
  364.                     System.out.println("\n***** " + ex);
  365.                     dimensionError = true;
  366.                    
  367.                 }
  368.                 catch (InputMismatchException ex) {
  369.                     heightTextField.setBackground(Color.RED);
  370.                     System.out.println("\n***** " + ex);
  371.                     dimensionError = true;
  372.                    
  373.                 }
  374.                 catch (NumberFormatException ex) {
  375.                     heightTextField.setBackground(Color.RED);
  376.                     System.out.println("\n***** " + ex);
  377.                     dimensionError = true;
  378.                    
  379.                 }
  380.             } // End Rectangle Button
  381.            
  382.             // Box
  383.             if (boxRadioButton.isSelected()) {
  384.                 dimensionError = false;
  385.                 // Width
  386.                 try {
  387.                     width = Integer.parseInt(widthTextField.getText());
  388.                     LessThanOrGreaterThanException(" Width ", width);
  389.                    
  390.                     widthTextField.setBackground(Color.WHITE);
  391.                    
  392.                 }
  393.                 catch (LessThanOrGreaterThanException ex) {
  394.                     widthTextField.setBackground(Color.YELLOW);
  395.                     System.out.println("\n***** " + ex);
  396.                     dimensionError = true;
  397.                    
  398.                 }
  399.                 catch (InputMismatchException ex) {
  400.                     widthTextField.setBackground(Color.RED);
  401.                     System.out.println("\n***** " + ex);
  402.                     dimensionError = true;
  403.                    
  404.                 }
  405.                 catch (NumberFormatException ex) {
  406.                     widthTextField.setBackground(Color.RED);
  407.                     System.out.println("\n***** " + ex);
  408.                     dimensionError = true;
  409.                    
  410.                 }
  411.                
  412.                 // Height
  413.                 try {
  414.                     height = Integer.parseInt(heightTextField.getText());
  415.                     LessThanOrGreaterThanException(" Height ", height);
  416.                    
  417.                     heightTextField.setBackground(Color.WHITE);
  418.                    
  419.                 }
  420.                 catch (LessThanOrGreaterThanException ex) {
  421.                     heightTextField.setBackground(Color.YELLOW);
  422.                     System.out.println("\n***** " + ex);
  423.                     dimensionError = true;
  424.                    
  425.                 }
  426.                 catch (InputMismatchException ex) {
  427.                     heightTextField.setBackground(Color.RED);
  428.                     System.out.println("\n***** " + ex);
  429.                     dimensionError = true;
  430.                    
  431.                 }
  432.                 catch (NumberFormatException ex) {
  433.                     heightTextField.setBackground(Color.RED);
  434.                     System.out.println("\n***** " + ex);
  435.                     dimensionError = true;
  436.                    
  437.                 }
  438.                
  439.                 // Length
  440.                 try {
  441.                     length = Integer.parseInt(lengthTextField.getText());
  442.                     LessThanOrGreaterThanException(" Length ", length);
  443.                    
  444.                     lengthTextField.setBackground(Color.WHITE);
  445.                    
  446.                 }
  447.                 catch (LessThanOrGreaterThanException ex) {
  448.                     lengthTextField.setBackground(Color.YELLOW);
  449.                     System.out.println("\n***** " + ex);
  450.                     dimensionError = true;
  451.                    
  452.                 }
  453.                 catch (InputMismatchException ex) {
  454.                     lengthTextField.setBackground(Color.RED);
  455.                     System.out.println("\n***** " + ex);
  456.                     dimensionError = true;
  457.                    
  458.                 }
  459.                 catch (NumberFormatException ex) {
  460.                     lengthTextField.setBackground(Color.RED);
  461.                     System.out.println("\n***** " + ex);
  462.                     dimensionError = true;
  463.                    
  464.                 }
  465.             } // End Box Button
  466.            
  467.             // Circle
  468.             if (circleRadioButton.isSelected()) {
  469.                 dimensionError = false;
  470.                 // Radius
  471.                 try {
  472.                     radius = Integer.parseInt(radiusTextField.getText());
  473.                     LessThanOrGreaterThanException(" Radius ", radius);
  474.                     radiusTextField.setBackground(Color.WHITE);
  475.                    
  476.                 }
  477.                 catch (LessThanOrGreaterThanException ex) {
  478.                     radiusTextField.setBackground(Color.YELLOW);
  479.                     System.out.println("\n***** " + ex);
  480.                     dimensionError = true;
  481.                    
  482.                 }
  483.                 catch (InputMismatchException ex) {
  484.                     radiusTextField.setBackground(Color.RED);
  485.                     System.out.println("\n***** " + ex);
  486.                     dimensionError = true;
  487.                    
  488.                 }
  489.                 catch (NumberFormatException ex) {
  490.                     radiusTextField.setBackground(Color.RED);
  491.                     System.out.println("\n***** " + ex);
  492.                     dimensionError = true;
  493.                    
  494.                 }
  495.             } // End Circle Button
  496.            
  497.             // Cylinder
  498.             if (cylinderRadioButton.isSelected()) {            
  499.                 dimensionError = false;
  500.                 // Radius
  501.                 try {
  502.                     radius = Integer.parseInt(radiusTextField.getText());
  503.                     LessThanOrGreaterThanException(" Radius ", radius);
  504.                     radiusTextField.setBackground(Color.WHITE);
  505.                    
  506.                 }
  507.                 catch (LessThanOrGreaterThanException ex) {
  508.                     radiusTextField.setBackground(Color.YELLOW);
  509.                     System.out.println("\n***** " + ex);
  510.                     dimensionError = true;
  511.                    
  512.                 }
  513.                 catch (InputMismatchException ex) {
  514.                     radiusTextField.setBackground(Color.RED);
  515.                     System.out.println("\n***** " + ex);
  516.                     dimensionError = true;
  517.                
  518.                 }
  519.                 catch (NumberFormatException ex) {
  520.                     radiusTextField.setBackground(Color.RED);
  521.                     System.out.println("\n***** " + ex);
  522.                     dimensionError = true;
  523.                    
  524.                 }
  525.                
  526.                 // Length
  527.                
  528.                 try {
  529.                     length = Integer.parseInt(lengthTextField.getText());
  530.                     LessThanOrGreaterThanException(" Length ", length);
  531.                     lengthTextField.setBackground(Color.WHITE);
  532.                     dimensionError = false;
  533.                    
  534.                 }
  535.                 catch (LessThanOrGreaterThanException ex) {
  536.                     lengthTextField.setBackground(Color.YELLOW);
  537.                     System.out.println("\n***** " + ex);
  538.                     dimensionError = true;
  539.                    
  540.                 }
  541.                 catch (InputMismatchException ex) {
  542.                     lengthTextField.setBackground(Color.RED);
  543.                     System.out.println("\n***** " + ex);
  544.                     dimensionError = true;
  545.                    
  546.                 }
  547.                 catch (NumberFormatException ex) {
  548.                     lengthTextField.setBackground(Color.RED);
  549.                     System.out.println("\n***** " + ex);
  550.                     dimensionError = true;
  551.                    
  552.                 }
  553.             } // End Cylinder Button
  554.            
  555.             if (dimensionError == false) {
  556.                 createAndShowFigure();
  557.             }
  558.         } // End process Button
  559.     } // End ActionPerformed
  560.    
  561.     private static void createAndShowFigure() {
  562.         JFrame frame2 = new JFrame("Lab 6 Figures");
  563.  
  564.         if (type == 1) { // Rectangle
  565.             Rectangle rectangle1 = new Rectangle(100, 200, width, height);
  566.             frame2.setContentPane(rectangle1.drawFigure());
  567.        
  568.         }  else if (type == 2) { // Box
  569.             Box box1 = new Box(100, 200, width, height, length);
  570.             frame2.setContentPane(box1.drawFigure());
  571.            
  572.         } else if (type == 3) { // Circle
  573.             Circle circle1 = new Circle(100, 200, radius);
  574.             frame2.setContentPane(circle1.drawFigure());
  575.         } else if (type == 4) {
  576.             Cylinder cylinder1 = new Cylinder(100, 200, radius, length);
  577.             frame2.setContentPane(cylinder1.drawFigure());
  578.         }
  579.        
  580.         frame2.setSize(700, 700);
  581.         frame2.setLocationRelativeTo(null);
  582.         frame2.setVisible(true);
  583.     }
  584.    
  585.     public static void createAndShowGUI() {
  586.         JFrame frame = new JFrame("Lab 5 GUI");
  587.         Lab6GUI lab5 = new Lab6GUI();
  588.  
  589.         frame.setContentPane(lab5.createContentPane());
  590.         frame.setLayout(null);
  591.         frame.setLocation(500, 450);
  592.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  593.         frame.setSize(600, 400);
  594.         frame.setLocationRelativeTo(null);
  595.         frame.setVisible(true);
  596.     }
  597.  
  598.     public static void main(String[] args) {
  599.         createAndShowGUI();
  600.     }
  601.    
  602.     public static void LessThanOrGreaterThanException(String myDimension, int myNumber)
  603.         throws LessThanOrGreaterThanException {
  604.        
  605.             if (myDimension == " Width ") {
  606.                 if (myNumber < minWidth || myNumber > maxWidth) throw new LessThanOrGreaterThanException(myDimension, myNumber, minWidth, maxWidth);
  607.            
  608.             } else if (myDimension == " Height ") {
  609.                 if (myNumber < minHeight || myNumber > maxHeight) throw new LessThanOrGreaterThanException(myDimension, myNumber, minHeight, maxHeight);
  610.                
  611.             } else if (myDimension == " Length ") {
  612.                 if (myNumber < minLength || myNumber > maxLength) throw new LessThanOrGreaterThanException(myDimension, myNumber, minLength, maxLength);
  613.                
  614.             } else if (myDimension == " Radius ") {
  615.                 if (myNumber < minRadius || myNumber > maxRadius) throw new LessThanOrGreaterThanException(myDimension, myNumber, minRadius, maxRadius);
  616.             }
  617.            
  618.     }
  619.    
  620.     public static void HeightLessThanOrGreaterThanException() {
  621.        
  622.     }
  623. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement