Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Code by Grey Hugentobler, March 31st, 2011
- package GravityApplet;
- import java.awt.*;
- import java.util.*;
- import javax.swing.*;
- public class GravityApplet extends JApplet implements Runnable
- {
- //G is the gravitational constant for the depicted universe
- public int g = 1;
- //T is the time since the simulation started
- public float t = 0;
- //dT is the increment of time we will advance the simulation by each step
- public float dt = .02f;
- //number of particles
- public final int n = 80;
- //total mass of the system
- public float totalMass = 0;
- //inverse total mass of the system
- public float inverseTotalMass = 0;
- //center of mass of the system (mechanism to check that the net momentum is in fact 0)
- public Vector center = new Vector(0, 0);
- //Mass stores the masses of the particles
- public float[] mass = new float[n];
- //InverseMass stores the inverse mass of each particle
- public float[] inverseMass = new float[n];
- //Position is a collection of the particles position Vectors
- public Vector[] position = new Vector[n];
- //Velocity is a collection of the particles velocity Vectors
- public Vector[] velocity = new Vector[n];
- //Acceleration is derived from NetForce using Mass.
- public Vector[] acceleration = new Vector[n];
- //NetForce is where we store the force of all the particles on each individual particle
- public Vector[] netForce = new Vector[n];
- //Force is where we store the force of each particle on each the other particles
- public Vector[][] force = new Vector[n][n];
- //bufferGraphics is the Graphics buffer
- public Graphics bufferGraphics;
- //offscreen is the image being put together offscreen
- public Image offscreen;
- //dim is the current dimensions of the applet
- public Dimension dim;
- //Show center or not
- public boolean showCenter = true;
- //Show time or not
- public boolean showTime = true;
- //Random number Generator
- public Random generator = new Random();
- // initiate Applet
- public void init()
- {
- //figure out how large the window is
- dim = getSize();
- //standard deviation of the velocity
- float sigmav = 1;
- //generate our distribution of particles
- for(int i = 0; i < n; i++)
- {
- mass[i] = 100*(2-generator.nextFloat());
- inverseMass[i] = 1/mass[i];
- position[i] = new Vector(Math.round(dim.width*(.5*generator.nextDouble()+ .25)), Math.round(dim.height*(.5*generator.nextDouble()+ .25)));
- velocity[i] = new Vector(Math.round(sigmav*generator.nextGaussian()), Math.round(sigmav*generator.nextGaussian()));
- acceleration[i] = new Vector(0, 0);
- }
- //correct for net momentum so that the system doesn't go off screen
- //create Vector representing net momentum
- Vector netMomentum = new Vector(0, 0);
- //find total momentum and total mass
- for(int i = 0; i < n; i++)
- {
- netMomentum = netMomentum.add(velocity[i].multiply(mass[i]));
- totalMass += mass[i];
- }
- inverseTotalMass = 1/totalMass;
- //create vector representing net velocity
- Vector netVelocity = netMomentum.multiply(inverseTotalMass);
- //modify all the velocities to cancel the net momentum
- for(int i = 0; i < n; i++)
- {
- velocity[i]=velocity[i].subtract(netVelocity);
- }
- //the background is black, like space
- setBackground(Color.black);
- //start a new thread to run the simulation
- Thread newThread;
- newThread = new Thread (this);
- newThread.start();
- }
- public void paint(Graphics onscreen)
- {
- //change dim to the current size of the Applet
- dim = getSize();
- //change the offscreen image to the dimensions of the Applet
- offscreen = createImage(dim.width,dim.height);
- bufferGraphics = offscreen.getGraphics();
- // Draw all the little yellow dots
- bufferGraphics.setColor(Color.yellow);
- for(int i = 0; i < n; i++)
- {
- bufferGraphics.fillRect((int) (position[i].x() - 1), (int) (position[i].y() - 1), 2, 2);
- }
- //draw the time to the screen in a green serif font
- if(showTime)
- {
- bufferGraphics.setFont(new Font("serif", Font.BOLD, 12));
- bufferGraphics.setColor(Color.green);
- bufferGraphics.drawString(((int) t + ""), 0, 12);
- }
- //draw the center of mass
- if(showCenter)
- {
- bufferGraphics.setColor(Color.white);
- bufferGraphics.fillRect((int) (center.x() - 1), (int) (center.y() - 1), 2, 2);
- }
- //draw it to the applet
- onscreen.drawImage(offscreen,0,0,this);
- }
- public void run()
- {
- while(true)
- {
- //sleep the thread for twenty milliseconds
- try
- {
- Thread.sleep((int) (1000*dt));
- }
- catch(Exception e) {}
- //create some storage variables to make the calculations easier
- Vector radius;
- float divisor;
- //find all the forces of each particle on each other particle
- for(int i = 0; i < n; i++)
- {
- for(int j = n - 1; j >= i; j--)
- {
- if(i == j)
- {
- force[i][j] = new Vector(0, 0);
- }
- else
- {
- radius = (position[i].subtract(position[j]));
- divisor = radius.dot(radius);
- if(divisor == 0)
- {
- force[i][j] = new Vector(0, 0);
- }
- else
- {
- force[i][j] = radius.multiply(-g*mass[i]*mass[j]/divisor);
- }
- force[j][i] = force[i][j].negative();
- }
- }
- }
- //clear the net Force
- for(int i = 0; i < n; i++)
- {
- netForce[i] = new Vector(0, 0);
- }
- //find sum up the forces to find the total force
- for(int i = 0; i < n; i++)
- {
- for(int j = 0; j < n; j++)
- {
- netForce[i] = netForce[i].add(force[i][j]);
- }
- }
- //convert force to acceleration
- for(int i = 0; i < n; i++)
- {
- acceleration[i] = netForce[i].multiply(inverseMass[i]);
- }
- //integrate the position and velocity
- for(int i = 0; i < n; i++)
- {
- position[i]=position[i].add(velocity[i].multiply(dt));
- velocity[i]=velocity[i].add(acceleration[i].multiply(dt));
- }
- //increment the time variable
- if(showTime)
- {
- t+=dt;
- }
- if(showCenter)
- {
- //intermediate between the properties of the particles and the center of mass
- Vector massPosition = new Vector(0, 0);
- //add up the masses multiplied by the positions, the masses have already been added up
- for(int i = 0; i < n; i++)
- {
- massPosition = massPosition.add(position[i].multiply(mass[i]));
- }
- //calculate the center of mass
- center = massPosition.multiply(inverseTotalMass);
- }
- repaint();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement