Page 1 of 2 12 LastLast
Results 1 to 20 of 37
Like Tree2Likes

Thread: Final Project - physics calculator

  1. #1
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Final Project - physics calculator

    Hello, I am starting a final project that we are supposed to complete and I've chosen to build a GUI that will initially display a window with 'x' number of buttons for different subject areas of physics (kinematics, waves, sound, circuits...etc). I would like to use an Actionlistener for each button so that when its pressed, the JFrame will extend a sidepanel or expand the JFrame with a new JPanel that contains variable Jtextfields so the user can enter data to calculate.

    I'd like to know if its possible to do this with the JFrame or should I just open a new window altogether? I have some other features I plan to add to the program, but this is where I am right now.

    Also, one other quick question, how difficult is it to add a banner image and imageIcon? By banner image I mean create a Jpanel that will contain a header for the window and will have some cool physics jpg image.

    Thanks for any help and advice.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: Final Project - physics calculator

    You might want to look at examples of using the JTabbedPane class. Each tab could represent an area of physics, and it's corresponding JPanel would have the form relevant to it.
    javaStooge likes this.

  3. #3
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Final Project - physics calculator

    thank you sehudson, I will definitely check this out. I'm still curious, do you know if it's possible to do what I mentioned.

  4. #4
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Final Project - physics calculator

    I would like to use an Actionlistener for each button so that when its pressed, the JFrame will extend a sidepanel or expand the JFrame with a new JPanel that contains variable Jtextfields so the user can enter data to calculate.
    Based on the above, you can do an Internet search using the search term "expand the JFrame with a new JPanel". I did that, and within 2 minutes I found https://community.oracle.com/thread/...art=0&tstart=0.

    As mentioned in that other forum post, you might want to take a look at BorderLayout (Java Platform SE 7 ). See also How to Use BorderLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container). The buttons can go into the LINE_START (WEST) area, and the new JPanel can go into the CENTER or LINE_END (EAST) area.

    Another Internet search term that you can use is "show hide jpanel". Here're a couple of pages I found that discuss the way to dynamically show/hide and add/remove a JPanel:

    Other than laying components using a BorderLayout, you might also want to take a look at How to Use Tabbed Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components). Instead of buttons, you can use tabs, which can be placed at the top, bottom, or on the left-hand side.

    how difficult is it to add a banner image and imageIcon? By banner image I mean create a Jpanel that will contain a header for the window and will have some cool physics jpg image.
    Shouldn't be difficult. Try using this search term, "add image to jpanel", and do some experimenting.

  5. #5
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Final Project - physics calculator

    I've already created an array of JButtons at the beginning of the program, now I'm trying to add an action listener for them. I ran into this problem in another program I did, but never figured it out.

    Java Code:
    JButton kinematic = new JButton("Kinematic");
    		kinematic.setMnemonic(KeyEvent.VK_K);
    		JButton wave = new JButton("Wave");
    		wave.setMnemonic(KeyEvent.VK_W);
    		JButton sound = new JButton("Sound");
    		sound.setMnemonic(KeyEvent.VK_S);
    		JButton circuit = new JButton("Circuit");
    		circuit.setMnemonic(KeyEvent.VK_C);
    Java Code:
    buttonArray.addActionListener(listener);
    		ActionListener listener = new ActionListener(){
    			public void actionPerformed(ActionEvent ae) {
    				for(int i = 0 ; i < buttonArray.length; i++){
    					if(ae.getSource() == buttonArray){
    				mainPanel.add(buttonArray[i], BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    					}
    				}
    			}
    		};
    Last edited by javaStooge; 04-30-2014 at 11:00 PM.

  6. #6
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Final Project - physics calculator

    Can you describe the problems you ran into?

    Right now I have problems following you because your code does not match your words.

    You talk about an array of buttons but the code regarding the buttons is not using an array.

    And your second code is using the listener before it is defined. So maybe you put together code of different locations?

    Konrad

  7. #7
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Final Project - physics calculator

    I've copied the image I want to use into my library and used the following code:
    Java Code:
    BufferedImage image = ImageIO.read(new File("/physimg.png"));
    		JLabel headerLabel = new JLabel(new ImageIcon(image));
    		headerLabel.setHorizontalAlignment(JLabel.CENTER);
    But it still doesn't want to work. It throws an IOException error: "Can't read input file." I got most of this from another forum post.

    *EDIT*

    Actually have another question regarding this...will the file be formatted to fit the frame (when it does load)?
    Last edited by javaStooge; 04-30-2014 at 11:52 PM.

  8. #8
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Final Project - physics calculator

    Quote Originally Posted by kneitzel View Post
    Can you describe the problems you ran into?

    Right now I have problems following you because your code does not match your words.

    You talk about an array of buttons but the code regarding the buttons is not using an array.

    And your second code is using the listener before it is defined. So maybe you put together code of different locations?

    Konrad
    Yes, I did, I was just trying to keep it at a minimum without posting the entire code. I will go ahead and post the entire thing, hopefully that will make it easier. What I am trying to do is condense the code so I don't have all that ActionListener code for each of the JButtons. Thanks.

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.border.Border;
    
    public class Frame extends JFrame {
    	public Frame() throws IOException {
    
    		// Buttons
    		JButton kinematic = new JButton("Kinematic");
    		kinematic.setMnemonic(KeyEvent.VK_K);
    		JButton wave = new JButton("Wave");
    		wave.setMnemonic(KeyEvent.VK_W);
    		JButton sound = new JButton("Sound");
    		sound.setMnemonic(KeyEvent.VK_S);
    		JButton circuit = new JButton("Circuit");
    		circuit.setMnemonic(KeyEvent.VK_C);
    		
    		JButton back = new JButton("Back");
    		back.setMnemonic(KeyEvent.VK_ESCAPE);
    		back.setFont(new java.awt.Font("Georgia", Font.ITALIC, 16));
    		back.setForeground(Color.RED);
    		//Button Array
    		final JButton[] buttonArray = {kinematic, wave, sound, circuit};
    		for(int i = 0 ; i < buttonArray.length; i++){
    			buttonArray[i].setFont(new java.awt.Font("Georgia", Font.BOLD, 16));
    		}
    		
    		// Panels
    		final JPanel mainPanel = new JPanel();
    		mainPanel.setLayout(new BorderLayout());
    		//Add image banner to this panel 
    		final JPanel buttonPanel = new JPanel();
    		buttonPanel.setLayout(new GridLayout(5, 1));
    		buttonPanel.setBorder(BorderFactory.createBevelBorder(MAXIMIZED_VERT));
    		buttonPanel.setBackground(Color.DARK_GRAY);
    		// Kinematic Pane and Contents
    		final JPanel kinematicPane = new JPanel();
    		kinematicPane.add(back, BorderLayout.CENTER);
    
    		final JPanel wavePane = new JPanel();
    		wavePane.add(back, BorderLayout.CENTER);
    
    		final JPanel soundPane = new JPanel();
    		soundPane.add(back, BorderLayout.CENTER);
    
    		final JPanel circuitPane = new JPanel();
    		circuitPane.add(back, BorderLayout.CENTER);
    		
    		BufferedImage image = ImageIO.read(new File("/PhysAssist/physimg.png"));
    		JLabel headerLabel = new JLabel(new ImageIcon(image));
    		headerLabel.setHorizontalAlignment(JLabel.CENTER);
    		
    		
    		buttonPanel.add(kinematic);
    		buttonPanel.add(wave);
    		buttonPanel.add(sound);
    		buttonPanel.add(circuit);
    
    		add(mainPanel);
    		mainPanel.add(buttonPanel, BorderLayout.CENTER);
    		// Use a for statement to process the object array instead of coding
    		// each individually
    		back.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				mainPanel.remove(kinematicPane);
    				mainPanel.remove(soundPane);
    				mainPanel.remove(wavePane);
    				mainPanel.remove(circuitPane);
    				mainPanel.add(buttonPanel, BorderLayout.CENTER);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    			}
    		});
    
    		buttonArray.addActionListener(listener);
    		ActionListener listener = new ActionListener(){
    			public void actionPerformed(ActionEvent ae) {
    				for(int i = 0 ; i < buttonArray.length; i++){
    					if(ae.getSource() == buttonArray){
    				mainPanel.add(buttonArray[i], BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    					}
    				}
    			}
    		};
    		
    		/*wave.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				mainPanel.add(wavePane, BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    			}
    		});
    
    		sound.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				mainPanel.add(soundPane, BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    			}
    		});
    
    		circuit.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				mainPanel.add(circuitPane, BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    			}
    		});*/
    	}
    }
    Last edited by javaStooge; 05-01-2014 at 01:49 AM.

  9. #9
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Final Project - physics calculator

    For the png image I am trying to load, I would really like to know how to resize it in order to fit nicely above the main buttons. I've checked a number of different sites and tried a different methods they have used (with no success). Finally, I was able to get the image to load, but it doesn't fit the JLabel, its zoomed in on the image. The .png file I am talking about isn't in the attachment.

    Final Project - physics calculator-screen-shot-2014-04-30-8.19.10-pm.jpg
    Last edited by javaStooge; 05-01-2014 at 03:23 AM.

  10. #10
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Final Project - physics calculator

    I'm having a hard time understanding which areas you need help in. Asking interleaving questions on multiple areas in a single forum thread is not helping anyone. I'd suggest that you concentrate on a single area, and post questions to sort it out before moving on to the next area. It might help to start a new thread for each distinct area, solving each sequentially.

    So far it seems that you need help in:
    1. having a JFrame containing a panel of buttons, which when pressed expands the JFrame to include a new panel containing fields for data entry
    2. adding ActionListener to the buttons, condensing the code to avoid having to write similar ActionListener code for each of the buttons
    3. adding a banner at the top of the GUI, with the banner containing some text(?) and an image

    Anything else?

    Which one of the above areas would you like to concentrate on first? For that one and only one area, post what you've done so far (code), and describe the issue you're facing.

  11. #11
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Final Project - physics calculator

    @jashburn, yes things got a little confusing for me as well. I'm loaded with assignments since the semester is coming to a close and feelin overwhelmed. Only had a few hours of decent rest this week.

    Alright, starting over then, I will post the code that is most up to date.

    Probably the most pertinent issue:I need to add an actionListener "Calculate" (JButton) that will retrieve the values entered into the JTextField. I am currently using a number of set methods from the Kinematic class that will pass the values from Frame class to Kinematic class and perform the calculations. Of course after this is done, I would like the calculations to be returned in another side panel (not yet created) that will replace the panel where the user entered the values.

    You will see in the code that I avoided condensing the code and just created each actionListener for the buttons individually. You will also see that I made an attempt to add a nested actionListener for the continue JButton, but I don't think that's the correct way to do this.

    I really hope all of that made sense, but if it didn't, please let me know and I will try to clear it up -- could really use some guidance on this. Thanks a lot!

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.border.Border;
    
    public class Frame extends JFrame {
    	public Frame() throws IOException {
    		super("PhysAssist");
    
    		// Buttons
    		JButton kinematic = new JButton("Kinematic");
    		kinematic.setMnemonic(KeyEvent.VK_K);
    		JButton dynamics = new JButton("Dynamics");
    		dynamics.setMnemonic(KeyEvent.VK_D);
    		JButton power = new JButton("Power and Energy");
    		power.setMnemonic(KeyEvent.VK_E);
    		JButton wave = new JButton("Wave");
    		wave.setMnemonic(KeyEvent.VK_W);
    		JButton sound = new JButton("Sound");
    		sound.setMnemonic(KeyEvent.VK_S);
    		JButton circuit = new JButton("Circuit");
    		circuit.setMnemonic(KeyEvent.VK_C);
    
    		// Execute Buttons
    		JButton calculate = new JButton("Calculate");
    		calculate.setMnemonic(KeyEvent.VK_ENTER);
    		calculate.setFont(new java.awt.Font("Georgia", Font.BOLD, 16));
    		calculate.setForeground(Color.BLUE);
    		JButton back = new JButton("Back");
    		back.setMnemonic(KeyEvent.VK_ESCAPE);
    		back.setFont(new java.awt.Font("Georgia", Font.ITALIC, 16));
    		back.setForeground(Color.RED);
    
    		// Button Array
    		final JButton[] buttonArray = { kinematic, dynamics, power, wave,
    				sound, circuit };
    		for (int i = 0; i < buttonArray.length; i++) {
    			buttonArray[i].setFont(new java.awt.Font("Georgia", Font.BOLD, 16));
    			buttonArray[i].setForeground(Color.darkGray);
    			buttonArray[i].setBackground(Color.GRAY);
    		}
    
    		// Panels
    		final JPanel mainPanel = new JPanel();
    		mainPanel.setLayout(new BorderLayout());
    
    		final JPanel executePanel = new JPanel();
    		executePanel.setLayout(new FlowLayout());
    		executePanel.add(back);
    		executePanel.add(calculate);
    
    		final JPanel sidePanel = new JPanel();
    		sidePanel.setLayout(new BorderLayout());
    
    		final JPanel buttonPanel = new JPanel();
    		buttonPanel.setLayout(new GridLayout(6, 1));
    		buttonPanel.setBackground(Color.DARK_GRAY);
    
    		// Kinematic Pane and Contents
    		final Kinematics kinematics = new Kinematics();
    		final JPanel kinematicPane = new JPanel(new GridLayout(6, 2));
    		kinematicPane.add(new JLabel("Initial Distance"));
    		final JTextField dI = new JTextField();
    		kinematicPane.add(dI);
    		kinematicPane.add(new JLabel("Final Distance"));
    		final JTextField d0 = new JTextField();
    		kinematicPane.add(d0);
    		kinematicPane.add(new JLabel("Initial Velocity"));
    		final JTextField velocityI = new JTextField();
    		kinematicPane.add(velocityI);
    		kinematicPane.add(new JLabel("Final Velocity"));
    		final JTextField velocityF = new JTextField();
    		kinematicPane.add(velocityF);
    		kinematicPane.add(new JLabel("Average Velocity"));
    		final JTextField velocityAverage = new JTextField();
    		kinematicPane.add(velocityAverage);
    		kinematicPane.add(new JLabel("Acceleration"));
    		final JTextField accelerationAverage = new JTextField();
    		kinematicPane.add(accelerationAverage);
    		// JTextField mass = new JTextField();
    		// kinematicPane.add(mass);
    
    		// Dynamics Pane
    		final JPanel dynamicsPane = new JPanel(new GridLayout(6, 2));
    		dynamicsPane.add(new JLabel("Force"));
    		final JTextField force = new JTextField();
    		dynamicsPane.add(force);
    		dynamicsPane.add(new JLabel("Mass One"));
    		final JTextField massOne = new JTextField();
    		dynamicsPane.add(massOne);
    		dynamicsPane.add(new JLabel("Mass One"));
    		final JTextField massTwo = new JTextField();
    		dynamicsPane.add(massTwo);
    		dynamicsPane.add(new JLabel("Radius One"));
    		final JTextField radiusOne = new JTextField();
    		dynamicsPane.add(radiusOne);
    		dynamicsPane.add(new JLabel("Radius Two"));
    		final JTextField radiusTwo = new JTextField();
    		dynamicsPane.add(radiusTwo);
    		dynamicsPane.add(new JLabel("Normal Friction"));
    		final JTextField frictionNormal = new JTextField();
    		dynamicsPane.add(frictionNormal);
    
    		// Power, Work, Energy Pane
    		final JPanel powerPane = new JPanel(new GridLayout(7, 2));
    		powerPane.add(new JLabel("Work"));
    		final JTextField work = new JTextField();
    		powerPane.add(work);
    		powerPane.add(new JLabel("Energy"));
    		final JTextField energy = new JTextField();
    		powerPane.add(energy);
    		powerPane.add(new JLabel("Power"));
    		final JTextField powerP = new JTextField();
    		powerPane.add(power);
    		powerPane.add(new JLabel("Mass"));
    		final JTextField mass = new JTextField();
    		powerPane.add(mass);
    		powerPane.add(new JLabel("Initial Time"));
    		final JTextField timeOne = new JTextField();
    		powerPane.add(timeOne);
    		powerPane.add(new JLabel("Final Time"));
    		final JTextField timeTwo = new JTextField();
    		powerPane.add(timeTwo);
    		powerPane.add(new JLabel("Velocity"));
    		final JTextField velocity = new JTextField();
    		powerPane.add(velocity);
    		powerPane.add(new JLabel("Force"));
    		final JTextField powerForce = new JTextField();
    		powerPane.add(powerForce);
    
    		final JPanel wavePane = new JPanel();
    
    		final JPanel soundPane = new JPanel();
    
    		final JPanel circuitPane = new JPanel(new GridLayout(3, 2));
    		circuitPane.add(new JLabel("Voltage"));
    		final JTextField voltage = new JTextField();
    		circuitPane.add(voltage);
    		circuitPane.add(new JLabel("Current"));
    		final JTextField current = new JTextField();
    		circuitPane.add(current);
    		circuitPane.add(new JLabel("Resistance"));
    		final JTextField resistance = new JTextField();
    		circuitPane.add(resistance);
    
    		// Add Header to JFrame
    		final JPanel imagePanel = new JPanel();
    		imagePanel.setBackground(Color.DARK_GRAY);
    		BufferedImage bfimage = ImageIO.read(getClass().getResource(
    				"physicsImg.jpg"));
    		ImageLabel image = new ImageLabel(bfimage);
    		JLabel headerLabel = new JLabel(new ImageIcon(bfimage));
    		headerLabel.add(image);
    		imagePanel.add(headerLabel);
    
    		// Add button components and header
    
    		buttonPanel.add(kinematic);
    		buttonPanel.add(dynamics);
    		buttonPanel.add(power);
    		buttonPanel.add(wave);
    		buttonPanel.add(sound);
    		buttonPanel.add(circuit);
    
    		add(mainPanel);
    		mainPanel.add(imagePanel, BorderLayout.CENTER);
    		mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    
    		validate();
    
    		back.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				for (int i = 0; i < buttonArray.length; i++) {
    					mainPanel.remove(buttonArray[i]);
    				}
    				mainPanel.remove(sidePanel);
    				mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    				mainPanel.add(imagePanel, BorderLayout.CENTER);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    			}
    		});
    
    		kinematic.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				sidePanel.add(kinematicPane, BorderLayout.CENTER);
    				sidePanel.add(executePanel, BorderLayout.SOUTH);
    				mainPanel.add(sidePanel, BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.remove(imagePanel);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    }
    				calculate.addActionListener(new ActionListener{
    					public void actionPerformed(ActionEvent ae){
    						kinematics.setDistanceI(Double.parseDouble(dI.getText()));
    					}
    				});
    				
    				/*double distanceI = Double.parseDouble(dI.getText());
    				double distanceF = Double.parseDouble(d0.getText());
    				double v0 = Double.parseDouble(velocityI.getText());
    				double v1 = Double.parseDouble(velocityF.getText());
    				double vA = Double.parseDouble(velocityAverage.getText());
    				double aA = Double.parseDouble(accelerationAverage.getText()); */
    				
    			
    		});
    
    		dynamics.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				sidePanel.add(dynamicsPane, BorderLayout.CENTER);
    				sidePanel.add(executePanel, BorderLayout.SOUTH);
    				mainPanel.add(sidePanel, BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.remove(imagePanel);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    
    				double f = Double.parseDouble(force.getText());
    				double m1 = Double.parseDouble(massOne.getText());
    				double m2 = Double.parseDouble(massTwo.getText());
    				double r1 = Double.parseDouble(radiusOne.getText());
    				double r2 = Double.parseDouble(radiusTwo.getText());
    				double fN = Double.parseDouble(frictionNormal.getText());
    
    			}
    		});
    
    		power.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				sidePanel.add(powerPane, BorderLayout.CENTER);
    				sidePanel.add(executePanel, BorderLayout.SOUTH);
    				mainPanel.add(sidePanel, BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.remove(imagePanel);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    
    				double w = Double.parseDouble(work.getText());
    				double e = Double.parseDouble(energy.getText());
    				double p = Double.parseDouble(powerP.getText());
    				double m = Double.parseDouble(mass.getText());
    				double t1 = Double.parseDouble(timeOne.getText());
    				double t2 = Double.parseDouble(timeTwo.getText());
    				double v = Double.parseDouble(velocity.getText());
    				double pF = Double.parseDouble(powerForce.getText());
    			}
    		});
    
    		wave.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				sidePanel.add(wavePane, BorderLayout.CENTER);
    				sidePanel.add(executePanel, BorderLayout.SOUTH);
    				mainPanel.add(sidePanel, BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.remove(imagePanel);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    			}
    		});
    
    		sound.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				sidePanel.add(soundPane, BorderLayout.CENTER);
    				sidePanel.add(executePanel, BorderLayout.SOUTH);
    				mainPanel.add(sidePanel, BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.remove(imagePanel);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    			}
    		});
    
    		circuit.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				sidePanel.add(circuitPane, BorderLayout.CENTER);
    				sidePanel.add(executePanel, BorderLayout.SOUTH);
    				mainPanel.add(sidePanel, BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.remove(imagePanel);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    
    				double volt = Double.parseDouble(voltage.getText());
    				double cI = Double.parseDouble(current.getText());
    				double rR = Double.parseDouble(resistance.getText());
    			}
    		});
    	System.out.println(kinematics.getDistanceI());
    	}
    }

    ** Image needs resized to fit frame better. If you happen to notice any other errors or poor coding, please let me know! thanks
    Final Project - physics calculator-screen-shot-2014-05-01-5.13.43-pm.jpg
    Last edited by javaStooge; 05-02-2014 at 05:16 AM.

  12. #12
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Final Project - physics calculator

    Now after I retrieve these values from the JTextField, how would I go about returning the calculation to display in a JPanel for the user to view?
    Last edited by javaStooge; 05-02-2014 at 06:14 AM.

  13. #13
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Final Project - physics calculator

    The following in your posted code contains misplaced brackets and braces such that the code cannot be compiled.
    Java Code:
            kinematic.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    ...
                    mainPanel.revalidate();
                    mainPanel.repaint();
    }
                    calculate.addActionListener(new ActionListener{
                        public void actionPerformed(ActionEvent ae){
                            kinematics.setDistanceI(Double.parseDouble(dI.getText()));
                        }
                    });
                     
                    /*double distanceI = Double.parseDouble(dI.getText());
                    ...
                    double aA = Double.parseDouble(accelerationAverage.getText()); */
                     
                 
            });
    "calculate.addActionListener(new ActionListener{" is also missing "()" after ActionListener.

    Since based on the screenshot it appears that you're able to run it, the above is probably a mistake when pasting the code here. It'll be good for you to either edit your posted code (if you haven't made significant changes since), or post your latest working code.

    There are a number of other issues with your code, but I'd defer them for now to answer your main question - displaying the result of calculation.

    Firstly, the ActionListener for the Calculate button obviously needs references, directly or indirectly, to the input fields in order to retrieve the input values. Then you'll also need a result field to display the result.

    The following is some sample code showing the retrieval of field values, calculation, and display of result:
    Java Code:
    // JTextFields need to be final otherwise the ActionListener anonymous inner class
    // cannot reference them.
    final JTextField aTextField = new JTextField(20);
    final JTextField bTextField = new JTextField(20);
    final JTextField resultTextField = new JTextField(20);
    resultTextField.setEditable(false);  // Set this non-editable to prevent user input.
    ...
    JButton calcButton = new JButton("Calculate");
    calcButton.addActionListener(new ActionListener() {
        
        @Override
        public void actionPerformed(ActionEvent e) {
            int a = 0, b = 0;
            try {
                a = Integer.parseInt(aTextField.getText());
                b = Integer.parseInt(bTextField.getText());
            }
            catch (NumberFormatException nfe) {  // In case user enters non-integers.
                nfe.printStackTrace();
                resultTextField.setText("Invalid: " + nfe.getMessage());
                
                return;
            }
    
            resultTextField.setText(Integer.toString(a + b));
        }
    });
    I'd advocate displaying the result on the same panel as the input fields so that the user can see the input values and result all in the same place.
    javaStooge likes this.

  14. #14
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Final Project - physics calculator

    Quote Originally Posted by jashburn View Post
    There are a number of other issues with your code, but I'd defer them for now to answer your main question - displaying the result of calculation.
    Of this I have no doubt!

    Quote Originally Posted by jashburn View Post
    Firstly, the ActionListener for the Calculate button obviously needs references, directly or indirectly, to the input fields in order to retrieve the input values. Then you'll also need a result field to display the result.

    The following is some sample code showing the retrieval of field values, calculation, and display of result:
    Java Code:
    // JTextFields need to be final otherwise the ActionListener anonymous inner class
    // cannot reference them.
    final JTextField aTextField = new JTextField(20);
    final JTextField bTextField = new JTextField(20);
    final JTextField resultTextField = new JTextField(20);
    resultTextField.setEditable(false);  // Set this non-editable to prevent user input.
    ...
    JButton calcButton = new JButton("Calculate");
    calcButton.addActionListener(new ActionListener() {
        
        @Override
        public void actionPerformed(ActionEvent e) {
            int a = 0, b = 0;
            try {
                a = Integer.parseInt(aTextField.getText());
                b = Integer.parseInt(bTextField.getText());
            }
            catch (NumberFormatException nfe) {  // In case user enters non-integers.
                nfe.printStackTrace();
                resultTextField.setText("Invalid: " + nfe.getMessage());
                
                return;
            }
    
            resultTextField.setText(Integer.toString(a + b));
        }
    });
    I'd advocate displaying the result on the same panel as the input fields so that the user can see the input values and result all in the same place.
    Since my last post, I did manage to figure out the code similar to what you have used here. Except, based on the layout of my GUI, one of the JTF's will be left blank, since one of the variables will be unknown. For this reason, I need to test which field is blank and assign a variable that can be passed to kinematic class. Within this class the unknown will represent an integer value in a switch/case statement where it will calculate and return the result.

    I think you are right about displaying the result...best to keep it all in one panel. Besides, I think I'm already in over my head! haha

  15. #15
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Final Project - physics calculator

    So you're okay now with displaying the result of the calculation, I presume?

  16. #16
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Final Project - physics calculator

    Quote Originally Posted by jashburn View Post
    So you're okay now with displaying the result of the calculation, I presume?
    No, I'm not. What other issues did you find exactly?

  17. #17
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Question Re: Final Project - physics calculator

    Alright, hopefully I will have some luck on this forum and someone can help me get past this issue: There are a lot of variables in physics and it is easy to manipulate equations to find the unknown variable. Well, in my program I have the problem of determining which equation to solve for an unknown. You can see I have a timeF, timeI, time, distanceI, distanceF, distance...etc...variables. The way that I have written the code, it will scan for an empty() JTF and assign it as the unknown variable (not sure I can do it this way anymore). At that point, the value is passed into the correct calculation method to process the equation based on a series of case statements in each method. Can someone please help me figure this part out. I think once I get past this issue...the rest will fall into place.

    I know the button arrays I have set up will process correctly in the checkJTF method...and I will need to account for that...but right now my focus is on getting the program to properly calculate the kinematics equations and display the result in the JPanel.

    Java Code:
     public class PhysicsFrame extends JFrame {
        //Kinematic variables
         private static float distanceI, distanceF, velocityI, velocityF,
        velocityAverage, acceleration, time, timeF, timeI, result;
         //Power variables
         private static float power, energyKin, energyGrav, energyTot, work, timeOne, timeTwo, distanceOne,
    	distanceTwo, velocityOne, velocityTwo, mass, gravity, heightF, heightI;
         //Dynamics variables
         private static float netForce, weight, friction, radius, radiusOne, radiusTwo,
    	massOne, massTwo, frictionConstant, gravForce, gravConstant, frictionNormal;
         private static int unknownVar = 0;
            
    	public PhysicsFrame() throws IOException {
    		super("PhysAssist");
    
    		// Buttons
    		JButton kinematic = new JButton("Kinematic");
    		kinematic.setMnemonic(KeyEvent.VK_K);
    		JButton dynamics = new JButton("Dynamics");
    		dynamics.setMnemonic(KeyEvent.VK_D);
    		JButton power = new JButton("Power and Energy");
    		power.setMnemonic(KeyEvent.VK_E);
    		JButton wave = new JButton("Wave");
    		wave.setMnemonic(KeyEvent.VK_W);
    		JButton sound = new JButton("Sound");
    		sound.setMnemonic(KeyEvent.VK_S);
    		JButton circuit = new JButton("Circuit");
    		circuit.setMnemonic(KeyEvent.VK_C);
    
    		// Execute Buttons
    		JButton calculate = new JButton("Calculate");
    		calculate.setMnemonic(KeyEvent.VK_ENTER);
    		calculate.setFont(new java.awt.Font("Georgia", Font.BOLD, 16));
    		calculate.setForeground(Color.BLUE);
    		JButton back = new JButton("Back");
    		back.setMnemonic(KeyEvent.VK_ESCAPE);
    		back.setFont(new java.awt.Font("Georgia", Font.ITALIC, 16));
    		back.setForeground(Color.RED);
    
    		// Button Array Characteristics
    		final JButton[] buttonArray = { kinematic, dynamics, power, wave,
    				sound, circuit };
    		for (int i = 0; i < buttonArray.length; i++) {
    			buttonArray[i].setFont(new java.awt.Font("Georgia", Font.BOLD, 16));
    			buttonArray[i].setForeground(Color.darkGray);
    			buttonArray[i].setBackground(Color.GRAY);
    		}
    
    		// Panels
    		final JPanel mainPanel = new JPanel();
    		mainPanel.setLayout(new BorderLayout());
    
    		final JPanel executePanel = new JPanel();
    		executePanel.setLayout(new FlowLayout());
    		executePanel.add(back);
    		executePanel.add(calculate);
    
    		final JPanel sidePanel = new JPanel();
    		sidePanel.setLayout(new BorderLayout());
    
    		final JPanel combinationPanel = new JPanel();
    		combinationPanel.setLayout(new GridLayout(2, 1));
    
    		final JPanel resultPanel = new JPanel();
    		Border border = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
    		TitledBorder titled = BorderFactory.createTitledBorder(border,
    				"Results", TitledBorder.DEFAULT_JUSTIFICATION,
    				TitledBorder.DEFAULT_POSITION, new Font("Synchro LET",
    						Font.BOLD, 14), Color.WHITE);
    		resultPanel.setBorder(titled);
    		resultPanel.setLayout(new GridLayout(4, 2));                
    		resultPanel.setBackground(Color.DARK_GRAY); 
                 /////////////////////////////////////////////////
    		final JPanel buttonPanel = new JPanel();
    		buttonPanel.setLayout(new GridLayout(6, 1));
    		buttonPanel.setBackground(Color.DARK_GRAY);
    
    	/////////// Kinematic Pane and Contents////////////////
    		final JPanel kinematicPanel = new JPanel(new GridLayout(8, 2));
    		kinematicPanel.add(new JLabel("Initial Distance"));
    		final JTextField dI = new JTextField();
    		kinematicPanel.add(dI);
    		kinematicPanel.add(new JLabel("Final Distance"));
    		final JTextField d0 = new JTextField();
    		kinematicPanel.add(d0);
    		kinematicPanel.add(new JLabel("Initial Velocity"));
    		final JTextField velocityI = new JTextField();
    		kinematicPanel.add(velocityI);
    		kinematicPanel.add(new JLabel("Final Velocity"));
    		final JTextField velocityF = new JTextField();
    		kinematicPanel.add(velocityF);
    		kinematicPanel.add(new JLabel("Average Velocity"));
    		final JTextField velocityAverage = new JTextField();
    		kinematicPanel.add(velocityAverage);
    		kinematicPanel.add(new JLabel("Acceleration"));
    		final JTextField accelerationAverage = new JTextField();
    		kinematicPanel.add(accelerationAverage);
    		kinematicPanel.add(new JLabel("Time"));
    		final JTextField initialTime = new JTextField();
    		kinematicPanel.add(initialTime);
    		/*kinematicPane.add(new JLabel("Final Time"));
    		final JTextField finalTime = new JTextField();
    		kinematicPane.add(finalTime);*/
    
    		final JTextField[] kinematicsJT = { dI, d0, velocityI, velocityF,
    				velocityAverage, accelerationAverage, initialTime};
    
    		
    		
    		
    		
    	////////// Dynamics Panel//////////////
    		final JPanel dynamicsPanel = new JPanel(new GridLayout(6, 2));
    		dynamicsPanel.add(new JLabel("Force"));
    		final JTextField force = new JTextField();
    		dynamicsPanel.add(force);
    		dynamicsPanel.add(new JLabel("Mass One"));
    		final JTextField massOne = new JTextField();
    		dynamicsPanel.add(massOne);
    		dynamicsPanel.add(new JLabel("Mass One"));
    		final JTextField massTwo = new JTextField();
    		dynamicsPanel.add(massTwo);
    		dynamicsPanel.add(new JLabel("Radius One"));
    		final JTextField radiusOne = new JTextField();
    		dynamicsPanel.add(radiusOne);
    		dynamicsPanel.add(new JLabel("Radius Two"));
    		final JTextField radiusTwo = new JTextField();
    		dynamicsPanel.add(radiusTwo);
    		dynamicsPanel.add(new JLabel("Normal Friction"));
               	final JTextField frictionNormal = new JTextField();
    		dynamicsPanel.add(frictionNormal);
                    
                    final JTextField[] dynamicsJT = {force, massOne,massTwo, radiusOne,
                                          radiusTwo, frictionNormal};
    
    	/////////Power, Work, Energy Pane/////////////
    		final JPanel powerPanel = new JPanel(new GridLayout(7, 2));
    		powerPanel.add(new JLabel("Work"));
    		final JTextField work = new JTextField();
    		powerPanel.add(work);
    		powerPanel.add(new JLabel("Energy"));
    		final JTextField energy = new JTextField();
    		powerPanel.add(energy);
    		powerPanel.add(new JLabel("Power"));
    		final JTextField powerTF = new JTextField();
    		powerPanel.add(powerTF);
    		powerPanel.add(new JLabel("Mass"));
    		final JTextField mass = new JTextField();
    		powerPanel.add(mass);
    		powerPanel.add(new JLabel("Initial Time"));
    		final JTextField timeOne = new JTextField();
    		powerPanel.add(timeOne);
    		powerPanel.add(new JLabel("Final Time"));
    		final JTextField timeTwo = new JTextField();
    		powerPanel.add(timeTwo);
    		powerPanel.add(new JLabel("Velocity"));
    		final JTextField velocity = new JTextField();
    		powerPanel.add(velocity);
    		powerPanel.add(new JLabel("Force"));
    		final JTextField powerForce = new JTextField();
    		powerPanel.add(powerForce);
                
                    final JTextField[] powerJT = {work, energy, powerTF, mass, timeOne,
                    timeTwo, velocity, powerForce};
               ////////////WAVE PANE//////////////
    		final JPanel wavePanel = new JPanel();
    		wavePanel.add(new JLabel());
              ////////////SOUND PANE//////////////
    		final JPanel soundPane = new JPanel();
              //////////////CIRCUIT PANE//////////
    		final JPanel circuitPanel = new JPanel(new GridLayout(3, 2));
    		circuitPanel.add(new JLabel("Voltage"));
    		final JTextField voltage = new JTextField();
    		circuitPanel.add(voltage);
    		circuitPanel.add(new JLabel("Current"));
    		final JTextField current = new JTextField();
    		circuitPanel.add(current);
    		circuitPanel.add(new JLabel("Resistance"));
    		final JTextField resistance = new JTextField();
    		circuitPanel.add(resistance);
    
                /////////// Add Header to JFrame////////////
    		final JPanel imagePanel = new JPanel();
    		//imagePanel.setBackground(Color.DARK_GRAY);
    		//BufferedImage bfimage = ImageIO.read(getClass().getResource(
    				//"physicsImg.jpg"));
    		//ImageLabel image = new ImageLabel(bfimage);
    		//JLabel headerLabel = new JLabel(new ImageIcon(bfimage));
    		//headerLabel.add(image);
    		//imagePanel.add(headerLabel);
    
                ///////////// ADD BUTTON PANEL COMPONENTS////////////////
    		buttonPanel.add(kinematic);
    		buttonPanel.add(dynamics);
    		buttonPanel.add(power);
    		buttonPanel.add(wave);
    		buttonPanel.add(sound);
    		buttonPanel.add(circuit);
    
    		add(mainPanel);
    		mainPanel.add(imagePanel, BorderLayout.CENTER);
    		mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    
    		validate();
                ///////////////ACTION LISTENERS///////////////
    		back.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				for (int i = 0; i < buttonArray.length; i++) {
    					mainPanel.remove(buttonArray[i]);
    				}
    				mainPanel.remove(sidePanel);
    				mainPanel.remove(combinationPanel);
    				mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    				mainPanel.add(imagePanel, BorderLayout.CENTER);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    			}
    		});
    
    		calculate.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				PhysicsFrame.checkJTF(kinematicsJT);
    				if(unknownVar >= 0 && unknownVar <= 6){
                                    try {
    					float distanceI = Float.parseFloat(dI.getText());
    					PhysicsFrame.setDistanceI(distanceI);
    					float distanceF = Float.parseFloat(d0.getText());
    					PhysicsFrame.setDistanceF(distanceF);
    					float v0 = Float.parseFloat(velocityI.getText());
    					PhysicsFrame.setVelocityI(v0);
    					float v1 = Float.parseFloat(velocityF.getText());
    					PhysicsFrame.setVelocityF(v1);
    					float vA = Float.parseFloat(velocityAverage.getText());
    					PhysicsFrame.setVelocityAverage(vA);
    					float aA = Float.parseFloat(accelerationAverage.getText());
    					PhysicsFrame.setAcceleration(aA);
                                            float time = Float.parseFloat(initialTime.getText());
                                            PhysicsFrame.setTime(time);
                                            calcKinematic(getUnknown());
    
    				} catch (NumberFormatException e) {
    					JOptionPane.showMessageDialog(null, "Numbers Only");
    				}
                                }
                                    PhysicsFrame.checkJTF(dynamicsJT);
                                    if(unknownVar >=7 && unknownVar <= 13){
                                        try{
                                            float f = Float.parseFloat(force.getText());
    				float m1 = Float.parseFloat(massOne.getText());
                                    PhysicsFrame.setMassOne(m1);
    				float m2 = Float.parseFloat(massTwo.getText());
                                    PhysicsFrame.setMassTwo(m2);
    				float r1 = Float.parseFloat(radiusOne.getText());
                                    PhysicsFrame.setRadiusOne(r1);
    				float r2 = Float.parseFloat(radiusTwo.getText());
                                    PhysicsFrame.setRadiusTwo(r2);
    				float fN = Float.parseFloat(frictionNormal.getText());
                                    PhysicsFrame.setFrictionNormal(fN);
                                        }catch(NumberFormatException e){
                                            JOptionPane.showMessageDialog(null, "Numbers Only");
                                        }
                                        
                                    }
                                    PhysicsFrame.checkJTF(powerJT);
                                    if(unknownVar >=14 && unknownVar <= 22){
                                        
                                    }
                                   // final JLabel unknownLabel = new JLabel("Unknown" + );
                                   // resultPanel.add(unknownLabel);
                                    //resultPanel.revalidate();
    				//resultPanel.repaint();
                           }
    		});
    
    		kinematic.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				sidePanel.add(kinematicPanel, BorderLayout.CENTER);
    				sidePanel.add(executePanel, BorderLayout.SOUTH);
    				combinationPanel.add(buttonPanel);
    				combinationPanel.add(resultPanel);
    				mainPanel.add(combinationPanel, BorderLayout.WEST);
    				mainPanel.add(sidePanel, BorderLayout.CENTER);
    				mainPanel.remove(imagePanel);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    			}
    		});
    
    		dynamics.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				sidePanel.add(dynamicsPanel, BorderLayout.CENTER);
    				sidePanel.add(executePanel, BorderLayout.SOUTH);
    				mainPanel.add(sidePanel, BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.remove(imagePanel);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    			}
    		});
    
    		power.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				sidePanel.add(powerPanel, BorderLayout.CENTER);
    				sidePanel.add(executePanel, BorderLayout.SOUTH);
    				mainPanel.add(sidePanel, BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.remove(imagePanel);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    
    				float w = Float.parseFloat(work.getText());
    				float e = Float.parseFloat(energy.getText());
    				float p = Float.parseFloat(powerTF.getText());
    				float m = Float.parseFloat(mass.getText());
    				float t1 = Float.parseFloat(timeOne.getText());
    				float t2 = Float.parseFloat(timeTwo.getText());
    				float v = Float.parseFloat(velocity.getText());
    				float pF = Float.parseFloat(powerForce.getText());
    			}
    		});
    
    		wave.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				sidePanel.add(wavePanel, BorderLayout.CENTER);
    				sidePanel.add(executePanel, BorderLayout.SOUTH);
    				mainPanel.add(sidePanel, BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.remove(imagePanel);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    			}
    		});
    
    		sound.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				sidePanel.add(soundPane, BorderLayout.CENTER);
    				sidePanel.add(executePanel, BorderLayout.SOUTH);
    				mainPanel.add(sidePanel, BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.remove(imagePanel);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    			}
    		});
    
    		circuit.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				sidePanel.add(circuitPanel, BorderLayout.CENTER);
    				sidePanel.add(executePanel, BorderLayout.SOUTH);
    				mainPanel.add(sidePanel, BorderLayout.CENTER);
    				mainPanel.add(buttonPanel, BorderLayout.WEST);
    				mainPanel.remove(imagePanel);
    				mainPanel.revalidate();
    				mainPanel.repaint();
    
    				float volt = Float.parseFloat(voltage.getText());
    				float cI = Float.parseFloat(current.getText());
    				float rR = Float.parseFloat(resistance.getText());
    			}
    		});
    		// System.out.println(kinematics.getDistanceI());
    	}
       ///////////FIND UNKNOWN VARIABLE METHOD////////////     
    	static void checkJTF(JTextField[] a) {
    		for (int i = 0; i < a.length; i++) {
    			if (a[i].getText().isEmpty()) {
                                    int unknownVar = i;
    				PhysicsFrame.setUnknown(unknownVar);
    				System.out.println("checkJTF: " + unknownVar);
                                    a[i].setText("0");
    			}
    		}
    	}
        /////////////SET/GET VARIABLES//////////////
    	public static void setDistanceI(float dI){
    		distanceI = dI;
    	}
    	public static void setDistanceF(float dF){
    		distanceF = dF;
    	}
    	public static void setVelocityI(float vI){
    		velocityI = vI;
    	}
    	public static void setVelocityF(float F){
    		velocityF = F;
    	}
    	public static void setVelocityAverage(float vAverage){
    		velocityAverage = vAverage; 
    	}
    	public static void setAcceleration(float accel){
    		acceleration = accel;
    	}
            public static void setTime(float timeTF){
                time = timeTF;
            }
            public static void setMassOne(float m1){
                massOne = m1;
            }
            public static void setMassTwo(float m2){
                massTwo = m2;
            }
            public static void setRadiusOne(float r1){
                radiusOne = r1;
            }
             public static void setRadiusTwo(float r2){
                radiusTwo = r2;
            }
              public static void setFrictionNormal(float fn){
                frictionNormal = fn;
            }
    	public static void setUnknown(int unknown) {
                    unknownVar = unknown;
        }
            public static void setResult(float results){
                result = results;
            }
            
    
        public int getUnknown() {
            return unknownVar;
        }
        public static int getResult() {
            return unknownVar;
        }
        //////////////CALCULATION METHODS////////////////
            public static void calcKinematic(int unknown){
            
                if (unknown >= 0 && unknown <= 7) {
                switch (unknown) {
                    case 0://velocifyI
                        result = velocityF - acceleration * (timeF - timeI);
                        System.out.println("Case 0");
                        System.out.println("VerifyAcceleraion : " + acceleration);
                        System.out.println("VerifyVelocityI : " + velocityI);
                        System.out.println("Restult" + result);
                        
                        break;
                    case 1: //distanceF
                        result = distanceI + ((velocityI + velocityF) / 2);
                        System.out.println("Case 1");
                        System.out.println("VerifyDistance: " + distanceI);
                        System.out.println("VerifyVelocityI : " + velocityI);
                        System.out.println("VerifyVelocityF :" + velocityF);
                        System.out.println("Result: " + result);
                        break;
                    case 2: //distanceF
                        result = (float) (velocityF * (timeF - timeI) + 0.5 * (acceleration * ((time) * (time))));
                        System.out.println("Case 2");
                        System.out.println("Restult" + result);
                        break;
                    case 3://velocityF
                        result = (float) Math.sqrt((velocityI * velocityI) + 2 * acceleration * (distanceF - distanceI));
                                System.out.println("Case 3");
                                System.out.println("VerifyRestult" + result);
                                System.out.println("VerifyVelocity : " + velocityI);                            
                        break;       
                    case 4: //velocityAverage
                        result = (distanceF - distanceI) / (time);
                        System.out.println("Case4");
                        System.out.println("Restult " + result);
                        break;
                    case 5: //acceleration
                        result = (velocityF - velocityI)/time;
                        System.out.println("Case 5");
                        System.out.println("Restult " + result);
                    }
                PhysicsFrame.setResult((result));
                
                }
            }
            
            public static void calcDynamics(int unknown){
                if(unknown >= 7 && unknown <= 13){
                    switch(unknown){
                        case 6: //
                        case 7:
                        case 8:
                        case 9:
                        case 10:
                        case 11:
                        case 12:
                    }
                }
            }
            public static void calcPower(int unknown){
                if(unknown >= 14 && unknown <= 22){
                    switch(unknown){
                        case 14: //
                        case 15:
                        case 16:
                        case 17:
                        case 18:
                        case 19:
                        case 20:
                        case 21:
                        case 22: 
                        
                    }
                }
            }                 
    }
    Last edited by javaStooge; 05-04-2014 at 09:01 PM.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Final Project - physics calculator

    Is this the same project: Testing double null
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Final Project - physics calculator

    Yes, this thread was about a different topic for the same program, but I wanted to post my question on this site in hoping to get a different result. There are different people on each site, no?
    Last edited by javaStooge; 05-04-2014 at 11:15 PM.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Final Project - physics calculator

    It is better if you tell everyone about all the sites the question is posted on. Many of us don't like wasting time answering questions that have already been answered. Also it allows more people to see everything that has been posted.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Final Java Project
    By ExX in forum New To Java
    Replies: 1
    Last Post: 05-06-2012, 10:57 AM
  2. Final Year Project
    By srrehman in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 09-14-2011, 02:00 PM
  3. Help me? Class final project.
    By Blue Energy in forum Advanced Java
    Replies: 2
    Last Post: 05-14-2011, 07:39 AM
  4. GUI Final Project help
    By Unknown in forum New To Java
    Replies: 21
    Last Post: 12-30-2008, 08:58 PM
  5. help to complete final part of project
    By dirtycash in forum New To Java
    Replies: 6
    Last Post: 12-31-2007, 07:21 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
  •