001: import java.awt.*;
002: import java.awt.event.*;
003: import JSci.physics.*;
004:
005: /**
006: * Angular momentum simulator.
007: * @author Mark Hale
008: * @version 1.0
009: */
010: public final class Rotation extends Frame implements Runnable {
011: private RigidBody2D body = new RigidBody2D();
012: private Display display = new Display();
013:
014: public static void main(String arg[]) {
015: Frame app = new Rotation();
016: app.setSize(250, 250);
017: app.setVisible(true);
018: }
019:
020: public Rotation() {
021: super ("Rotation!");
022: add(display, "Center");
023: addWindowListener(new WindowAdapter() {
024: public void windowClosing(WindowEvent evt) {
025: dispose();
026: System.exit(0);
027: }
028: });
029: body.setMass(5.0);
030: body.setMomentOfInertia(5.0);
031: Thread thr = new Thread(this );
032: thr.start();
033: }
034:
035: public void run() {
036: double width, height;
037: double x, y;
038: while (true) {
039: body.move(0.01);
040: width = getSize().width;
041: height = getSize().height;
042: x = body.getXPosition();
043: y = body.getYPosition();
044: if (x > width / 2.0)
045: body.setXPosition(x - width);
046: else if (x < -width / 2.0)
047: body.setXPosition(x + width);
048: if (y > height / 2.0)
049: body.setYPosition(y - height);
050: else if (y < -height / 2.0)
051: body.setYPosition(y + height);
052: display.repaint();
053: try {
054: Thread.sleep(100);
055: } catch (InterruptedException e) {
056: }
057: }
058: }
059:
060: private final class Display extends Canvas {
061: private Point start, end;
062: private boolean firstDrag = false;
063:
064: public Display() {
065: addMouseListener(new MouseAdapter() {
066: public void mousePressed(MouseEvent e) {
067: firstDrag = true;
068: start = end = null;
069: }
070:
071: public void mouseReleased(MouseEvent e) {
072: if (start != null && end != null) {
073: final double Fx = end.x - start.x;
074: final double Fy = -(end.y - start.y);
075: final int cx = getSize().width / 2;
076: final int cy = getSize().height / 2;
077: final double x = (end.x - cx)
078: - body.getXPosition();
079: final double y = -(end.y - cy)
080: - body.getYPosition();
081: for (int i = 0; i < 4; i++) {
082: body.applyForce(Fx, Fy, x, y, 0.05);
083: body.move(0.05);
084: repaint();
085: }
086: System.out.println("Force (" + Fx + ',' + Fy
087: + ") applied at (" + x + ',' + y + ')');
088: }
089: }
090: });
091: addMouseMotionListener(new MouseMotionAdapter() {
092: public void mouseDragged(MouseEvent e) {
093: if (firstDrag) {
094: start = e.getPoint();
095: firstDrag = false;
096: } else if (start != null)
097: end = e.getPoint();
098: }
099: });
100: }
101:
102: public void paint(Graphics g) {
103: final Graphics2D g2 = (Graphics2D) g;
104: final int cx = getSize().width / 2;
105: final int cy = getSize().height / 2;
106: final double x = body.getXPosition();
107: final double y = body.getYPosition();
108: g2.translate(x, -y);
109: g2.rotate(-body.getAngle(), cx, cy);
110: g2.setColor(Color.red);
111: g2.fillRect(cx - 50, cy - 10, 100, 20);
112: }
113:
114: public Dimension getPreferredSize() {
115: return new Dimension(200, 200);
116: }
117: }
118: }
|