The WorldOfParticles
is container for particles governed by the common physical laws.
More...
Classes | |
class | Barrier |
Infinite Potential Barrier (2D-Box) class. More... | |
interface | RenderingContext |
Provides rendering context interface for the WorldOfParticles . More... | |
Public Member Functions | |
WorldOfParticles (RenderingContext context) | |
Creates and starts object's (world's) main thread. | |
boolean | isPaused () |
Returns indicator whether forces between particles are calculated or not. | |
void | togglePaused () |
Turns on/off calculations of forces between particles. | |
int | getParticleCount () |
Gets number of particles in our world. | |
void | incTimeScale (double factor) |
Increments/decrements time scale. | |
double | getTimeScale () |
Gets world's time to 'real' world's time ratio. | |
GraphicsConfiguration | getGraphicsConfiguration () |
Gets the GraphicsConfiguration associated with parent Component. | |
Particle | addNewParticle (double M, double Q, double X, double Y, double R, double T) |
Adds one extra particle to the world. | |
void | removeParticle (Particle p) |
Removes the particle p from the world. | |
void | dump () |
Dumps internal state for all particles to System.out (for debugging purposes). | |
void | resetVelocities () |
Resets velocities for all particles to 0. | |
void | makeCentripetalVelocities () |
Makes velocities for all particles to be centripetal to current particles' acceleration. | |
void | paint (Graphics g, boolean annotate) |
Renders the world on Graphics . | |
Barrier | getBarrier () |
Gets boundaries of infinite potential barrier keeping particles together. | |
void | calculateInteractions () |
Calculates forces acting on particles. | |
void | run () |
Main thread that calculates interactions (forces) between all particles in the world. | |
Private Attributes | |
ArrayList< Particle > | particles = new ArrayList<Particle> () |
Collection of Particles that belongs to this world. | |
volatile double | timeScale = 0.5 |
World's time to 'real' world's time ratio. | |
volatile boolean | paused = false |
Indicates whether forces between particles are calculated or not. | |
volatile boolean | running = true |
Indicates whether to keep world's thread running or not. | |
Thread | mainThread |
Each object (i.e. | |
RenderingContext | context |
Particular instance with rendering context interface. |
The WorldOfParticles
is container for particles governed by the common physical laws.
Forces acting on particles belonging to this world are calculated in the main thread of the instance. The instance of the WorldOfParticles
is rendered inside the Component
, which acts as a parent.
Definition at line 15 of file WorldOfParticles.java.
WorldOfParticles.WorldOfParticles | ( | RenderingContext | context ) |
Creates and starts object's (world's) main thread.
context | instance of the rendering context |
Definition at line 87 of file WorldOfParticles.java.
References context, and mainThread.
{ this.context = context; mainThread = new Thread( this ); mainThread.start (); }
Particle WorldOfParticles.addNewParticle | ( | double | M, |
double | Q, | ||
double | X, | ||
double | Y, | ||
double | R, | ||
double | T | ||
) |
Adds one extra particle to the world.
M | mass of the Particle |
Q | electrical charge of the Particle |
X | initial position, x-component |
Y | initial position, y-component |
R | radius range |
T | life-time in seconds |
Definition at line 165 of file WorldOfParticles.java.
References context, WorldOfParticles.RenderingContext.onParticleCountChanged(), and particles.
Referenced by JPWorld.actionPerformed(), and JPWorld.createPairOfChargedParticles().
void WorldOfParticles.calculateInteractions | ( | ) |
Calculates forces acting on particles.
Definition at line 271 of file WorldOfParticles.java.
References particles.
Referenced by run().
{ synchronized( this ) { for ( Particle p : particles ) { /* The sum of all forces acting on particle p */ p.resetForce (); /* Get sum of forces of interactions with other particles in the world */ for ( Particle q : particles ) { p.addForceFromParticle( q ); } /* Apply summed forces */ p.applyForce (); /* Relax CPU usage */ Thread.yield (); } } }
void WorldOfParticles.dump | ( | ) |
Dumps internal state for all particles to System.out (for debugging purposes).
Definition at line 202 of file WorldOfParticles.java.
References getParticleCount(), and particles.
Referenced by JPWorld.keyPressed().
{ synchronized( this ) { System.out.printf( "%8s %8s %8s %8s %8s %8s %8s\n", "lifetime", "xPos", "yPos", "vx", "vy", "ax", "ay" ); for( Particle p : particles ) { p.dump (); } System.out.printf( "Total %d particles\n", getParticleCount () ); } }
Barrier WorldOfParticles.getBarrier | ( | ) |
Gets boundaries of infinite potential barrier keeping particles together.
Definition at line 263 of file WorldOfParticles.java.
References context, and WorldOfParticles.RenderingContext.getBarrier().
Referenced by Particle.integrateNewtonLaws().
{ return context.getBarrier (); }
GraphicsConfiguration WorldOfParticles.getGraphicsConfiguration | ( | ) |
Gets the GraphicsConfiguration associated with parent Component.
Definition at line 151 of file WorldOfParticles.java.
References context, and WorldOfParticles.RenderingContext.getGraphicsConfiguration().
Referenced by Particle.createParticleImage().
{ return context.getGraphicsConfiguration (); }
int WorldOfParticles.getParticleCount | ( | ) |
Gets number of particles in our world.
Definition at line 116 of file WorldOfParticles.java.
References particles.
Referenced by JPWorld.actionPerformed(), JPWorld.createPairOfChargedParticles(), dump(), and JPWorld.paintComponent().
{ synchronized( this ) { return particles.size (); } }
double WorldOfParticles.getTimeScale | ( | ) |
Gets world's time to 'real' world's time ratio.
Definition at line 141 of file WorldOfParticles.java.
References timeScale.
Referenced by JPWorld.paintComponent(), and Particle.run().
{ return timeScale; }
void WorldOfParticles.incTimeScale | ( | double | factor ) |
Increments/decrements time scale.
factor | delta time |
Definition at line 129 of file WorldOfParticles.java.
References timeScale.
Referenced by JPWorld.keyPressed().
boolean WorldOfParticles.isPaused | ( | ) |
Returns indicator whether forces between particles are calculated or not.
Definition at line 98 of file WorldOfParticles.java.
References paused.
Referenced by Particle.run().
{ return paused; }
void WorldOfParticles.makeCentripetalVelocities | ( | ) |
Makes velocities for all particles to be centripetal to current particles' acceleration.
Definition at line 234 of file WorldOfParticles.java.
References particles.
Referenced by JPWorld.keyPressed().
void WorldOfParticles.paint | ( | Graphics | g, |
boolean | annotate | ||
) |
Renders the world on Graphics
.
g | where to render |
annotate | annotate particle accelerations |
Definition at line 250 of file WorldOfParticles.java.
References particles.
Referenced by JPWorld.paintComponent().
void WorldOfParticles.removeParticle | ( | Particle | p ) |
Removes the particle p
from the world.
p | instance of the Particle to be removed |
Definition at line 188 of file WorldOfParticles.java.
References context, WorldOfParticles.RenderingContext.onParticleCountChanged(), and particles.
Referenced by Particle.run().
{ synchronized( this ) { particles.remove( p ); /* Inform parent that number of particles has been changed */ context.onParticleCountChanged( particles.size () ); } }
void WorldOfParticles.resetVelocities | ( | ) |
Resets velocities for all particles to 0.
Definition at line 220 of file WorldOfParticles.java.
References particles.
Referenced by JPWorld.keyPressed().
void WorldOfParticles.run | ( | ) |
Main thread that calculates interactions (forces) between all particles in the world.
Definition at line 305 of file WorldOfParticles.java.
References calculateInteractions(), paused, and running.
{ final int sleepMillis = 5; // ~200 Hz while( running ) { try { Thread.sleep( sleepMillis ); } catch( InterruptedException ie ) { } if ( ! paused ) { calculateInteractions (); } } }
void WorldOfParticles.togglePaused | ( | ) |
Turns on/off calculations of forces between particles.
Definition at line 106 of file WorldOfParticles.java.
References paused.
Referenced by JPWorld.keyPressed().
RenderingContext WorldOfParticles.context [private] |
Particular instance with rendering context interface.
Definition at line 78 of file WorldOfParticles.java.
Referenced by addNewParticle(), getBarrier(), getGraphicsConfiguration(), removeParticle(), and WorldOfParticles().
Thread WorldOfParticles.mainThread [private] |
Each object (i.e.
world) has it's own physical rules. Forces between particles in our world are are calculated in object's main thread.
Definition at line 73 of file WorldOfParticles.java.
Referenced by WorldOfParticles().
ArrayList<Particle> WorldOfParticles.particles = new ArrayList<Particle> () [private] |
Collection of Particles
that belongs to this world.
Definition at line 51 of file WorldOfParticles.java.
Referenced by addNewParticle(), calculateInteractions(), dump(), getParticleCount(), makeCentripetalVelocities(), paint(), removeParticle(), and resetVelocities().
volatile boolean WorldOfParticles.paused = false [private] |
Indicates whether forces between particles are calculated or not.
The velocities of the particles are freezed if forces are not calculated.
Definition at line 62 of file WorldOfParticles.java.
Referenced by isPaused(), run(), and togglePaused().
volatile boolean WorldOfParticles.running = true [private] |
Indicates whether to keep world's thread running or not.
Definition at line 67 of file WorldOfParticles.java.
Referenced by run().
volatile double WorldOfParticles.timeScale = 0.5 [private] |
World's time to 'real' world's time ratio.
Slower if less then 1.
Definition at line 56 of file WorldOfParticles.java.
Referenced by getTimeScale(), and incTimeScale().