001: import java.awt.*;
002: import java.awt.event.*;
003: import JSci.physics.*;
004:
005: /**
006: * Two-body collision simulator.
007: * @author Mark Hale
008: * @version 1.0
009: */
010: public final class Collision extends Frame {
011: private ClassicalParticle2D A = new ClassicalParticle2D();
012: private ClassicalParticle2D B = new ClassicalParticle2D();
013: private TextField massA = new TextField("2.0");
014: private TextField massB = new TextField("2.0");
015: private TextField velXA = new TextField("3.0");
016: private TextField velXB = new TextField("3.0");
017: private TextField velYA = new TextField("3.0");
018: private TextField velYB = new TextField("-1.0");
019: private VectorDisplay display = new VectorDisplay(4);
020: private Label energyBefore = new Label();
021: private Label energyAfter = new Label();
022: private Label momentumXBefore = new Label();
023: private Label momentumXAfter = new Label();
024: private Label momentumYBefore = new Label();
025: private Label momentumYAfter = new Label();
026:
027: public static void main(String arg[]) {
028: Frame app = new Collision();
029: app.setSize(500, 500);
030: app.setVisible(true);
031: }
032:
033: public Collision() {
034: super ("Collision!");
035: final Panel controls = new Panel();
036: controls.setLayout(new GridLayout(4, 3));
037: final Button button = new Button("Collide");
038: button.addActionListener(new ActionListener() {
039: public void actionPerformed(ActionEvent evt) {
040: collide();
041: }
042: });
043: controls.add(button);
044: final Label labelA = new Label("Particle A");
045: labelA.setForeground(Color.red);
046: controls.add(labelA);
047: final Label labelB = new Label("Particle B");
048: labelB.setForeground(Color.blue);
049: controls.add(labelB);
050: controls.add(new Label("Mass:"));
051: controls.add(massA);
052: controls.add(massB);
053: controls.add(new Label("X Velocity:"));
054: controls.add(velXA);
055: controls.add(velXB);
056: controls.add(new Label("Y Velocity:"));
057: controls.add(velYA);
058: controls.add(velYB);
059: final Panel info = new Panel();
060: info.setLayout(new GridLayout(4, 3));
061: info.add(new Panel());
062: info.add(new Label("Before"));
063: info.add(new Label("After"));
064: info.add(new Label("Energy:"));
065: info.add(energyBefore);
066: info.add(energyAfter);
067: info.add(new Label("X Momentum:"));
068: info.add(momentumXBefore);
069: info.add(momentumXAfter);
070: info.add(new Label("Y Momentum:"));
071: info.add(momentumYBefore);
072: info.add(momentumYAfter);
073: add(controls, "North");
074: add(display, "Center");
075: add(info, "South");
076: addWindowListener(new WindowAdapter() {
077: public void windowClosing(WindowEvent evt) {
078: dispose();
079: System.exit(0);
080: }
081: });
082: collide();
083: }
084:
085: private void collide() {
086: A.setMass(parseDouble(massA.getText()));
087: A.setVelocity(parseDouble(velXA.getText()), parseDouble(velYA
088: .getText()));
089: B.setMass(parseDouble(massB.getText()));
090: B.setVelocity(parseDouble(velXB.getText()), parseDouble(velYB
091: .getText()));
092: display.setVector(0, -A.getXVelocity(), A.getYVelocity());
093: display.setVector(1, -B.getXVelocity(), B.getYVelocity());
094: energyBefore.setText(Double.toString(A.energy() + B.energy()));
095: momentumXBefore.setText(Double.toString(A.getXMomentum()
096: + B.getXMomentum()));
097: momentumYBefore.setText(Double.toString(A.getYMomentum()
098: + B.getYMomentum()));
099: A.collide(B, 0.0);
100: display.setVector(2, A.getXVelocity(), -A.getYVelocity());
101: display.setVector(3, B.getXVelocity(), -B.getYVelocity());
102: energyAfter.setText(Double.toString(A.energy() + B.energy()));
103: momentumXAfter.setText(Double.toString(A.getXMomentum()
104: + B.getXMomentum()));
105: momentumYAfter.setText(Double.toString(A.getYMomentum()
106: + B.getYMomentum()));
107: }
108:
109: private static double parseDouble(String s) {
110: return Double.valueOf(s).doubleValue();
111: }
112:
113: private final class VectorDisplay extends Canvas {
114: private float vecX[], vecY[];
115: private Color color[];
116:
117: public VectorDisplay(int n) {
118: vecX = new float[n];
119: vecY = new float[n];
120: color = new Color[n];
121: color[0] = Color.red;
122: color[1] = Color.blue;
123: color[2] = Color.red;
124: color[3] = Color.blue;
125: }
126:
127: public void setVector(int i, double dx, double dy) {
128: final double norm = Math.sqrt(dx * dx + dy * dy);
129: vecX[i] = (float) (dx / norm);
130: vecY[i] = (float) (dy / norm);
131: repaint();
132: }
133:
134: public void paint(Graphics g) {
135: final int ox = getSize().width / 2;
136: final int oy = getSize().height / 2;
137: int endX, endY;
138: for (int i = 0; i < vecX.length; i++) {
139: endX = ox + (int) ((ox - 20) * vecX[i]);
140: endY = oy + (int) ((oy - 20) * vecY[i]);
141: g.setColor(color[i]);
142: g.drawLine(ox, oy, endX, endY);
143: }
144: }
145:
146: public Dimension getPreferredSize() {
147: return new Dimension(100, 100);
148: }
149: }
150: }
|