Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.InputMismatchException;
- import javax.swing.ButtonGroup;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JRadioButton;
- import javax.swing.JTextField;
- public class Lab6GUI implements ActionListener {
- static int width;
- static int height;
- static int radius;
- static int length;
- static int maxWidth = 300;
- static int maxHeight = 300;
- static int maxLength = 300;
- static int maxRadius = 400;
- static int minWidth = 50;
- static int minHeight = 50;
- static int minLength = 50;
- static int minRadius = 50;
- static boolean dimensionError;
- static int type;
- JLabel titleLabel;
- JLabel radioLabel;
- JLabel widthLabel;
- JLabel heightLabel;
- JLabel radiusLabel;
- JLabel lengthLabel;
- JRadioButton rectangleRadioButton;
- JRadioButton boxRadioButton;
- JRadioButton circleRadioButton;
- JRadioButton cylinderRadioButton;
- ButtonGroup radioButtonGroup;
- JTextField widthTextField;
- JTextField lengthTextField;
- JTextField heightTextField;
- JTextField radiusTextField;
- JPanel radioButtonPanel;
- JPanel widthPanel;
- JPanel lengthPanel;
- JPanel heightPanel;
- JPanel radiusPanel;
- JPanel totalGUI;
- JButton processButton;
- public JPanel createContentPane() {
- Font titleFont = new Font("Verdana", Font.BOLD, 26);
- totalGUI = new JPanel();
- totalGUI.setBackground(Color.DARK_GRAY);
- totalGUI.setLayout(null);
- titleLabel = new JLabel("The Figure Center");
- titleLabel.setLocation(170, 20);
- titleLabel.setSize(350, 100);
- titleLabel.setForeground(Color.WHITE);
- titleLabel.setFont(titleFont);
- totalGUI.add(titleLabel);
- // Radio Button Panel
- radioButtonPanel = new JPanel();
- radioButtonPanel.setBackground(null);
- radioButtonPanel.setLayout(null);
- radioButtonPanel.setSize(130, 120);
- radioButtonPanel.setLocation(90, 125);
- radioLabel = new JLabel("Select a figure.");
- radioLabel.setLocation(0, -3);
- radioLabel.setSize(100, 30);
- radioLabel.setForeground(Color.white);
- radioButtonPanel.add(radioLabel);
- rectangleRadioButton = new JRadioButton("Rectangle");
- rectangleRadioButton.setLocation(0, 20);
- rectangleRadioButton.setSize(100, 30);
- rectangleRadioButton.setBackground(null);
- rectangleRadioButton.setForeground(Color.white);
- rectangleRadioButton.setSelected(false);
- rectangleRadioButton.addActionListener(this);
- radioButtonPanel.add(rectangleRadioButton);
- boxRadioButton = new JRadioButton("Box");
- boxRadioButton.setLocation(0, 45);
- boxRadioButton.setSize(100, 25);
- boxRadioButton.setBackground(null);
- boxRadioButton.setForeground(Color.white);
- boxRadioButton.addActionListener(this);
- radioButtonPanel.add(boxRadioButton);
- circleRadioButton = new JRadioButton("Circle");
- circleRadioButton.setLocation(0, 67);
- circleRadioButton.setSize(100, 25);
- circleRadioButton.setBackground(null);
- circleRadioButton.setForeground(Color.WHITE);
- circleRadioButton.setSelected(false);
- circleRadioButton.addActionListener(this);
- radioButtonPanel.add(circleRadioButton);
- cylinderRadioButton = new JRadioButton("Cylinder");
- cylinderRadioButton.setLocation(0, 90);
- cylinderRadioButton.setSize(100, 25);
- cylinderRadioButton.setBackground(null);
- cylinderRadioButton.setForeground(Color.white);
- cylinderRadioButton.setSelected(false);
- cylinderRadioButton.addActionListener(this);
- radioButtonPanel.add(cylinderRadioButton);
- radioButtonGroup = new ButtonGroup();
- radioButtonGroup.add(rectangleRadioButton);
- radioButtonGroup.add(boxRadioButton);
- radioButtonGroup.add(circleRadioButton);
- radioButtonGroup.add(cylinderRadioButton);
- totalGUI.add(radioButtonPanel);
- // Width Panel
- widthPanel = new JPanel();
- widthPanel.setBackground(null);
- widthPanel.setLayout(null);
- widthPanel.setSize(70, 50);
- widthPanel.setLocation(250, 125);
- widthLabel = new JLabel("Enter Width");
- widthLabel.setBackground(null);
- widthLabel.setForeground(Color.WHITE);
- widthLabel.setLocation(0, 0);
- widthLabel.setSize(70, 25);
- widthPanel.add(widthLabel);
- widthTextField = new JTextField();
- widthTextField.setLocation(0, 25);
- widthTextField.setSize(70, 20);
- widthPanel.add(widthTextField);
- totalGUI.add(widthPanel);
- // Radius Panel
- radiusPanel = new JPanel();
- radiusPanel.setBackground(null);
- radiusPanel.setLayout(null);
- radiusPanel.setSize(75, 50);
- radiusPanel.setLocation(250, 185);
- radiusLabel = new JLabel("Enter Radius");
- radiusLabel.setBackground(null);
- radiusLabel.setForeground(Color.WHITE);
- radiusLabel.setSize(75, 25);
- radiusLabel.setLocation(0, 0);
- radiusPanel.add(radiusLabel);
- radiusTextField = new JTextField();
- radiusTextField.setLocation(0, 25);
- radiusTextField.setSize(70, 20);
- radiusPanel.add(radiusTextField);
- totalGUI.add(radiusPanel);
- // Length Panel
- lengthPanel = new JPanel();
- lengthPanel.setBackground(null);
- lengthPanel.setLayout(null);
- lengthPanel.setSize(75, 50);
- lengthPanel.setLocation(400, 125);
- lengthLabel = new JLabel("Enter Length");
- lengthLabel.setForeground(Color.WHITE);
- lengthLabel.setSize(75, 25);
- lengthLabel.setLocation(0, 0);
- lengthPanel.add(lengthLabel);
- lengthTextField = new JTextField();
- lengthTextField.setSize(75, 20);
- lengthTextField.setLocation(0, 25);
- lengthPanel.add(lengthTextField);
- totalGUI.add(lengthPanel);
- // Height Panel
- heightPanel = new JPanel();
- heightPanel.setBackground(null);
- heightPanel.setLayout(null);
- heightPanel.setSize(75, 50);
- heightPanel.setLocation(400, 185);
- heightLabel = new JLabel("Enter Height");
- heightLabel.setForeground(Color.WHITE);
- heightLabel.setSize(75, 25);
- heightLabel.setLocation(0, 0);
- heightPanel.add(heightLabel);
- heightTextField = new JTextField();
- heightTextField.setSize(75, 20);
- heightTextField.setLocation(0, 25);
- heightPanel.add(heightTextField);
- totalGUI.add(heightPanel);
- // Process Button
- processButton = new JButton("Click to Process");
- processButton.setLocation(270, 260);
- processButton.setSize(185, 50);
- processButton.addActionListener(this);
- totalGUI.add(processButton);
- // Make the panel invisible
- heightPanel.setVisible(false);
- lengthPanel.setVisible(false);
- radiusPanel.setVisible(false);
- widthPanel.setVisible(false);
- processButton.setVisible(false);
- return totalGUI;
- } // End createContentPane
- public void actionPerformed(ActionEvent e) {
- dimensionError = true;
- if (e.getSource() == rectangleRadioButton) {
- type = 1;
- heightPanel.setVisible(true);
- widthPanel.setVisible(true);
- radiusPanel.setVisible(false);
- lengthPanel.setVisible(false);
- processButton.setVisible(true);
- // Change clear the text and change the color to white
- heightTextField.setText("");
- heightTextField.setBackground(Color.WHITE);
- widthTextField.setText("");
- widthTextField.setBackground(Color.WHITE);
- lengthTextField.setText("");
- lengthTextField.setBackground(Color.WHITE);
- radiusTextField.setText("");
- radiusTextField.setBackground(Color.WHITE);
- totalGUI.repaint();
- }
- else if (e.getSource() == boxRadioButton) {
- type = 2;
- heightPanel.setVisible(true);
- widthPanel.setVisible(true);
- lengthPanel.setVisible(true);
- radiusPanel.setVisible(false);
- processButton.setVisible(true);
- heightTextField.setText("");
- heightTextField.setBackground(Color.WHITE);
- widthTextField.setText("");
- widthTextField.setBackground(Color.WHITE);
- lengthTextField.setText("");
- lengthTextField.setBackground(Color.WHITE);
- radiusTextField.setText("");
- radiusTextField.setBackground(Color.WHITE);
- totalGUI.repaint();
- }
- else if (e.getSource() == circleRadioButton) {
- type = 3;
- heightPanel.setVisible(false);
- widthPanel.setVisible(false);
- lengthPanel.setVisible(false);
- radiusPanel.setVisible(true);
- processButton.setVisible(true);
- heightTextField.setText("");
- heightTextField.setBackground(Color.WHITE);
- widthTextField.setText("");
- widthTextField.setBackground(Color.WHITE);
- lengthTextField.setText("");
- lengthTextField.setBackground(Color.WHITE);
- radiusTextField.setText("");
- radiusTextField.setBackground(Color.WHITE);
- totalGUI.repaint();
- }
- else if (e.getSource() == cylinderRadioButton) {
- type = 4;
- heightPanel.setVisible(false);
- widthPanel.setVisible(false);
- lengthPanel.setVisible(true);
- radiusPanel.setVisible(true);
- processButton.setVisible(true);
- heightTextField.setText("");
- heightTextField.setBackground(Color.WHITE);
- widthTextField.setText("");
- widthTextField.setBackground(Color.WHITE);
- lengthTextField.setText("");
- lengthTextField.setBackground(Color.WHITE);
- radiusTextField.setText("");
- radiusTextField.setBackground(Color.WHITE);
- totalGUI.repaint();
- }
- else if(e.getSource() == processButton) {
- // Rectangle
- if (rectangleRadioButton.isSelected()) {
- dimensionError = false;
- // Width
- try {
- width = Integer.parseInt(widthTextField.getText());
- LessThanOrGreaterThanException(" Width ", width);
- widthTextField.setBackground(Color.WHITE);
- }
- catch (LessThanOrGreaterThanException ex) {
- widthTextField.setBackground(Color.YELLOW);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (InputMismatchException ex) {
- widthTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (NumberFormatException ex) {
- widthTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- // Height
- try {
- height = Integer.parseInt(heightTextField.getText());
- LessThanOrGreaterThanException(" Height ", height);
- heightTextField.setBackground(Color.WHITE);
- }
- catch (LessThanOrGreaterThanException ex) {
- heightTextField.setBackground(Color.YELLOW);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (InputMismatchException ex) {
- heightTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (NumberFormatException ex) {
- heightTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- } // End Rectangle Button
- // Box
- if (boxRadioButton.isSelected()) {
- dimensionError = false;
- // Width
- try {
- width = Integer.parseInt(widthTextField.getText());
- LessThanOrGreaterThanException(" Width ", width);
- widthTextField.setBackground(Color.WHITE);
- }
- catch (LessThanOrGreaterThanException ex) {
- widthTextField.setBackground(Color.YELLOW);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (InputMismatchException ex) {
- widthTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (NumberFormatException ex) {
- widthTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- // Height
- try {
- height = Integer.parseInt(heightTextField.getText());
- LessThanOrGreaterThanException(" Height ", height);
- heightTextField.setBackground(Color.WHITE);
- }
- catch (LessThanOrGreaterThanException ex) {
- heightTextField.setBackground(Color.YELLOW);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (InputMismatchException ex) {
- heightTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (NumberFormatException ex) {
- heightTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- // Length
- try {
- length = Integer.parseInt(lengthTextField.getText());
- LessThanOrGreaterThanException(" Length ", length);
- lengthTextField.setBackground(Color.WHITE);
- }
- catch (LessThanOrGreaterThanException ex) {
- lengthTextField.setBackground(Color.YELLOW);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (InputMismatchException ex) {
- lengthTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (NumberFormatException ex) {
- lengthTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- } // End Box Button
- // Circle
- if (circleRadioButton.isSelected()) {
- dimensionError = false;
- // Radius
- try {
- radius = Integer.parseInt(radiusTextField.getText());
- LessThanOrGreaterThanException(" Radius ", radius);
- radiusTextField.setBackground(Color.WHITE);
- }
- catch (LessThanOrGreaterThanException ex) {
- radiusTextField.setBackground(Color.YELLOW);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (InputMismatchException ex) {
- radiusTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (NumberFormatException ex) {
- radiusTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- } // End Circle Button
- // Cylinder
- if (cylinderRadioButton.isSelected()) {
- dimensionError = false;
- // Radius
- try {
- radius = Integer.parseInt(radiusTextField.getText());
- LessThanOrGreaterThanException(" Radius ", radius);
- radiusTextField.setBackground(Color.WHITE);
- }
- catch (LessThanOrGreaterThanException ex) {
- radiusTextField.setBackground(Color.YELLOW);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (InputMismatchException ex) {
- radiusTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (NumberFormatException ex) {
- radiusTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- // Length
- try {
- length = Integer.parseInt(lengthTextField.getText());
- LessThanOrGreaterThanException(" Length ", length);
- lengthTextField.setBackground(Color.WHITE);
- dimensionError = false;
- }
- catch (LessThanOrGreaterThanException ex) {
- lengthTextField.setBackground(Color.YELLOW);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (InputMismatchException ex) {
- lengthTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- catch (NumberFormatException ex) {
- lengthTextField.setBackground(Color.RED);
- System.out.println("\n***** " + ex);
- dimensionError = true;
- }
- } // End Cylinder Button
- if (dimensionError == false) {
- createAndShowFigure();
- }
- } // End process Button
- } // End ActionPerformed
- private static void createAndShowFigure() {
- JFrame frame2 = new JFrame("Lab 6 Figures");
- if (type == 1) { // Rectangle
- Rectangle rectangle1 = new Rectangle(100, 200, width, height);
- frame2.setContentPane(rectangle1.drawFigure());
- } else if (type == 2) { // Box
- Box box1 = new Box(100, 200, width, height, length);
- frame2.setContentPane(box1.drawFigure());
- } else if (type == 3) { // Circle
- Circle circle1 = new Circle(100, 200, radius);
- frame2.setContentPane(circle1.drawFigure());
- } else if (type == 4) {
- Cylinder cylinder1 = new Cylinder(100, 200, radius, length);
- frame2.setContentPane(cylinder1.drawFigure());
- }
- frame2.setSize(700, 700);
- frame2.setLocationRelativeTo(null);
- frame2.setVisible(true);
- }
- public static void createAndShowGUI() {
- JFrame frame = new JFrame("Lab 5 GUI");
- Lab6GUI lab5 = new Lab6GUI();
- frame.setContentPane(lab5.createContentPane());
- frame.setLayout(null);
- frame.setLocation(500, 450);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(600, 400);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- createAndShowGUI();
- }
- public static void LessThanOrGreaterThanException(String myDimension, int myNumber)
- throws LessThanOrGreaterThanException {
- if (myDimension == " Width ") {
- if (myNumber < minWidth || myNumber > maxWidth) throw new LessThanOrGreaterThanException(myDimension, myNumber, minWidth, maxWidth);
- } else if (myDimension == " Height ") {
- if (myNumber < minHeight || myNumber > maxHeight) throw new LessThanOrGreaterThanException(myDimension, myNumber, minHeight, maxHeight);
- } else if (myDimension == " Length ") {
- if (myNumber < minLength || myNumber > maxLength) throw new LessThanOrGreaterThanException(myDimension, myNumber, minLength, maxLength);
- } else if (myDimension == " Radius ") {
- if (myNumber < minRadius || myNumber > maxRadius) throw new LessThanOrGreaterThanException(myDimension, myNumber, minRadius, maxRadius);
- }
- }
- public static void HeightLessThanOrGreaterThanException() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement