package physicspractice;
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.*;
public class Frame extends JFrame{
//Objects
final Kinematics kinematics = new Kinematics();
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 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 buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(6, 1));
buttonPanel.setBackground(Color.DARK_GRAY);
// Kinematic Pane and Contents
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);
final JTextField [] kinematicsJT = {dI, d0, velocityI, velocityF,
velocityAverage, accelerationAverage};
// 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();
}
});
calculate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
kinematics.checkJTF(kinematicsJT);
try{
float distanceI = Float.parseFloat(dI.getText());
kinematics.setDistanceI(distanceI);
float distanceF = Float.parseFloat(d0.getText());
float v0 = Float.parseFloat(velocityI.getText());
float v1 = Float.parseFloat(velocityF.getText());
float vA = Float.parseFloat(velocityAverage.getText());
float aA = Float.parseFloat(accelerationAverage.getText());
} catch(NumberFormatException ex){
JOptionPane.showConfirmDialog(null, "Numbers Only");
}
}
});
kinematic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
sidePanel.add(kinematicPane, BorderLayout.CENTER);
sidePanel.add(executePanel, BorderLayout.SOUTH);
mainPanel.add(buttonPanel, 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(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();
float f = Float.parseFloat(force.getText());
float m1 = Float.parseFloat(massOne.getText());
float m2 = Float.parseFloat(massTwo.getText());
float r1 = Float.parseFloat(radiusOne.getText());
float r2 = Float.parseFloat(radiusTwo.getText());
float fN = Float.parseFloat(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();
float w = Float.parseFloat(work.getText());
float e = Float.parseFloat(energy.getText());
float p = Float.parseFloat(powerP.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(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();
float volt = Float.parseFloat(voltage.getText());
float cI = Float.parseFloat(current.getText());
float rR = Float.parseFloat(resistance.getText());
}
});
}
public void checkJTF(JTextField[] a){
for (int i = 0 ; i < a.length ; i++){
if(a[i].getText().isEmpty()){
int unknown = i;
kinematics.setUnknown(unknown);
}
}
}
}