Advertisement
Grey_Hugentobler

Gravity Applet 1.0

Apr 2nd, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. //Code by Grey Hugentobler, March 31st, 2011
  2.  
  3. package GravityApplet;
  4.  
  5. import java.awt.*;
  6. import java.util.*;
  7. import javax.swing.*;
  8.  
  9. public class GravityApplet extends JApplet implements Runnable
  10. {
  11. //G is the gravitational constant for the depicted universe
  12. public final int g = 1;
  13. //t is the time since the simulation started
  14. public float t = 0;
  15. //dt is the increment of time we will advance the simulation by each step
  16. public final float dt = .01f;
  17. //number of particles
  18. public final int n = 200;
  19. //total mass of the system
  20. public float totalMass = 0;
  21. //inverse total mass of the system
  22. public float inverseTotalMass = 0;
  23. //center of mass of the system (mechanism to check that the net momentum is in fact 0)
  24. public Vector center = new Vector(0, 0);
  25.  
  26. //Mass stores the masses of the particles
  27. public float[] mass = new float[n];
  28. //InverseMass stores the inverse mass of each particle
  29. public float[] inverseMass = new float[n];
  30. //Position is a collection of the particles position Vectors
  31. public Vector[] position = new Vector[n];
  32. //Velocity is a collection of the particles velocity Vectors
  33. public Vector[] velocity = new Vector[n];
  34. //Acceleration is derived from NetForce using Mass.
  35. public Vector[] acceleration = new Vector[n];
  36. //NetForce is where we store the force of all the particles on each individual particle
  37. public Vector[] netForce = new Vector[n];
  38. //Force is where we store the force of each particle on each the other particles
  39. public Vector[][] force = new Vector[n][n];
  40. //Color is where the random colors are stored
  41. public Color[] color = new Color[n];
  42.  
  43. //bufferGraphics is the Graphics buffer
  44. public Graphics bufferGraphics;
  45. //offscreen is the image being put together offscreen
  46. public Image offscreen;
  47. //dim is the current dimensions of the applet
  48. public Dimension dim;
  49.  
  50. //Show center or not
  51. public boolean showCenter = true;
  52. //Show time or not
  53. public boolean showTime = true;
  54.  
  55. //Random number Generator
  56. public Random generator = new Random();
  57.  
  58. // initiate Applet
  59. public void init()
  60. {
  61. //figure out how large the window is
  62. dim = getSize();
  63.  
  64. //standard deviation of the velocity
  65. double sigmar = 0.5*Math.min(dim.width, dim.height);
  66. double sigmav = 100;
  67.  
  68. //generate our distribution of particles
  69. for(int i = 0; i < n; i++)
  70. {
  71. mass[i] = 100*(2-generator.nextFloat());
  72. inverseMass[i] = 1/mass[i];
  73. double random1=sigmar*generator.nextGaussian();
  74. double random2=2*Math.PI*generator.nextDouble();
  75. double random3=sigmav*generator.nextGaussian();
  76. double random4=2*Math.PI*generator.nextDouble();
  77. position[i] = new Vector(Math.round(random1*Math.sin(random2)+0.5*dim.width),Math.round(random1*Math.cos(random2)+0.5*dim.height));
  78. velocity[i] = new Vector(Math.round(random3*Math.sin(random4)),Math.round(random3*Math.cos(random4)));
  79. acceleration[i] = new Vector(0, 0);
  80. float randomRed = Math.round(generator.nextDouble());
  81. float randomGreen = Math.round(generator.nextDouble());
  82. float randomBlue = Math.round(generator.nextDouble());
  83. color[i] = new Color(randomRed, randomGreen, randomBlue);
  84. }
  85.  
  86. //correct for net momentum so that the system doesn't go off screen
  87. //create Vector representing net momentum
  88. Vector netMomentum = new Vector(0, 0);
  89.  
  90. //find total momentum and total mass
  91. for(int i = 0; i < n; i++)
  92. {
  93. netMomentum = netMomentum.add(velocity[i].multiply(mass[i]));
  94. totalMass += mass[i];
  95. }
  96.  
  97. inverseTotalMass = 1/totalMass;
  98.  
  99. //create vector representing net velocity
  100. Vector netVelocity = netMomentum.multiply(inverseTotalMass);
  101.  
  102. //modify all the velocities to cancel the net momentum
  103. for(int i = 0; i < n; i++)
  104. {
  105. velocity[i]=velocity[i].subtract(netVelocity);
  106. }
  107.  
  108. //the background is black, like space
  109. setBackground(Color.black);
  110.  
  111. //start a new thread to run the simulation
  112. Thread newThread;
  113. newThread = new Thread (this);
  114. newThread.start();
  115. }
  116.  
  117. public void paint(Graphics onscreenGraphics)
  118. {
  119. //change dim to the current size of the Applet
  120. dim = getSize();
  121.  
  122. //change the offscreen image to the dimensions of the Applet
  123. offscreen = createImage(dim.width,dim.height);
  124. bufferGraphics = offscreen.getGraphics();
  125.  
  126. // Draw all the little yellow dots
  127. for(int i = 0; i < n; i++)
  128. {
  129. bufferGraphics.setColor(color[i]);
  130. bufferGraphics.fillRect((int) (position[i].x() - 1), (int) (position[i].y() - 1), 2, 2);
  131. }
  132.  
  133. //draw the time to the screen in a green serif font
  134. if(showTime)
  135. {
  136. bufferGraphics.setFont(new Font("serif", Font.BOLD, 12));
  137. bufferGraphics.setColor(Color.green);
  138. bufferGraphics.drawString(((int) t + ""), 0, 12);
  139. }
  140.  
  141. //draw the center of mass
  142. if(showCenter)
  143. {
  144. bufferGraphics.setColor(Color.white);
  145. bufferGraphics.fillRect((int) (center.x() - 1), (int) (center.y() - 1), 2, 2);
  146. }
  147.  
  148. //draw it to the applet
  149. onscreenGraphics.drawImage(offscreen,0,0,this);
  150. }
  151.  
  152. public void run()
  153. {
  154. while(true)
  155. {
  156. //sleep the thread for twenty milliseconds
  157. try
  158. {
  159. Thread.sleep((int) (1000*dt));
  160. }
  161. catch(Exception e) {}
  162.  
  163. //create some storage variables to make the calculations easier
  164. Vector radius;
  165. float divisor;
  166.  
  167. //find all the forces of each particle on each other particle
  168. for(int i = 0; i < n; i++)
  169. {
  170. for(int j = n - 1; j >= i; j--)
  171. {
  172. if(i == j)
  173. {
  174. force[i][j] = new Vector(0, 0);
  175. }
  176. else
  177. {
  178. radius = (position[i].subtract(position[j]));
  179. divisor = radius.dot(radius);
  180. if(divisor <= .01)
  181. {
  182. force[i][j] = new Vector(0, 0);
  183. }
  184. else
  185. {
  186. force[i][j] = radius.multiply(-g*mass[i]*mass[j]/divisor);
  187. }
  188. force[j][i] = force[i][j].negative();
  189. }
  190. }
  191. }
  192.  
  193. //clear the net Force
  194. for(int i = 0; i < n; i++)
  195. {
  196. netForce[i] = new Vector(0, 0);
  197. }
  198.  
  199. //find sum up the forces to find the total force
  200. for(int i = 0; i < n; i++)
  201. {
  202. for(int j = 0; j < n; j++)
  203. {
  204. netForce[i] = netForce[i].add(force[i][j]);
  205. }
  206. }
  207.  
  208. //convert force to acceleration
  209. for(int i = 0; i < n; i++)
  210. {
  211. acceleration[i] = netForce[i].multiply(inverseMass[i]);
  212. }
  213.  
  214. //integrate the position and velocity
  215. for(int i = 0; i < n; i++)
  216. {
  217. position[i]=position[i].add(velocity[i].multiply(dt));
  218. velocity[i]=velocity[i].add(acceleration[i].multiply(dt));
  219. }
  220.  
  221. //increment the time variable
  222. if(showTime)
  223. {
  224. t+=dt;
  225. }
  226.  
  227. if(showCenter)
  228. {
  229. //intermediate between the properties of the particles and the center of mass
  230. Vector massPosition = new Vector(0, 0);
  231.  
  232. //add up the masses multiplied by the positions, the masses have already been added up
  233. for(int i = 0; i < n; i++)
  234. {
  235. massPosition = massPosition.add(position[i].multiply(mass[i]));
  236. }
  237.  
  238. //calculate the center of mass
  239. center = massPosition.multiply(inverseTotalMass);
  240. }
  241.  
  242. repaint();
  243. }
  244. }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement