1 import java.awt.*; 2 import java.awt.event.*; 3 import JSci.physics.*; 4 5 10 public final class Collision extends Frame { 11 private ClassicalParticle2D A=new ClassicalParticle2D(); 12 private ClassicalParticle2D B=new ClassicalParticle2D(); 13 private TextField massA=new TextField("2.0"); 14 private TextField massB=new TextField("2.0"); 15 private TextField velXA=new TextField("3.0"); 16 private TextField velXB=new TextField("3.0"); 17 private TextField velYA=new TextField("3.0"); 18 private TextField velYB=new TextField("-1.0"); 19 private VectorDisplay display=new VectorDisplay(4); 20 private Label energyBefore=new Label(); 21 private Label energyAfter=new Label(); 22 private Label momentumXBefore=new Label(); 23 private Label momentumXAfter=new Label(); 24 private Label momentumYBefore=new Label(); 25 private Label momentumYAfter=new Label(); 26 public static void main(String arg[]) { 27 Frame app=new Collision(); 28 app.setSize(500,500); 29 app.setVisible(true); 30 } 31 public Collision() { 32 super("Collision!"); 33 final Panel controls=new Panel(); 34 controls.setLayout(new GridLayout(4,3)); 35 final Button button=new Button("Collide"); 36 button.addActionListener(new ActionListener() { 37 public void actionPerformed(ActionEvent evt) { 38 collide(); 39 } 40 }); 41 controls.add(button); 42 final Label labelA=new Label("Particle A"); 43 labelA.setForeground(Color.red); 44 controls.add(labelA); 45 final Label labelB=new Label("Particle B"); 46 labelB.setForeground(Color.blue); 47 controls.add(labelB); 48 controls.add(new Label("Mass:")); 49 controls.add(massA); 50 controls.add(massB); 51 controls.add(new Label("X Velocity:")); 52 controls.add(velXA); 53 controls.add(velXB); 54 controls.add(new Label("Y Velocity:")); 55 controls.add(velYA); 56 controls.add(velYB); 57 final Panel info=new Panel(); 58 info.setLayout(new GridLayout(4,3)); 59 info.add(new Panel()); 60 info.add(new Label("Before")); 61 info.add(new Label("After")); 62 info.add(new Label("Energy:")); 63 info.add(energyBefore); 64 info.add(energyAfter); 65 info.add(new Label("X Momentum:")); 66 info.add(momentumXBefore); 67 info.add(momentumXAfter); 68 info.add(new Label("Y Momentum:")); 69 info.add(momentumYBefore); 70 info.add(momentumYAfter); 71 add(controls,"North"); 72 add(display,"Center"); 73 add(info,"South"); 74 addWindowListener(new WindowAdapter() { 75 public void windowClosing(WindowEvent evt) { 76 dispose(); 77 System.exit(0); 78 } 79 }); 80 collide(); 81 } 82 private void collide() { 83 A.setMass(parseDouble(massA.getText())); 84 A.setVelocity(parseDouble(velXA.getText()),parseDouble(velYA.getText())); 85 B.setMass(parseDouble(massB.getText())); 86 B.setVelocity(parseDouble(velXB.getText()),parseDouble(velYB.getText())); 87 display.setVector(0,-A.getXVelocity(),A.getYVelocity()); 88 display.setVector(1,-B.getXVelocity(),B.getYVelocity()); 89 energyBefore.setText(Double.toString(A.energy()+B.energy())); 90 momentumXBefore.setText(Double.toString(A.getXMomentum()+B.getXMomentum())); 91 momentumYBefore.setText(Double.toString(A.getYMomentum()+B.getYMomentum())); 92 A.collide(B, 0.0); 93 display.setVector(2,A.getXVelocity(),-A.getYVelocity()); 94 display.setVector(3,B.getXVelocity(),-B.getYVelocity()); 95 energyAfter.setText(Double.toString(A.energy()+B.energy())); 96 momentumXAfter.setText(Double.toString(A.getXMomentum()+B.getXMomentum())); 97 momentumYAfter.setText(Double.toString(A.getYMomentum()+B.getYMomentum())); 98 } 99 private static double parseDouble(String s) { 100 return Double.valueOf(s).doubleValue(); 101 } 102 private final class VectorDisplay extends Canvas { 103 private float vecX[],vecY[]; 104 private Color color[]; 105 public VectorDisplay(int n) { 106 vecX=new float[n]; 107 vecY=new float[n]; 108 color=new Color[n]; 109 color[0]=Color.red; 110 color[1]=Color.blue; 111 color[2]=Color.red; 112 color[3]=Color.blue; 113 } 114 public void setVector(int i,double dx,double dy) { 115 final double norm=Math.sqrt(dx*dx+dy*dy); 116 vecX[i]=(float)(dx/norm); 117 vecY[i]=(float)(dy/norm); 118 repaint(); 119 } 120 public void paint(Graphics g) { 121 final int ox=getSize().width/2; 122 final int oy=getSize().height/2; 123 int endX,endY; 124 for(int i=0;i<vecX.length;i++) { 125 endX=ox+(int)((ox-20)*vecX[i]); 126 endY=oy+(int)((oy-20)*vecY[i]); 127 g.setColor(color[i]); 128 g.drawLine(ox,oy,endX,endY); 129 } 130 } 131 public Dimension getPreferredSize() { 132 return new Dimension(100,100); 133 } 134 } 135 } 136 137
| Popular Tags
|