Results 1 to 4 of 4
  1. #1
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Geometric Calculator

    Hey guys, I'm designing a geometric calculator. Please check it out and try editing it or suggest more futures for it, coz I'm out of ideas.
    Find an attached source folder containing the images and source code.

    Java Code:
    package calculator;
    
    
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.text.DecimalFormat;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    /*
     * Geometric Calculator
     * @author Bello Danlami Kankia
     * 6/12/2011
     *
     */
    public class Calculator extends JApplet {
    
        private JPanel shapeSelectionPanel;
        private JPanel formularDisplayPanel;
        private JPanel logicPanel;
        private JPanel displayPanel;
        private JComboBox shapesComboBox;
        private JLabel selectedShapeLabel;
        private JLabel selectedShapeFormularLabel;
        private JLabel aLabel;
        private JLabel bLabel;
        private JLabel cLabel;
        private JLabel dLabel;
        private JLabel hLabel;
        private JLabel lLabel;
        private JLabel sLabel;
        private JLabel wLabel;
        private JLabel theetaLabel;
        private JLabel radiusLabel;
        private JTextArea resultTextArea;
        private JTextField aTextField;
        private JTextField bTextField;
        private JTextField cTextField;
        private JTextField dTextField;
        private JTextField hTextField;
        private JTextField lTextField;
        private JTextField sTextField;
        private JTextField wTextField;
        private JTextField theetaTextField;
        private JTextField radiusTextField;
        private JButton clearButton;
        private JButton enterButton;
        private JButton quitButton;
    										 
    								 // creating an array called "shapeNames" that stores the files name 		  
        private String[] shapeNames = {	"Sector.gif", "Ellipse.gif","Trapezium.gif", 
        								"Parallelogram.gif", "Cylinder.gif",
        								"Square Based Pyramid.gif", "Cone.gif", "Sphere.gif", "Cube.gif",
    								    "Rectangular Prism.gif"};
        
        
        // creating an array called "formulae" that stores the icon image of the formulas 
        private String[] formulae = {  "fSector.gif", "fEllipse.gif", "fTrapezium.gif",
        							   "fParallelogram.gif", "fCylinder.gif", "fSquareBasedPyramid.gif",
        							   "fCone.gif", "fSphere.gif", "fCube.gif", "fRectangularPrism.gif"};
        
        //creating an array of objects that will store the shape's icon files
        private Icon[] icons = {
                new ImageIcon(getClass().getResource(shapeNames[0])),
                new ImageIcon(getClass().getResource(shapeNames[1])),
                new ImageIcon(getClass().getResource(shapeNames[2])),
                new ImageIcon(getClass().getResource(shapeNames[3])),
                new ImageIcon(getClass().getResource(shapeNames[4])),
                new ImageIcon(getClass().getResource(shapeNames[5])),
                new ImageIcon(getClass().getResource(shapeNames[6])),
                new ImageIcon(getClass().getResource(shapeNames[7])),
                new ImageIcon(getClass().getResource(shapeNames[8])),
                new ImageIcon(getClass().getResource(shapeNames[9]))};
            
        //creating an array of objects that will store the formula's icon files
        private Icon[] formularIcons = {
                new ImageIcon(getClass().getResource(formulae[0])),
                new ImageIcon(getClass().getResource(formulae[1])),
                new ImageIcon(getClass().getResource(formulae[2])),
                new ImageIcon(getClass().getResource(formulae[3])),
                new ImageIcon(getClass().getResource(formulae[4])),
                new ImageIcon(getClass().getResource(formulae[5])),
                new ImageIcon(getClass().getResource(formulae[6])),
                new ImageIcon(getClass().getResource(formulae[7])),
                new ImageIcon(getClass().getResource(formulae[8])),
                new ImageIcon(getClass().getResource(formulae[9]))};
     
        
        public void init(){
    			//setting the size of the applet
    			    setSize(500,500);
    			  //making the following text fields uneditable at the start of the applet.    
    			     aTextField.setEditable(false);
                     bTextField.setEditable(false);
                     cTextField.setEditable(false);
                     dTextField.setEditable(false);
                     hTextField.setEditable(false);
                     lTextField.setEditable(false);
                     sTextField.setEditable(false);
                     wTextField.setEditable(false);
        	}
        		
    
        public Calculator() {
        					//Creating the panels 
       							shapeSelectionPanel = new JPanel();
        						formularDisplayPanel = new JPanel();
        						logicPanel = new JPanel(new GridLayout());
        						displayPanel = new JPanel(new GridLayout(2, 1));
    
        						/*creating  a drop down box that will take the list of options 
        						in the shapeNames string below */
        						shapesComboBox = new JComboBox(shapeNames);
        						shapeSelectionPanel.add(shapesComboBox);
            
        					    //creating the labels that will display the images
        						selectedShapeLabel = new JLabel(icons[0]);
        					    selectedShapeFormularLabel = new JLabel(formularIcons[0]);
            
    
        					    shapesComboBox.addItemListener(new ItemListener() {
                @Override
            	/*creating the Itemlistener and putting a Unanimous
    			inner class that implements the item listener class   
    			*/
                public void itemStateChanged(ItemEvent e) {
                    	if (e.getStateChange() == ItemEvent.SELECTED) {
                        selectedShapeLabel.setIcon(icons[shapesComboBox.getSelectedIndex()]);
                        selectedShapeFormularLabel.setIcon(formularIcons[shapesComboBox.getSelectedIndex()]);
                    }
                    int index = shapesComboBox.getSelectedIndex();
                    reset(); //restore original textboxes states.
                    switch (index) {
                        case 0:
                            aTextField.setEditable(false);
                            bTextField.setEditable(false);
                            cTextField.setEditable(false);
                            dTextField.setEditable(false);
                            hTextField.setEditable(false);
                            lTextField.setEditable(false);
                            sTextField.setEditable(false);
                            wTextField.setEditable(false);
                            break;
                        case 1:
                            radiusTextField.setEditable(false);
                            theetaTextField.setEditable(false);
                            cTextField.setEditable(false);
                            dTextField.setEditable(false);
                            hTextField.setEditable(false);
                            lTextField.setEditable(false);
                            sTextField.setEditable(false);
                            wTextField.setEditable(false);
                            break;
                        case 2:
                            lTextField.setEditable(false);
                            sTextField.setEditable(false);
                            wTextField.setEditable(false);
                            radiusTextField.setEditable(false);
                            theetaTextField.setEditable(false);
                            break;
                        case 3:
                            aTextField.setEditable(false);
                            dTextField.setEditable(false);
                            lTextField.setEditable(false);
                            sTextField.setEditable(false);
                            wTextField.setEditable(false);
                            radiusTextField.setEditable(false);
                            theetaTextField.setEditable(false);
                            break;
                        case 4:
                            aTextField.setEditable(false);
                            bTextField.setEditable(false);
                            cTextField.setEditable(false);
                            dTextField.setEditable(false);
                            lTextField.setEditable(false);
                            sTextField.setEditable(false);
                            wTextField.setEditable(false);
                            theetaTextField.setEditable(false);
                            break;
                        case 5:
                            aTextField.setEditable(false);
                            cTextField.setEditable(false);
                            dTextField.setEditable(false);
                            lTextField.setEditable(false);
                            wTextField.setEditable(false);
                            radiusTextField.setEditable(false);
                            theetaTextField.setEditable(false);
                            break;
                        case 6:
                            aTextField.setEditable(false);
                            bTextField.setEditable(false);
                            cTextField.setEditable(false);
                            dTextField.setEditable(false);
                            lTextField.setEditable(false);
                            wTextField.setEditable(false);
                            theetaTextField.setEditable(false);
                            break;
                        case 7:
                            aTextField.setEditable(false);
                            bTextField.setEditable(false);
                            cTextField.setEditable(false);
                            dTextField.setEditable(false);
                            hTextField.setEditable(false);
                            lTextField.setEditable(false);
                            sTextField.setEditable(false);
                            wTextField.setEditable(false);
                            theetaTextField.setEditable(false);
                            break;
                        case 8:
                            aTextField.setEditable(false);
                            bTextField.setEditable(false);
                            cTextField.setEditable(false);
                            dTextField.setEditable(false);
                            sTextField.setEditable(false);
                            radiusTextField.setEditable(false);
                            theetaTextField.setEditable(false);
                            break;
                        case 9:
                            aTextField.setEditable(false);
                            bTextField.setEditable(false);
                            cTextField.setEditable(false);
                            dTextField.setEditable(false);
                            sTextField.setEditable(false);
                            radiusTextField.setEditable(false);
                            theetaTextField.setEditable(false);
                            break;
                    }
                }
    
                private void reset() {
                    aTextField.setEditable(true);
                    aTextField.setText("");
                    bTextField.setEditable(true);
                    bTextField.setText("");
                    cTextField.setEditable(true);
                    cTextField.setText("");
                    dTextField.setEditable(true);
                    dTextField.setText("");
                    hTextField.setEditable(true);
                    hTextField.setText("");
                    lTextField.setEditable(true);
                    lTextField.setText("");
                    sTextField.setEditable(true);
                    sTextField.setText("");
                    wTextField.setEditable(true);
                    wTextField.setText("");
                    radiusTextField.setEditable(true);
                    radiusTextField.setText("");
                    theetaTextField.setEditable(true);
                    theetaTextField.setText("");
                    
                }
            });
        						
            shapeSelectionPanel.add(selectedShapeLabel);
            formularDisplayPanel.add(selectedShapeFormularLabel);
    
            Font displayFont = new Font("Arial", Font.BOLD, 16); // create font for display of answer
            resultTextArea = new JTextArea();
            resultTextArea.setFont(displayFont);
            resultTextArea.setLineWrap(true);
           
            //resultLabel.setBorder(BorderFactory.createLoweredBevelBorder());
            resultTextArea.setSize(56, 25);
            displayPanel.add(resultTextArea);
            
            //giving the labels visible parameters and aligning them to the right
            aLabel = new JLabel("a :  ", JLabel.RIGHT);
            bLabel = new JLabel("b :  ", JLabel.RIGHT);
            cLabel = new JLabel("c :  ", JLabel.RIGHT);
            dLabel = new JLabel("d :  ", JLabel.RIGHT);
            hLabel = new JLabel("h :  ", JLabel.RIGHT);
            lLabel = new JLabel("l :  ", JLabel.RIGHT);
            sLabel = new JLabel("s :  ", JLabel.RIGHT);
            wLabel = new JLabel("w :  ", JLabel.RIGHT);
            theetaLabel = new JLabel("Angle of radians :  ", JLabel.RIGHT);
            radiusLabel = new JLabel("r :  ", JLabel.RIGHT);
    
            JPanel labelsPanel = new JPanel(new GridLayout(10, 2));
    
            aTextField = new JTextField(12);
            bTextField = new JTextField(12);
            cTextField = new JTextField(12);
            dTextField = new JTextField(12);
            hTextField = new JTextField(12);
            lTextField = new JTextField(12);
            sTextField = new JTextField(12);
            wTextField = new JTextField(12);
            theetaTextField = new JTextField(12);
            radiusTextField = new JTextField(12);
    
            labelsPanel.add(aLabel);
            labelsPanel.add(aTextField);
            labelsPanel.add(bLabel);
            labelsPanel.add(bTextField);
            labelsPanel.add(cLabel);
            labelsPanel.add(cTextField);
            labelsPanel.add(dLabel);
            labelsPanel.add(dTextField);
            labelsPanel.add(hLabel);
            labelsPanel.add(hTextField);
            labelsPanel.add(lLabel);
            labelsPanel.add(lTextField);
            labelsPanel.add(sLabel);
            labelsPanel.add(sTextField);
            labelsPanel.add(wLabel);
            labelsPanel.add(wTextField);
            labelsPanel.add(theetaLabel);
            labelsPanel.add(theetaTextField);
            labelsPanel.add(radiusLabel);
            labelsPanel.add(radiusTextField);
    
            JPanel labelsAndTextBoxesPanel = new JPanel(new GridLayout(1, 2));
            labelsAndTextBoxesPanel.add(labelsPanel);
            //labelsAndTextBoxesPanel.add(textBoxesPanel);
    
            logicPanel.add(labelsAndTextBoxesPanel);
    
            //Creating three buttons
            clearButton = new JButton("Clear");
            enterButton = new JButton("Enter");
            quitButton = new JButton("Quit");
        								
            enterButton.addActionListener(new ComputeActionListener());
            clearButton.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    clearFields();
                }
            });
            quitButton.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
    
            //Adding the buttons to the panel
            JPanel buttonsPanel = new JPanel(new FlowLayout());
            buttonsPanel.add(clearButton);
            buttonsPanel.add(enterButton);
            buttonsPanel.add(quitButton);
    
            JPanel logicAndButtonsPanels = new JPanel(new GridLayout(2, 1));
            logicAndButtonsPanels.add(logicPanel);
            logicAndButtonsPanels.add(buttonsPanel);
    
            JPanel topPanel = new JPanel(new GridLayout(1, 2));
            topPanel.add(shapeSelectionPanel);
            topPanel.add(formularDisplayPanel);
    
            JPanel bottomPanel = new JPanel(new GridLayout(1, 2));
            bottomPanel.add(logicAndButtonsPanels);
            bottomPanel.add(displayPanel);
    
            //Creating the main panel and adding some panels to it
            JPanel  mainPanel = new JPanel();
            mainPanel.setLayout(new GridLayout(2, 1));
            mainPanel.add(topPanel);
            mainPanel.add(bottomPanel);
            mainPanel.setSize(500, 700);
            mainPanel.setVisible(true);
            add(mainPanel);
        }
    
        //Creating a method that clears the text fields
        private void clearFields() {
            aTextField.setText("");
            bTextField.setText("");
            cTextField.setText("");
            dTextField.setText("");
            hTextField.setText("");
            lTextField.setText("");
            sTextField.setText("");
            wTextField.setText("");
            radiusTextField.setText("");
            theetaTextField.setText("");
            resultTextArea.setText("");
    
        }
    
        //Inner class
        private class ComputeActionListener implements ActionListener {
    
        	//setting the format of the answer    
            DecimalFormat df = new DecimalFormat("0.00");
    
            public ComputeActionListener() {
            }
    
            @Override
            
            public void actionPerformed(ActionEvent e) {
                try {
                    int index = shapesComboBox.getSelectedIndex();
                    switch (index) {
                        case 0:
                            sectorComputation();
                            break;
                        case 1:
                            ellipseComputation();
                            break;
                        case 2:
                            trapeziumComputation();
                            break;
                        case 3:
                            parallelogramComputation();
                            break;
                        case 4:
                            cylinderComputation();
                            break;
                        case 5:
                            squareBasedPyramidComputation();
                            break;
                        case 6:
                            coneComputation();
                            break;
                        case 7:
                            sphereComputation();
                            break;
                        case 8:
                            cubeComputation();
                            break;
                        case 9:
                            rectangularPrismComputation();
                            break;
                    }
                    //catching exceptions
                } catch (ArithmeticException ex) {
                    resultTextArea.setText("Input Error!!!! Pls re-enter the value ");
                    ex.printStackTrace();
                } catch (NumberFormatException ex) {
                    resultTextArea.setText("Input Error!!!! Pls re-enter the value ");
                    ex.printStackTrace();
                } catch (Exception ex) {
                	
                }
            }
    
            private void sectorComputation() throws ArithmeticException,
                    NumberFormatException, Exception {
                String stringRadius = radiusTextField.getText(); // get the radius in as a string first
                String stringAngle = theetaTextField.getText();
    
                double radius = Double.parseDouble(stringRadius); // convert to double
                double theeta = Double.parseDouble(stringAngle);
                double area = 0.5 * Math.pow(radius, 2) * Math.toRadians(theeta);//convert Q to radians first.
    
                resultTextArea.setText("Area = " + df.format(area));
            }
    
            private void ellipseComputation() throws ArithmeticException,
                  NumberFormatException, Exception {
                String aString = aTextField.getText();
                String bString = bTextField.getText();
    
                double a = Double.parseDouble(aString);
                double b = Double.parseDouble(bString);
    
                double area =  Math.PI * a *b ;
                resultTextArea.setText("Area = " + df.format(area));
            }
    
            private void trapeziumComputation() throws ArithmeticException,
                    NumberFormatException, Exception {
                String aString = aTextField.getText();
                String bString = bTextField.getText();
                String cString = cTextField.getText();
                String dString = dTextField.getText();
                String hString = hTextField.getText();
    
                double a = Double.parseDouble(aString);
                double b = Double.parseDouble(bString);
                double c = Double.parseDouble(cString);
                double d = Double.parseDouble(dString);
                double h = Double.parseDouble(hString);
    
                double area = 0.5 * (a + b) * h;
                double perimeter = a + b + c + d;
    
                resultTextArea.setText("Area = " + df.format(area)
                        + "\n" + " Perimeter = " + df.format(perimeter));
            }
    
            private void parallelogramComputation() throws ArithmeticException,
                    NumberFormatException, Exception {
                String bString = bTextField.getText();
                String hString = hTextField.getText();
                String cString = cTextField.getText();
    
                double b = Double.parseDouble(bString);
                double h = Double.parseDouble(hString);
                double c = Double.parseDouble(cString);
    
                double area = b * h;
                double perimeter = 2 * b + 2 * c;
    
                resultTextArea.setText("Area = " + df.format(area)
                        + "\n" + " Perimeter = " + df.format(perimeter));
            }
    
            private void cylinderComputation() throws ArithmeticException,
                    NumberFormatException, Exception {
                
                String rString = radiusTextField.getText();
                String hString = hTextField.getText();
                double r = Double.parseDouble(rString);
                double h = Double.parseDouble(hString);
    
                double area = (2 * Math.PI * r * h) + (2 * Math.PI * Math.pow(r, 2));
                double volume = Math.PI * Math.pow(r, 2) * h;
    
                resultTextArea.setText("Area = " + df.format(area)
                        + "\n" + " Volume = " + df.format(volume));
            }
    
            private void squareBasedPyramidComputation() throws ArithmeticException,
                    NumberFormatException, Exception {
    
                String bString = bTextField.getText();
                String sString = sTextField.getText();
                String hString = hTextField.getText();
    
    
                double b = Double.parseDouble(bString);
                double h = Double.parseDouble(hString);
                double s = Double.parseDouble(sString);
    
                double surfaceArea = ((2*b*s) + (b*b));
                double volume = 0.3333 * Math.pow(b, 2) * h;
    
                resultTextArea.setText("Surface Area = " + df.format(surfaceArea)
                        + "\n" + " Volume = " + df.format(volume));
            }
    
            private void coneComputation() throws ArithmeticException,
                    NumberFormatException, Exception {
                String rString = radiusTextField.getText();
                String sString = sTextField.getText();
                String hString = hTextField.getText();
    
    
                double r = Double.parseDouble(rString);
                double h = Double.parseDouble(hString);
                double s = Double.parseDouble(sString);
    
                double surfaceArea = (Math.PI * r * s )+ (Math.PI * Math.pow(r, 2));
                double volume = 0.3333 * Math.PI * Math.pow(r, 2) * h;
               
                resultTextArea.setText("Surface Area = " + df.format(surfaceArea)
                        + "\n" + " Volume = " + df.format(volume));
            }
    
            private void sphereComputation() throws ArithmeticException,
                    NumberFormatException, Exception {
                String rString = radiusTextField.getText();
                double r = Double.parseDouble(rString);
    
                double surfaceArea = 4 * Math.PI * Math.pow(r, 2);
                double volume = 1.3333 * Math.PI * Math.pow(r, 3);
    
                resultTextArea.setText("Surface Area = " + df.format(surfaceArea)
                        + "\n" + " Volume = " + df.format(volume));
            }
    
            private void cubeComputation() throws ArithmeticException,
                    NumberFormatException, Exception {
                String lString = lTextField.getText();
                String wString = wTextField.getText();
                String hString = hTextField.getText();
    
    
                double l = Double.parseDouble(lString);
                double w = Double.parseDouble(wString);
                double h = Double.parseDouble(hString);
    
                double surfaceArea = 6 * Math.pow(h, 2);
                double volume = l * w * h;
    
                resultTextArea.setText("Surface Area = " + df.format(surfaceArea)
                        + "\n" + " Volume = " + df.format(volume));
            }
    
            private void rectangularPrismComputation() throws ArithmeticException,
                    NumberFormatException, Exception {
                String lString = lTextField.getText();
                String wString = wTextField.getText();
                String hString = hTextField.getText();
    
    
                double l = Double.parseDouble(lString);
                double w = Double.parseDouble(wString);
                double h = Double.parseDouble(hString);
    
                double surfaceArea = 2 * (w * h + l * w + l * h);
                double volume = l * w * h;
    
                resultTextArea.setText("Surface Area = " + df.format(surfaceArea)
                        + "\n" + " Volume = " + df.format(volume));
            }
        }
    }
    Attached Files Attached Files
    Last edited by Fubarable; 12-09-2011 at 12:15 PM. Reason: QUOTE TAGS CHANGED TO CODE TAGS

  2. #2
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Geometric Calculator

    Hello!! Can someone please give me a hand?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default Re: Geometric Calculator

    Quote Originally Posted by DK Bello View Post
    Hello!! Can someone please give me a hand?
    We didn't see any question.

    kind regards,

    Jos

    ps. That was way too much code to wade through ...
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Geometric Calculator

    My request is, I'm designing a geometric calculator and need someone to try editing it or suggest more futures for it, coz I'm out of ideas. you can download the ziped file named Calculator.zip just below the codes, the ziped file contains all the application source codes, so you don't have to copy all this codes.
    Geometric Calculator-calculator.png
    This is how the calculator looks like when you run the application.

Similar Threads

  1. Geometric shapes
    By droidus in forum New To Java
    Replies: 9
    Last Post: 10-25-2011, 01:55 AM
  2. calculator
    By rithish in forum AWT / Swing
    Replies: 10
    Last Post: 04-25-2011, 11:24 PM
  3. Help with AWT CALCULATOR
    By Megan Dosnueve in forum AWT / Swing
    Replies: 2
    Last Post: 04-04-2011, 05:49 PM
  4. need help with my calculator
    By semoche in forum AWT / Swing
    Replies: 6
    Last Post: 12-04-2009, 10:16 PM
  5. Calculator
    By water in forum AWT / Swing
    Replies: 4
    Last Post: 09-23-2009, 06:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •