Special Relativity Calculator
Java source code for Special Relativity Calculator
//Filename = "SrCalc.java"
//Copyright 2003 S.Edgeworth.
//Non-profit use with acknowledgement is permitted.
//Coded using TextPad and j2sdk1.4.1_02.
//To do: make accessible by passing browser accesibility preferences into applet as params.
//To do: clear output fields when user starts to enter a new speed.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.math.*;
import java.text.*;
import javax.swing.border.*;
public class SrCalc extends JApplet
{
final String[] unitDescription=
{
"miles per hour",
"miles per second",
"kilometres per hour",
"kilometres per second",
"metres per second"
};
final double[] unitValue= //the speed of light in various units
{
67061672.33, // mile/h
186282.4231, // mile/s
1079253000, // km/h
299792.5, // km/s
299792500 // m/s
};
JTextField speedText;
JTextField unitText;
JTextField lengthText;
JTextField clockText;
JTextField massText;
JComboBox unitComboBox;
JLabel commentLabel;
public void init()
{
Container content=getContentPane();
BoxLayout contentLayout=new BoxLayout(content, BoxLayout.Y_AXIS);
content.setLayout(contentLayout);
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
JPanel panel4=new JPanel();
JPanel panel5=new JPanel();
JPanel panel6=new JPanel();
panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
panel3.setLayout(new FlowLayout(FlowLayout.LEFT));
panel4.setLayout(new FlowLayout(FlowLayout.LEFT));
panel5.setLayout(new FlowLayout(FlowLayout.LEFT));
panel6.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel titleLabel=new JLabel("Special Relativity Calculator");
JLabel speedLabel=new JLabel("Speed =");
JLabel thatsLabel=new JLabel("that's");
JLabel lengthLabel=new JLabel("Length =");
JLabel clockLabel=new JLabel("Clockrate =");
JLabel massLabel=new JLabel("Mass =");
JLabel percentLabel1=new JLabel("% of the speed of light");
commentLabel=new JLabel("");
unitComboBox=new JComboBox(unitDescription);
unitComboBox.setEditable(false);
speedText=new JTextField("0");
unitText=new JTextField();
lengthText=new JTextField();
clockText=new JTextField();
massText=new JTextField();
Dimension dim1=new Dimension(150,20);
Dimension dim2=new Dimension(200,20);
speedText.setPreferredSize(dim1);
unitText.setPreferredSize(dim2);
lengthText.setPreferredSize(dim2);
clockText.setPreferredSize(dim2);
massText.setPreferredSize(dim2);
unitText.setEditable(false);
lengthText.setEditable(false);
clockText.setEditable(false);
massText.setEditable(false);
JButton goButton=new JButton("Go");
class ActionHandler implements ActionListener
{
final public static int GO_BUTTON = 101;
final public static int SPEED_TEXT = 102;
private int buttonID;
public ActionHandler(int buttonID)
{
this.buttonID = buttonID;
}
public void actionPerformed(ActionEvent e)
{
if(buttonID==GO_BUTTON)
{
calculate();
}
else if(buttonID==SPEED_TEXT)
{
calculate();
}
}
}
class ItemHandler implements ItemListener
{
final public static int UNIT_COMBOBOX = 110;
private int buttonID;
public ItemHandler(int buttonID)
{
this.buttonID = buttonID;
}
public void itemStateChanged(ItemEvent e)
{
if( buttonID==UNIT_COMBOBOX )
{
calculate();
}
}
}
goButton.addActionListener(new ActionHandler(ActionHandler.GO_BUTTON));
speedText.addActionListener(new ActionHandler(ActionHandler.SPEED_TEXT));
unitComboBox.addItemListener(new ItemHandler(ItemHandler.UNIT_COMBOBOX));
panel1.add(speedLabel);
panel1.add(speedText);
panel1.add(percentLabel1);
panel2.add(thatsLabel);
panel2.add(unitText);
panel2.add(unitComboBox);
panel3.add(lengthLabel);
panel3.add(lengthText);
panel4.add(clockLabel);
panel4.add(clockText);
panel5.add(massLabel);
panel5.add(massText);
panel6.add(goButton);
panel6.add(commentLabel);
content.add(panel1);
content.add(panel2);
content.add(panel3);
content.add(panel4);
content.add(panel5);
content.add(panel6);
calculate();
}
private void calculate()
{
int unitIndex=unitComboBox.getSelectedIndex();
String s=speedText.getText();
double speed=Double.parseDouble(s);
speed=speed/100;
if( speed<0 || speed>=1)
{
invalid();
return;
}
double unitSpeed, length, clock, mass;
unitSpeed=speed*unitValue[unitIndex];
unitSpeed=Math.round(unitSpeed);
int intUnitSpeed=(int)unitSpeed;
length=Math.sqrt(1.0-Math.pow(speed, 2.0));
clock=length;
mass=1/length;
length*=100.0;
clock*=100.0;
mass*=100.0;
NumberFormat numberFormat=NumberFormat.getInstance();
if(numberFormat instanceof DecimalFormat)
{
numberFormat.setMinimumFractionDigits(0);
numberFormat.setMaximumFractionDigits(6);
numberFormat.setGroupingUsed(false);
}
String unitSpeedString=Integer.toString(intUnitSpeed);
String lengthString=numberFormat.format(length);
String clockString=numberFormat.format(clock);
String massString=numberFormat.format(mass);
unitText.setText(unitSpeedString);
lengthText.setText(lengthString+"%");
clockText.setText(clockString+"%");
massText.setText(massString+"%");
commentLabel.setText("");
}
private void clear()
{
unitText.setText("");
lengthText.setText("");
clockText.setText("");
massText.setText("");
commentLabel.setText("");
}
private void invalid()
{
clear();
commentLabel.setText(" That's impossible");
}
}
Special Relativity Calculator