vendredi 2 janvier 2015

Why isn't it solving the equations when selected by Radio Button?

So I have a method called equations that should check to see if any of the values are 0, but they don't? As far as I'm concerned they aren't running at all when I select them with the Radio Button. Why is this? It just prints the value out of displacement as 0. Idk what to do here? All help is appreciated!



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import java.applet.Applet;

public class RadioButton extends JPanel {
//Values that can be entered at the beginning, the code will explain to you what this means and how you can find the other values
//If you don't know one of the values, make it 0

int xDisplacement = 0;
int xVAvg = 8;
int xTime = 8 ;

public void equations() {
if (xDisplacement == 0) {
//solve for displacement
int xDisplacement = xVAvg * xTime;
}
if (xVAvg == 0) {
int xVAvg = xDisplacement / xTime;
}
if (xTime == 0) {
int xTime = xDisplacement / xVAvg;
}
}





static JFrame frame;

JLabel pic;
RadioListener myListener = null;
protected JRadioButton displacement;
protected JRadioButton vAvg;
protected JRadioButton time;
public RadioButton() {



// Create the radio buttons
displacement = new JRadioButton("Displacement");
displacement.setMnemonic(KeyEvent.VK_N);
displacement.setActionCommand("displacement");
//Displacement Button, set to automatically be clicked

vAvg = new JRadioButton("Average Velocity");
vAvg.setMnemonic(KeyEvent.VK_A);
vAvg.setActionCommand("averagevelocity");
//Acceleration Button

time = new JRadioButton("Change in time");
time.setMnemonic(KeyEvent.VK_S);
time.setActionCommand("deltaT");
//The change in time button


// Creates the group of buttons
ButtonGroup group = new ButtonGroup();
group.add(displacement);
group.add(vAvg);
group.add(time);

myListener = new RadioListener();
displacement.addActionListener(myListener);
vAvg.addActionListener(myListener);
time.addActionListener(myListener);


// Set up the picture label
pic = new JLabel(new ImageIcon(""+"numbers" + ".jpg")); //Set the Default Image

pic.setPreferredSize(new Dimension(177, 122));


// Puts the radio buttons down
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 1));
panel.add(displacement);
panel.add(vAvg);
panel.add(time);


setLayout(new BorderLayout());
add(panel, BorderLayout.WEST);
add(pic, BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(40,40,40,40));
}



//Listening to the buttons
class RadioListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
pic.setIcon(new ImageIcon(""+e.getActionCommand() + ".jpg"));
equations();
running();


}
}
public static void main(String s[]) {
frame = new JFrame("∆x = Vavg * time");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});

frame.getContentPane().add(new RadioButton(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}


public void running() {
if ( displacement.isSelected()) {
//Option 1
System.out.println("The distance traveled on the x axis in meters is " + xDisplacement);
System.out.println("You can find the Average Velocity by dividing this number by time or find the time by dividing this number by the Average Velocity");
System.out.println("----------> ");
System.out.println("^ " + xDisplacement );
}
if ( vAvg.isSelected()) {
//Option 2
System.out.println("The average velocity in Meters per Second is " + xVAvg);
System.out.println("You can find the displacement by multiplying the time and this number together or to find the time, just divide the displacement by this number");
System.out.println("----------> ");
System.out.println("^ " + xVAvg );
}

if (time.isSelected()) {
//Option 3
System.out.println("The time in seconds is " + xTime);
System.out.println("You can find the displacement by multiplying the velocity times this number or you can find the average velocity by dividing the displacement by this number");
System.out.println("----------> ");
System.out.println("^ " + xTime );
}
}



}

Aucun commentaire:

Enregistrer un commentaire