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());
}
}