When creating a particle system, it is important to consider the visual density of the particles being
emitted, that is, the proportion of a screen area that is dedicated to particles. To create visual effects that
look good, you need to be aware of the factors that affect the visual density of a particle system.
Changing the rate at which particles are emitted controls how dense the effect looks. An emitter that
creates a new particle every second will look sluggish and empty, while one that emits particles every
hundredth of a second will rapidly produce a thick collection of particles. Of course, the speed at which
particles travel away from the emitter also affects visual density. Fast-moving particles require an
emitter with a high emission rate to avoid appearing sparse. Similarly, slow-moving particles benefit
from an emitter with a slow emit rate to avoid over-saturating an area with particles.
The size of the particles is also significant. As the emit rate and animation speed of the particles
contribute to the visual density of a particle system, so too can the size of each particle be adjusted to
achieve just the right density.
Particle Appearance and Behavior
While visual density describes how the effect will look as a whole, the eye naturally picks out individual
particles as they travel across the scene. Controlling the attributes of each particle helps achieve the
The simplest particles travel in straight lines and then vanish after some time. But there can be
many variations in this behavior. For example, particles might start out fast and then slow as they reach
the end of their life cycle. Particles could also fade to transparent or change color as they age.
Creating particles that do not travel in a straight line can be a powerful tool when creating an effect.
One option is to have particles that “fall” toward the ground, like the sparks of an aerial fireworks
display. Or you could have particles that travel erratically, like air bubbles in water racing to the surface.
One of the most important aspects of particles' appearance is how they interact with each other.
The first example just uses opaque particles, but later examples show how partially transparent particles,
or particles that are combined with a blend effect, create eye-catching results.
JavaFX provides powerful techniques for creating animations through its KeyFrame and Timeline classes.
In our implementation, we will use a single Timeline with a single KeyFrame to coordinate the animation
of all of the particles. To achieve this coordination, our KeyFrame calls a function that updates the
location of each particle.
Example 1: Core Classes
This example introduces the basic classes you need to implement a particle system. The code will create
a scene with a number of red circular particles being emitted from the center of the visible area, shown
as small gray circles in Figure 2-2. You can find the code in the package org.lj.jfxe.chapter2.example1
of the companion source code. There are two implementation classes, Emitter and Particle , as well as a
Main class that simply creates a Stage and displays a single Emitter . This example focuses on the pattern
used to implement a particle system; later examples look at ways to refine the visual experience. To see
this example in action, run the file Main.fx.