Results 1 to 5 of 5
  1. #1
    pricklygoo33 is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Issue with GUI calculator

    Hello all! I am a student who is new to Java (I started earlier this year via an AP computer science class). Now that the AP test is over, we have been assigned one final project that must contain a GUI of some kind. For my project, I decided to put some of the stuff I learned in physics to use and design a calculator that will calculate the force of friction, mass, angle of elevation, etc. of an object that is either on a flat surface or an inclined plane.

    My project is nearly complete - just about everything has been coded and all that is really left is writing javadocs and saving the project as a jar file. However, during the testing phase of my project, I found that several of the functions of my calculator class are returning the incorrect values. What my project is supposed to do is, given the user's selection of a value to calculate, take the required inputs in their respective JTextFields, send these inputs to the calculator class, and output the results in an adjacent set of JTextFields. In addition, should the user elect to do so, I set up a JButton with a listener that is supposed to get these outputs and show an appropriate diagram of the situation in question. This, too, is problematic - the GUI window launched by the listener is not correctly getting these outputs and instead is returning all zeros.

    I suppose my question is: what sort of intermediate steps could be causing my results to be so different from what they should be? All I am doing to output the results in the JTextFields is using a calculatorClass.calculateValue(). Is there something I am forgetting? In addition, why doesn't using accessor methods to output the results in the JTextFields return anything but zeros for my diagram class?

    Thanks!

    EDIT: sorry I am new to the forum, I am trying to post code...

    This is my calculator class:
    Java Code:
    import java.lang.Math.*;
    import java.text.DecimalFormat;
    
    public class ForcesCalculator {
    
    	//instance variables
    	private double mass;
    	private double acceleration;
    	private double coefficientOfFriction;
    	private double angleOfElevation;
    	private double normalForce;
    	private double forceOfGravity;
    	private double forceOfFriction;
    	public double fgx;
    
    	//constructor
    	public ForcesCalculator(){
    	}
    
    	//calculations
    	public double calcMass(){
    		if(normalForce > 0 && angleOfElevation > 0 && angleOfElevation < 90){
    			mass = normalForce / (9.806 * Math.cos(angleOfElevation));
    			DecimalFormat formatDecimal = new DecimalFormat("#.####");
    			return Double.valueOf(formatDecimal.format(mass));
    		}
    		return mass;
    	}
    
    	public double calcAcceleration(){
    		if(mass > 0 && angleOfElevation > 0 && angleOfElevation < 90){
    			normalForce = mass * 9.806 * Math.cos(angleOfElevation);
    			forceOfFriction = coefficientOfFriction * normalForce;
    			double hold = (Math.cos(angleOfElevation));
    			double hold2 = Math.pow(hold, 2);
    			double hold3 = Math.sqrt(1 - hold2);
    			fgx = mass * 9.806 * hold3;
    			if(forceOfFriction >= fgx || angleOfElevation == 0)
    				acceleration = 0;
    			else if (forceOfFriction < fgx)
    				acceleration = (fgx - forceOfFriction) / mass;
    			DecimalFormat formatDecimal = new DecimalFormat("#.####");
    			return Double.valueOf(formatDecimal.format(acceleration));
    		}
    		return acceleration;
    	}
    
    	public double calcCoefficientOfFriction(){
    		if(mass > 0 && angleOfElevation > 0 && angleOfElevation < 90){
    			normalForce = mass * 9.806 * Math.cos(angleOfElevation);
    			coefficientOfFriction = forceOfFriction / normalForce;
    			DecimalFormat formatDecimal = new DecimalFormat("#.####");
    			return Double.valueOf(formatDecimal.format(coefficientOfFriction));
    		}
    		return coefficientOfFriction;
    	}
    
    	public double calcAngleOfElevation(){
    		if(mass > 0 && normalForce > 0){
    			double temp = normalForce / (mass * 9.806);
    			angleOfElevation = Math.acos(temp);
    			DecimalFormat formatDecimal = new DecimalFormat("#.####");
    			return Double.valueOf(formatDecimal.format(angleOfElevation));
    		}
    		return angleOfElevation;
    	}
    
    	public double calcNormalForce(){
    		if(mass > 0 && angleOfElevation < 90){
    			normalForce = mass * 9.806 * Math.cos(angleOfElevation);
    			DecimalFormat formatDecimal = new DecimalFormat("#.####");
    			return Double.valueOf(formatDecimal.format(normalForce));
    		}
    		return normalForce;
    	}
    
    	public double calcForceOfGravity(){
    //		calcMass();
    		if(mass > 0){
    			forceOfGravity = mass * 9.806;
    			DecimalFormat formatDecimal = new DecimalFormat("#.####");
    			return Double.valueOf(formatDecimal.format(forceOfGravity));
    		}
    		return forceOfGravity;
    	}
    
    	public double calcForceOfFriction(){
    		if(mass > 0 && angleOfElevation < 90){
    			normalForce = mass * 9.806 * Math.cos(angleOfElevation);
    			forceOfFriction = normalForce * coefficientOfFriction;
    			DecimalFormat formatDecimal = new DecimalFormat("#.####");
    			return Double.valueOf(formatDecimal.format(forceOfFriction));
    		}
    		return forceOfFriction;
    	}
    
    	//mutator methods
    	public void resetAll(){
        	mass = acceleration = coefficientOfFriction = angleOfElevation = forceOfFriction = normalForce = forceOfGravity = 0.0;
        }
        public void setMass(double value){
        	mass = value;
        }
        public void setAcceleration(double value){
        	acceleration = value;
        }
        public void setCoefficientOfFriction(double value){
        	coefficientOfFriction = value;
        }
        public void setAngleOfElevation(double value){
        	angleOfElevation = value;
        }
        public void setForceOfFriction(double value){
        	forceOfFriction = value;
        }
        public void setNormalForce(double value){
        	normalForce = value;
        }
        public void setForceOfGravity(double value){
        	forceOfGravity = value;
        }
    
    	//accessor methods
    	public double getMass(){
    		return mass;
    	}
    	public double getAcceleration(){
    		return acceleration;
    	}
    	public double getCoefficientOfFriction(){
    		return coefficientOfFriction;
    	}
    	public double getAngleOfElevation(){
    		return angleOfElevation;
    	}
    	public double getNormalForce(){
    		return normalForce;
    	}
    	public double getForceOfGravity(){
    		return forceOfGravity;
    	}
    	public double getForceOfFriction(){
    		return forceOfFriction;
    	}
    }
    This is my GUI for the calculator...
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    
    public class ForcesGUI extends JFrame{
    
    	private ForcesCalculator fc = new ForcesCalculator();
    
    	//declare items
    	ImageIcon header = new ImageIcon("translogo.png");
    	ImageIcon fbdtrans = new ImageIcon("imgB.png");
    	private JLabel titleL = new JLabel("", header, JLabel.CENTER);
    	private JLabel titleL2 = new JLabel("", fbdtrans, JLabel.RIGHT);
    	private JLabel myNameL = new JLabel("CREATED BY BRAD DERRICK. MAY 2013.");
    	private JLabel massL = new JLabel("Mass (kg):");
    	private JLabel accelerationL = new JLabel("Acceleration (m/sec/sec):");
    	private JLabel normalForceL = new JLabel("Normal force (N):");
    	private JLabel forceOfGravityL = new JLabel("Force of gravity (N):");
    	private JLabel forceOfFrictionL = new JLabel("Force of friction (N):");
    	private JLabel coefficientOfFrictionL = new JLabel("Coefficient of friction:");
    	private JLabel angleOfElevationL = new JLabel("Angle of elevation (degrees):");
    	private JLabel massL2 = new JLabel("Mass (kg):");
    	private JLabel accelerationL2 = new JLabel("Acceleration (m/sec/sec):");
    	private JLabel normalForceL2 = new JLabel("Normal force (N):");
    	private JLabel forceOfGravityL2 = new JLabel("Force of gravity (N):");
    	private JLabel forceOfFrictionL2 = new JLabel("Force of friction (N):");
    	private JLabel coefficientOfFrictionL2 = new JLabel("Coefficient of friction:");
    	private JLabel angleOfElevationL2 = new JLabel("Angle of elevation (degrees):");
    	private JTextField massF = new JTextField("");
    	private JTextField massF2 = new JTextField("0.0");
    	private JTextField accelerationF = new JTextField("");
    	private JTextField accelerationF2 = new JTextField("0.0");
    	private JTextField normalForceF = new JTextField("");
    	private JTextField normalForceF2 = new JTextField("0.0");
    	private JTextField forceOfGravityF = new JTextField("");
    	private JTextField forceOfGravityF2 = new JTextField("0.0");
    	private JTextField forceOfFrictionF = new JTextField("");
    	private JTextField forceOfFrictionF2 = new JTextField("0.0");
    	private JTextField coefficientOfFrictionF = new JTextField("");
    	private JTextField coefficientOfFrictionF2 = new JTextField("0.0");
    	private JTextField angleOfElevationF = new JTextField("");
    	private JTextField angleOfElevationF2 = new JTextField("0.0");
    	private JButton massB = new JButton("MASS");
    	private JButton accelerationB = new JButton("ACCELERATION");
    	private JButton normalForceB = new JButton("NORMAL FORCE");
    	private JButton forceOfGravityB = new JButton("FORCE OF GRAVITY");
    	private JButton forceOfFrictionB = new JButton("FORCE OF FRICTION");
    	private JButton coefficientOfFrictionB = new JButton("COEFFICIENT OF FRICTION");
    	private JButton angleOfElevationB = new JButton("ANGLE OF ELEVATION");
    	private JButton diagramB = new JButton("DIAGRAM");
    	private JButton runValuesB = new JButton("RUN VALUES");
    	private JButton clearB = new JButton("RESET");
    	private Font myFont;
    	private Font myFont2;
    	private Font myFont3;
    
    	//constructor
    	public ForcesGUI(){
    
    		//customize
    		Font myFont = new Font("impact", Font.PLAIN, 16);
    		Font myFont2 = new Font("impact", Font.PLAIN, 40);
    		Font myFont3 = new Font("impact", Font.PLAIN, 18);
    		massB.setBackground(new Color(243, 101, 101));
    		accelerationB.setBackground(new Color(247, 172, 98));
    		normalForceB.setBackground(new Color(245, 238, 99));
    		forceOfGravityB.setBackground(new Color(182, 239, 105));
    		forceOfFrictionB.setBackground(new Color(105, 192, 239));
    		coefficientOfFrictionB.setBackground(new Color(112, 101, 243));
    		angleOfElevationB.setBackground(new Color(202, 105, 239));
    		clearB.setBackground(new Color(194, 194, 194));
    		runValuesB.setBackground(new Color(210, 168, 104));
    		diagramB.setBackground(Color.BLACK);
    		diagramB.setForeground(Color.WHITE);
    
    		//tooltips
    		massF.setToolTipText("Enter a number greater than zero. Units are in kilograms (kg).");
    		accelerationF.setToolTipText("Enter a number greater than zero. Units are in meters per second per second.");
    		normalForceF.setToolTipText("Enter a number greater than zero. Units are in Newtons (N).");
    		forceOfGravityF.setToolTipText("Enter a number greater than zero. Units are in Newtons (N).");
    		forceOfFrictionF.setToolTipText("Enter a number greater than zero. Units are in Newtons (N).");
    		coefficientOfFrictionF.setToolTipText("Enter a number between zero and one (inclusive).");
    		angleOfElevationF.setToolTipText("Enter a number between zero and ninety. Zero may be used; ninety may not.");
    		massB.setToolTipText("Click this button to calculate the mass.");
    		accelerationB.setToolTipText("Click this button to calculate the acceleration.");
    		normalForceB.setToolTipText("Click this button to calculate the normal force.");
    		forceOfGravityB.setToolTipText("Click this button to calculate the force of gravity.");
    		forceOfFrictionB.setToolTipText("Click this button to calculate the force of friction.");
    		coefficientOfFrictionB.setToolTipText("Click this button to calculate the coefficient of friction.");
    		angleOfElevationB.setToolTipText("Click this button to calculate the angle of elevation.");
    		diagramB.setToolTipText("Click this button to view a free-body diagram representing the force in the current situation. Requires an angle of elevation and a coefficient of friction.");
    		runValuesB.setToolTipText("Click this button to calculate your values.");
    		clearB.setToolTipText("Click this button to clear all fields.");
    
    		//set items
    		massF.setEditable(false);
    		massF2.setEditable(false);
    		accelerationF.setEditable(false);
    		accelerationF2.setEditable(false);
    		normalForceF.setEditable(false);
    		normalForceF2.setEditable(false);
    		forceOfGravityF.setEditable(false);
    		forceOfGravityF2.setEditable(false);
    		forceOfFrictionF.setEditable(false);
    		forceOfFrictionF2.setEditable(false);
    		coefficientOfFrictionF.setEditable(false);
    		coefficientOfFrictionF2.setEditable(false);
    		angleOfElevationF.setEditable(false);
    		angleOfElevationF2.setEditable(false);
    		myNameL.setFont(myFont3);
    		myNameL.setForeground(Color.BLACK);
    		massB.setFont(myFont);
    		accelerationB.setFont(myFont);
    		normalForceB.setFont(myFont);
    		forceOfGravityB.setFont(myFont);
    		forceOfFrictionB.setFont(myFont);
    		coefficientOfFrictionB.setFont(myFont);
    		angleOfElevationB.setFont(myFont);
    		runValuesB.setFont(myFont);
    		diagramB.setFont(myFont2);
    		clearB.setFont(myFont);
    
    		//instantiate panels
    		JPanel leftMidP = new JPanel(new GridLayout(7, 1, 2, 5));
    		JPanel centerMidP = new JPanel(new GridLayout(7, 2, 2, 5));
    		JPanel rightMidP = new JPanel(new GridLayout(7, 2, 2, 5));
    		JPanel optionsP = new JPanel(new GridLayout(2, 1, 2, 5));
    		JPanel titleP = new JPanel();
    		JPanel myNameP = new JPanel();
    		titleP.setBackground(Color.WHITE);
    		titleP.setBorder(new LineBorder(Color.BLACK));
    		myNameP.setBorder(new LineBorder(Color.BLACK));
    		myNameP.setBackground(Color.WHITE);
    
    		//condense panels
    		leftMidP.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "Select value to calculate:"));
       		rightMidP.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "Results:"));
       		centerMidP.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "Enter values:"));
       		optionsP.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "Options:"));
       		JPanel midCollectP = new JPanel();
       		midCollectP.add(leftMidP);
       		midCollectP.add(centerMidP);
       		midCollectP.add(rightMidP);
       		midCollectP.add(optionsP);
       		JPanel bottomP = new JPanel();
       		bottomP.setLayout(new BoxLayout(bottomP, BoxLayout.Y_AXIS));
       		bottomP.add(myNameP);
       		JPanel topP = new JPanel();
    		topP.add(titleP);
    		JPanel optionsTopP = new JPanel();
    		JPanel optionsBottomP = new JPanel(new GridLayout(2, 1, 2, 5));
    		optionsP.add(optionsTopP);
    		optionsP.add(optionsBottomP);
    
    		//add fields, labels, and buttons to panels
       		myNameP.add(myNameL);
       		titleP.add(titleL);
       		titleP.add(titleL2);
       		optionsTopP.add(diagramB);
       		optionsBottomP.add(runValuesB);
       		optionsBottomP.add(clearB);
       		leftMidP.add(massB);
      		leftMidP.add(accelerationB);
      		leftMidP.add(normalForceB);
      		leftMidP.add(forceOfGravityB);
      		leftMidP.add(forceOfFrictionB);
      		leftMidP.add(coefficientOfFrictionB);
      		leftMidP.add(angleOfElevationB);
      		centerMidP.add(massL);
      		centerMidP.add(massF);
      		centerMidP.add(accelerationL);
      		centerMidP.add(accelerationF);
      		centerMidP.add(normalForceL);
      		centerMidP.add(normalForceF);
      		centerMidP.add(forceOfGravityL);
      		centerMidP.add(forceOfGravityF);
      		centerMidP.add(forceOfFrictionL);
      		centerMidP.add(forceOfFrictionF);
      		centerMidP.add(coefficientOfFrictionL);
      		centerMidP.add(coefficientOfFrictionF);
      		centerMidP.add(angleOfElevationL);
      		centerMidP.add(angleOfElevationF);
      		rightMidP.add(massL2);
      		rightMidP.add(massF2);
      		rightMidP.add(accelerationL2);
      		rightMidP.add(accelerationF2);
      		rightMidP.add(normalForceL2);
      		rightMidP.add(normalForceF2);
      		rightMidP.add(forceOfGravityL2);
      		rightMidP.add(forceOfGravityF2);
      		rightMidP.add(forceOfFrictionL2);
      		rightMidP.add(forceOfFrictionF2);
      		rightMidP.add(coefficientOfFrictionL2);
      		rightMidP.add(coefficientOfFrictionF2);
      		rightMidP.add(angleOfElevationL2);
      		rightMidP.add(angleOfElevationF2);
    
    		//place condensed panels into window
      		add(midCollectP, BorderLayout.CENTER);
    		add(topP, BorderLayout.NORTH);
    		add(bottomP, BorderLayout.SOUTH);
    
    		//join buttons and their respective listeners
    		massB.addActionListener(new MassListener());
    		accelerationB.addActionListener(new AccelerationListener());
    		normalForceB.addActionListener(new NormalForceListener());
    		forceOfGravityB.addActionListener(new ForceOfGravityListener());
    		forceOfFrictionB.addActionListener(new ForceOfFrictionListener());
    		coefficientOfFrictionB.addActionListener(new CoefficientOfFrictionListener());
    		angleOfElevationB.addActionListener(new AngleOfElevationListener());
    		diagramB.addActionListener(new DiagramListener());
    		runValuesB.addActionListener(new RunValuesListener());
    		clearB.addActionListener(new ClearListener());
    
    	}//end constructor
    
    	private void screenInput(){
    		try{
    	      if(!massF.getText().equals("") && !massF.getText().equals("calculated value"))
    	      	fc.setMass(Double.parseDouble(massF.getText()));
    	      if(!accelerationF.getText().equals("") && !accelerationF.getText().equals("calculated value"))
    	      	fc.setAcceleration(Double.parseDouble(accelerationF.getText()));
    	      if(!normalForceF.getText().equals("") && !normalForceF.getText().equals("calculated value"))
    	      	fc.setNormalForce(Double.parseDouble(normalForceF.getText()));
    	      if(!forceOfGravityF.getText().equals("") && !forceOfGravityF.getText().equals("calculated value"))
    	      	fc.setForceOfGravity(Double.parseDouble(forceOfGravityF.getText()));
    	      if(!forceOfFrictionF.getText().equals("") && !forceOfFrictionF.getText().equals("calculated value"))
    	      	fc.setForceOfFriction(Double.parseDouble(forceOfFrictionF.getText()));
    	      if(!coefficientOfFrictionF.getText().equals("") && !coefficientOfFrictionF.getText().equals("calculated value"))
    	      	fc.setCoefficientOfFriction(Double.parseDouble(coefficientOfFrictionF.getText()));
    	      if(!angleOfElevationF.getText().equals("") && !angleOfElevationF.getText().equals("calculated value"))
    	      	fc.setAngleOfElevation(Double.parseDouble(angleOfElevationF.getText()));
    	      	}
    	      	catch (NumberFormatException e){
    	      		JOptionPane.showMessageDialog(ForcesGUI.this, "Improper number format.", "Forces Calculator", JOptionPane.ERROR_MESSAGE);
    	      	}
    	}
    
    	//listeners
    	private class MassListener implements ActionListener{
    		public void actionPerformed(ActionEvent newAction){
    			fc.resetAll();
    	      	massF.setEditable(false);
    	      	massF.setText("calculated value");
    	      	angleOfElevationF.setText("");
    	      	angleOfElevationF.setEditable(true);
    	      	normalForceF.setText("");
    	     	normalForceF.setEditable(true);
    	      	fc.setMass(0.0);
    	      	massF.setText("" + fc.getMass());
          	}
    	}
    
    	private class AccelerationListener implements ActionListener{
    		public void actionPerformed(ActionEvent newAction){
    			fc.resetAll();
    	      	accelerationF.setEditable(false);
    	      	accelerationF.setText("calculated value");
    	      	coefficientOfFrictionF.setText("");
    	      	coefficientOfFrictionF.setEditable(true);
    	      	angleOfElevationF.setText("");
    	      	angleOfElevationF.setEditable(true);
    	      	massF.setText("");
    	   		massF.setEditable(true);
    	      	fc.setAcceleration(0.0);
    	      	accelerationF.setText("" + fc.getAcceleration());
          	}
    	}
    
    	private class NormalForceListener implements ActionListener{
    		public void actionPerformed(ActionEvent newAction){
    			fc.resetAll();
    	      	normalForceF.setEditable(false);
    	      	normalForceF.setText("calculated value");
    	      	angleOfElevationF.setText("");
    	      	angleOfElevationF.setEditable(true);
    	      	massF.setText("");
    	   		massF.setEditable(true);
    	      	fc.setNormalForce(0.0);
    	      	normalForceF.setText("" + fc.getNormalForce());
          	}
    	}
    
    	private class ForceOfGravityListener implements ActionListener{
    		public void actionPerformed(ActionEvent newAction){
    			fc.resetAll();
    	      	forceOfGravityF.setEditable(false);
    	      	forceOfGravityF.setText("calculated value");
    	      	massF.setText("");
    	   		massF.setEditable(true);
    	      	fc.setForceOfGravity(0.0);
    	      	forceOfGravityF.setText("" + fc.getForceOfGravity());
          	}
    	}
    
    	private class ForceOfFrictionListener implements ActionListener{
    		public void actionPerformed(ActionEvent newAction){
    			fc.resetAll();
    	      	forceOfFrictionF.setEditable(false);
    	      	forceOfFrictionF.setText("calculated value");
    	      	coefficientOfFrictionF.setText("");
    	      	coefficientOfFrictionF.setEditable(true);
    	      	angleOfElevationF.setText("");
    	      	angleOfElevationF.setEditable(true);
    	      	massF.setText("");
    	   		massF.setEditable(true);
    	      	fc.setForceOfFriction(0.0);
    	      	forceOfFrictionF.setText("" + fc.getForceOfFriction());
          	}
    	}
    
    	private class CoefficientOfFrictionListener implements ActionListener{
    		public void actionPerformed(ActionEvent newAction){
    			fc.resetAll();
    	      	coefficientOfFrictionF.setEditable(false);
    	      	coefficientOfFrictionF.setText("calculated value");
    	      	forceOfFrictionF.setText("");
    	      	forceOfFrictionF.setEditable(true);
    	      	angleOfElevationF.setText("");
    	      	angleOfElevationF.setEditable(true);
    	      	massF.setText("");
    	   		massF.setEditable(true);
    	      	fc.setCoefficientOfFriction(0.0);
    	      	coefficientOfFrictionF.setText("" + fc.getCoefficientOfFriction());
          	}
    	}
    
    	private class AngleOfElevationListener implements ActionListener{
    		public void actionPerformed(ActionEvent newAction){
    			fc.resetAll();
    	      	angleOfElevationF.setEditable(false);
    	      	angleOfElevationF.setText("calculated value");
    	      	normalForceF.setText("");
    	   		normalForceF.setEditable(true);
    	      	massF.setText("");
    	   		massF.setEditable(true);
    	      	fc.setAngleOfElevation(0.0);
    	      	angleOfElevationF.setText("" + fc.getAngleOfElevation());
          	}
    	}
    
    	private class DiagramListener implements ActionListener{
    		public void actionPerformed(ActionEvent newAction){
    	      	DrawDiagram fbd = new DrawDiagram();
    			fbd.setTitle("Free Body Diagram");
    			fbd.setResizable(false);
    			fbd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			GraphicsConfiguration gc = fbd.getGraphicsConfiguration();
    	        Rectangle bounds = gc.getBounds();
    	      	fbd.setLocation(bounds.width / 2 - 300, bounds.height / 2 - 300);
    	      	fbd.setIconImage(Toolkit.getDefaultToolkit().getImage("forcesicon.png"));
    			fbd.pack();
    			fbd.setVisible(true);
          	}
    	}
    
    	private class RunValuesListener implements ActionListener{
    		public void actionPerformed(ActionEvent newAction){
    	      	fc.resetAll();
    	      	screenInput();
    	      	massF2.setText("" + fc.calcMass());
    	      	accelerationF2.setText("" + fc.calcAcceleration());
    	      	normalForceF2.setText("" + fc.calcNormalForce());
    	      	forceOfGravityF2.setText("" + fc.calcForceOfGravity());
    	      	forceOfFrictionF2.setText("" + fc.calcForceOfFriction());
    	      	coefficientOfFrictionF2.setText("" + fc.calcCoefficientOfFriction());
    	      	angleOfElevationF2.setText("" + fc.calcAngleOfElevation());
          	}
    	}
    
    	private class ClearListener implements ActionListener{
    		public void actionPerformed(ActionEvent newAction){
    	      	fc.resetAll();
    	      	massF.setText("");
    	      	accelerationF.setText("");
    	      	normalForceF.setText("");
    	      	forceOfGravityF.setText("");
    	      	forceOfFrictionF.setText("");
    	      	coefficientOfFrictionF.setText("");
    	      	angleOfElevationF.setText("");
    	      	massF.setEditable(false);
    			accelerationF.setEditable(false);
    			normalForceF.setEditable(false);
    			forceOfGravityF.setEditable(false);
    			forceOfFrictionF.setEditable(false);
    			coefficientOfFrictionF.setEditable(false);
    			angleOfElevationF.setEditable(false);
    	      	massF2.setText("" + fc.getMass());
    	      	accelerationF2.setText("" + fc.getAcceleration());
    	      	normalForceF2.setText("" + fc.getNormalForce());
    	      	forceOfGravityF2.setText("" + fc.getForceOfGravity());
    	      	forceOfFrictionF2.setText("" + fc.getForceOfFriction());
    	      	coefficientOfFrictionF2.setText("" + fc.getCoefficientOfFriction());
    	      	angleOfElevationF2.setText("" + fc.getAngleOfElevation());
          	}
    	}
    And this is the class that is supposed to pop up the diagram:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    
    public class DrawDiagram extends JFrame{
    
    	private ForcesCalculator fc2 = new ForcesCalculator();
    	private ForcesGUI fg2 = new ForcesGUI();
    
    	//declare items
    	ImageIcon flat = new ImageIcon("flat.png");
    	ImageIcon tenDegNoFric = new ImageIcon("tendegnofric.png");
    	ImageIcon tenDegFric = new ImageIcon("tendegfric.png");
    	ImageIcon twentyDegnoFric = new ImageIcon("twentydegnofric.png");
    	ImageIcon twentyDegFric = new ImageIcon("twentydegfric.png");
    	ImageIcon thirtyDegNoFric = new ImageIcon("thirtydegnofric.png");
    	ImageIcon thirtyDegFric = new ImageIcon("thirtydegfric.png");
    	ImageIcon fortyDegNoFric = new ImageIcon("fortydegnofric.png");
    	ImageIcon fortyDegFric = new ImageIcon("fortydegfric.png");
    	ImageIcon fiftyDegNoFric = new ImageIcon("fiftydegnofric.png");
    	ImageIcon fiftyDegFric = new ImageIcon("fiftydegfric.png");
    	ImageIcon sixtyDegNoFric = new ImageIcon("sixtydegnofric.png");
    	ImageIcon sixtyDegFric = new ImageIcon("sixtydegfric.png");
    	ImageIcon seventyDegNoFric = new ImageIcon("seventydegnofric.png");
    	ImageIcon seventyDegFric = new ImageIcon("seventydegfric.png");
    	ImageIcon eightyDegNoFric = new ImageIcon("eightydegnofric.png");
    	ImageIcon eightyDegFric = new ImageIcon("eightydegfric.png");
    	private JLabel flatL = new JLabel(flat);
    	private JLabel tenDNF = new JLabel(tenDegNoFric);
    	private JLabel tenDF = new JLabel(tenDegFric);
    	private JLabel twentyDNF = new JLabel(twentyDegnoFric);
    	private JLabel twentyDF = new JLabel(twentyDegFric);
    	private JLabel thirtyDNF = new JLabel(thirtyDegNoFric);
    	private JLabel thirtyDF = new JLabel(thirtyDegFric);
    	private JLabel fortyDNF = new JLabel(fortyDegNoFric);
    	private JLabel fortyDF = new JLabel(fortyDegFric);
    	private JLabel fiftyDNF = new JLabel(fiftyDegNoFric);
    	private JLabel fiftyDF = new JLabel(fiftyDegFric);
    	private JLabel sixtyDNF = new JLabel(sixtyDegNoFric);
    	private JLabel sixtyDF = new JLabel(sixtyDegFric);
    	private JLabel seventyDNF = new JLabel(seventyDegNoFric);
    	private JLabel seventyDF = new JLabel(seventyDegFric);
    	private JLabel eightyDNF = new JLabel(eightyDegNoFric);
    	private JLabel eightyDF = new JLabel(eightyDegFric);
    
    	//constructor
    	public DrawDiagram(){
    
    		fc2.calcCoefficientOfFriction();
    		fc2.calcAngleOfElevation();
    
    		//instantiate panels
    		JPanel topDDP = new JPanel();
    		topDDP.setBackground(Color.WHITE);
    
    		//conditionally add top items to panels
    		if(fc2.calcAngleOfElevation() == 0)
    			topDDP.add(flatL);
    
    		if(fc2.calcAngleOfElevation() > 0 && fc2.calcAngleOfElevation() <= 15){
    			if(fc2.calcCoefficientOfFriction() != 0)
    				topDDP.add(tenDF);
    			else
    				topDDP.add(tenDNF);
    		}
    
    		if(fc2.calcAngleOfElevation() > 15 && fc2.calcAngleOfElevation() <= 25){
    			if(fc2.calcCoefficientOfFriction() != 0)
    				topDDP.add(twentyDF);
    			else
    				topDDP.add(twentyDNF);
    		}
    
    		if(fc2.calcAngleOfElevation() > 25 && fc2.calcAngleOfElevation() <= 35){
    			if(fc2.calcCoefficientOfFriction() != 0)
    				topDDP.add(thirtyDF);
    			else
    				topDDP.add(thirtyDNF);
    		}
    
    		if(fc2.calcAngleOfElevation() > 35 && fc2.calcAngleOfElevation() <= 45){
    			if(fc2.calcCoefficientOfFriction() != 0)
    				topDDP.add(fortyDF);
    			else
    				topDDP.add(fortyDNF);
    		}
    
    		if(fc2.calcAngleOfElevation() > 45 && fc2.calcAngleOfElevation() <= 55){
    			if(fc2.calcCoefficientOfFriction() != 0)
    				topDDP.add(fiftyDF);
    			else
    				topDDP.add(fiftyDNF);
    		}
    
    		if(fc2.calcAngleOfElevation() > 55 && fc2.calcAngleOfElevation() <= 65){
    			if(fc2.calcCoefficientOfFriction() != 0)
    				topDDP.add(sixtyDF);
    			else
    				topDDP.add(sixtyDNF);
    		}
    
    		if(fc2.calcAngleOfElevation() > 65 && fc2.calcAngleOfElevation() <= 75){
    			if(fc2.calcCoefficientOfFriction() != 0)
    				topDDP.add(seventyDF);
    			else
    				topDDP.add(seventyDNF);
    		}
    
    		if(fc2.calcAngleOfElevation() > 75 && fc2.calcAngleOfElevation() < 90){
    			if(fc2.calcCoefficientOfFriction() != 0)
    				topDDP.add(eightyDF);
    			else
    				topDDP.add(eightyDNF);
    		}
    
    		//add panels to window
    		add(topDDP, BorderLayout.NORTH);
    
    	}//end constructor
    }//end of class
    Last edited by pricklygoo33; 05-27-2013 at 01:56 AM.

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: Issue with GUI calculator

    If the reason is not a logic error somewhere in your code, then a quick reason I can think of may be that you do divisions with integers without typecasting as double
    post the class that does the operations, because there is not enough information

  3. #3
    pricklygoo33 is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Re: Issue with GUI calculator

    I've added the code (sorry it's so many lines!) to my post. I'll check my logic :) how do divisions and typecasting work?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Issue with GUI calculator

    Also, the trig methods in the Math class consider their arguments as radians, not degrees.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: Issue with GUI calculator

    You don't use any ints so that is not the problem. If you divide ints like double s = int1 / int2, you get 0.0 if i'm not mistaken

Similar Threads

  1. BMI calculator
    By snustad in forum New To Java
    Replies: 4
    Last Post: 11-29-2012, 09:08 PM
  2. Basic Java Calculator - Division Issue
    By Alpinstang in forum New To Java
    Replies: 3
    Last Post: 09-07-2012, 10:31 PM
  3. calculator help
    By sirstroud in forum New To Java
    Replies: 0
    Last Post: 04-01-2012, 05:58 AM
  4. Regular Expression issue and setName() method issue
    By geforce in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 03:33 AM
  5. Need help with Calculator
    By Joshua4missions in forum New To Java
    Replies: 4
    Last Post: 12-10-2011, 02:44 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
  •