KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Rotation


1 import java.awt.*;
2 import java.awt.event.*;
3 import JSci.physics.*;
4
5 /**
6 * Angular momentum simulator.
7 * @author Mark Hale
8 * @version 1.0
9 */

10 public final class Rotation extends Frame implements Runnable JavaDoc {
11         private RigidBody2D body=new RigidBody2D();
12         private Display display=new Display();
13
14         public static void main(String JavaDoc arg[]) {
15                 Frame app=new Rotation();
16                 app.setSize(250,250);
17                 app.setVisible(true);
18         }
19         public Rotation() {
20                 super("Rotation!");
21                 add(display,"Center");
22                 addWindowListener(new WindowAdapter() {
23                         public void windowClosing(WindowEvent evt) {
24                                 dispose();
25                                 System.exit(0);
26                         }
27                 });
28                 body.setMass(5.0);
29                 body.setMomentOfInertia(5.0);
30                 Thread JavaDoc thr=new Thread JavaDoc(this);
31                 thr.start();
32         }
33         public void run() {
34                 double width,height;
35                 double x,y;
36                 while(true) {
37                         body.move(0.01);
38                         width=getSize().width;
39                         height=getSize().height;
40                         x=body.getXPosition();
41                         y=body.getYPosition();
42                         if(x>width/2.0)
43                                 body.setXPosition(x-width);
44                         else if(x<-width/2.0)
45                                 body.setXPosition(x+width);
46                         if(y>height/2.0)
47                                 body.setYPosition(y-height);
48                         else if(y<-height/2.0)
49                                 body.setYPosition(y+height);
50                         display.repaint();
51                         try {
52                                 Thread.sleep(100);
53                         } catch(InterruptedException JavaDoc e) {}
54                 }
55         }
56         private final class Display extends Canvas {
57                 private Point start,end;
58                 private boolean firstDrag=false;
59                 public Display() {
60                         addMouseListener(new MouseAdapter() {
61                                 public void mousePressed(MouseEvent e) {
62                                         firstDrag=true;
63                                         start=end=null;
64                                 }
65                                 public void mouseReleased(MouseEvent e) {
66                                         if(start!=null && end!=null) {
67                                                 final double Fx=end.x-start.x;
68                                                 final double Fy=-(end.y-start.y);
69                                                 final int cx=getSize().width/2;
70                                                 final int cy=getSize().height/2;
71                                                 final double x=(end.x-cx)-body.getXPosition();
72                                                 final double y=-(end.y-cy)-body.getYPosition();
73                                                 for(int i=0;i<4;i++) {
74                                                         body.applyForce(Fx,Fy,x,y,0.05);
75                                                         body.move(0.05);
76                                                         repaint();
77                                                 }
78                                                 System.out.println("Force ("+Fx+','+Fy+") applied at ("+x+','+y+')');
79                                         }
80                                 }
81                         });
82                         addMouseMotionListener(new MouseMotionAdapter() {
83                                 public void mouseDragged(MouseEvent e) {
84                                         if(firstDrag) {
85                                                 start=e.getPoint();
86                                                 firstDrag=false;
87                                         } else if(start!=null)
88                                                 end=e.getPoint();
89                                 }
90                         });
91                 }
92                 public void paint(Graphics g) {
93                         final Graphics2D g2=(Graphics2D)g;
94                         final int cx=getSize().width/2;
95                         final int cy=getSize().height/2;
96                         final double x=body.getXPosition();
97                         final double y=body.getYPosition();
98                         g2.translate(x,-y);
99                         g2.rotate(-body.getAngle(),cx,cy);
100                         g2.setColor(Color.red);
101                         g2.fillRect(cx-50,cy-10,100,20);
102                 }
103                 public Dimension getPreferredSize() {
104                         return new Dimension(200,200);
105                 }
106         }
107 }
108
109
Popular Tags