//Einstein.java import java.applet.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.awt.Image; import Reality; import PerpRods; import EinsteinObject; import SpacePanel; import Stars; import EINtutor; import CarLight; public class Einstein extends Applet implements Runnable { Thread animation; static int REFRESH_RATE = 50; Graphics offscr,offscr2,obj; Image image,image2,obj_im; int width= 500, height=300; public Reality reality; Panel ButtonPanel,LeftPanel,RightPanel; Button START,STEP,RESET,b; boolean started = false; boolean stepped = false; boolean pushed = false; double t_inc = 1; public int topValue; Scrollbar top_scr; Choice exp_choice; SpacePanel space=null; TimePanel time = null; Label top_lab; Button flipSwitch; Label flipLabel; boolean frame = Reality.OBSERVER_FRAME; EinsteinObject mover; EINtutor EINtut; public void PanelInit() { Font little = new Font("Serif",Font.PLAIN,10); Font temp = getFont(); setFont(little); LeftPanel = new Panel(new GridBagLayout()); RightPanel = new Panel(new GridBagLayout()); LeftPanel.setBackground(Color.white); RightPanel.setBackground(Color.white); GridBagConstraints c = new GridBagConstraints(); top_lab = new Label("Object v= .00 c",Label.RIGHT); top_lab.setForeground(Color.blue); top_scr = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,99); top_scr.addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent ae) { topValue = ae.getValue(); double value = .01 * topValue; if (mover != null) { mover.setV(-value); reality.setVelocity(value); } Double d = new Double(value); String sv = d.toString(); int start =0,end = sv.length(); if (sv.charAt(0) == '0') start = 1; if ((end - start) > 3) end = start + 3; top_lab.setText("Object v= "+sv.substring(start,end)+" c"); if (time != null) time.repaint(); if (space != null) space.repaint(); } } ); c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(5,5,5,5); c.gridx =0; c.gridy=0;c.gridwidth=3;c.gridheight=1; c.weightx = 0; c.weighty=0; LeftPanel.add(top_lab,c); c.gridx = 3; c.gridwidth=7;c.weightx = 1; LeftPanel.add(top_scr,c); c.gridx = 0; c.gridwidth=10; c.fill = GridBagConstraints.BOTH; c.insets = new Insets(0,5,5,5); space = new SpacePanel(-30,5,60,10,this); time = new TimePanel(this); c.gridy=1; c.gridheight=10; c.weightx = 1; c.weighty=1; LeftPanel.add(space,c); RightPanel.add(time,c); c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(0,5,5,5); flipSwitch = new Button("Switch Frames"); flipLabel = new Label("In Observer's Frame"); flipLabel.setForeground(Color.red); flipSwitch.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { if (frame == Reality.OBJECT_FRAME) { frame = Reality.OBSERVER_FRAME; space.xstart = (int)(reality.theirX)+space.ixstart; space.s = new Stars(space); flipLabel.setText("In Observer's Frame"); flipLabel.setForeground(Color.red); } else { frame = Reality.OBJECT_FRAME; space.xstart = (int)(mover.getReality().myX)+space.ixstart; space.s = new Stars(space); flipLabel.setText("In Object's Frame"); flipLabel.setForeground(Color.blue); } flipLabel.repaint(); if (!started) { time.repaint(); space.repaint(); } } }); c.gridy = 12; c.gridheight =1; c.weightx = 0; c.weighty=0; c.gridwidth=4; LeftPanel.add(flipSwitch,c); c.insets = new Insets(0,5,5,5); c.gridx = 4; c.gridwidth=6;c.weightx=1; LeftPanel.add(flipLabel,c); c.gridx = 0; c.gridwidth=10; exp_choice = new Choice(); exp_choice.add("Exp 1: Perpendicular Rods"); exp_choice.add("Exp 2: Flying Clocks"); exp_choice.add("Exp 3: A Light Clock"); exp_choice.add("Exp 4: Moving Michelson-Morley"); exp_choice.add("Exp 5: Barn & Ladder Paradox"); exp_choice.add("Exp 6: Car Headlight Experiment"); exp_choice.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ie) { Choice ctemp = (Choice)ie.getSource(); int choiceNum = ctemp.getSelectedIndex(); reset(); initMover(choiceNum); } } ); c.gridy = 13; LeftPanel.add(exp_choice,c); ButtonPanelInit(); c.insets = new Insets(0,0,0,0); RightPanel.add(ButtonPanel,c); setFont(temp); } public void initMover(int c) { switch(c) { case 0: mover = new PerpRods(topValue,this); break; case 1: mover = new FlyingClocks(topValue,this); break; case 2: mover = new LightClock(topValue,this); break; case 3: mover = new MM(topValue,this); break; case 4: mover = new BarnLadder(topValue,this); break; case 5: mover = new CarLight(topValue,this); mover.setCar(getImage(getDocumentBase(),"images/carsmall.gif")); break; default: mover = new EinsteinObject(topValue,this); } repaint(); time.repaint(); space.repaint(); } public void ButtonPanelInit() { ButtonPanel = new Panel(new FlowLayout(FlowLayout.CENTER,5,5)); ButtonPanel.setBackground(Color.white); START = new Button("Start"); START.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!started) { started = true; START.setLabel("Stop"); top_scr.setEnabled(false); } else { started = false; stepped = false; START.setLabel("Start"); } time.repaint(); space.repaint(); } } ); ButtonPanel.add(START); STEP = new Button("Step"); STEP.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (started) { START.setLabel("Start"); started = false; } else { top_scr.setEnabled(false); } stepped = true; time.repaint(); space.repaint(); } } ); ButtonPanel.add(STEP); RESET = new Button("Reset"); RESET.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { reset(); } } ); ButtonPanel.add(RESET); b = new Button("Java TA"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Button buttTarget = (Button)e.getSource(); buttTarget.setEnabled(false); buttTarget.setVisible(false); pushed = true; } } ); ButtonPanel.add(b); } public void reset() { started = false; top_scr.setEnabled(true); START.setLabel("Start"); space.init(); reality = new Reality(0); initMover(exp_choice.getSelectedIndex()); topValue = 0; top_scr.setValue(0); top_lab.setText("Object v= .00 c"); initMover(exp_choice.getSelectedIndex()); time.offsetX = 0; time.offsetY = 0; repaint(); time.repaint(); space.repaint(); } public void init() { topValue = 0; mover = new PerpRods(topValue,this); image = createImage(300,300); offscr= image.getGraphics(); image2 = createImage(300,300); offscr2= image2.getGraphics(); setLayout(new GridLayout(1,2,0,0)); PanelInit(); add(LeftPanel); add(RightPanel); reality = new Reality(0); } public Reality getReality() { return reality; } public void start() { animation = new Thread(this); if (animation != null) { animation.start(); } } public void run() { while (true) { repaint(); //Rectangle r = space.getBounds(); if (started || stepped) { stepped = false; if (mover != null) { space.s.move(frame, mover.getV()); mover.Increment(); } reality.Increment(); space.repaint(); time.repaint(); } if (pushed) { EINtut = new EINtutor(this); b.setLabel("TA on"); pushed = false; b.setEnabled(true); b.setVisible(true); } try { Thread.sleep(REFRESH_RATE); } catch (Exception exc) {}; } } public void stop() { if (animation != null) { animation.stop(); animation= null; } } }