Source Code Cross Referenced for Collision.java in  » Science » JSci » examples » Mechanics » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » JSci » examples.Mechanics 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.