You can create an hourglass animation by assigning each particle a starting position, a launch time, and a destination. Moving the particles through a shared point at the neck gives the impression of sand passing between two chambers.

This SwingLoops example uses 650 golden particles, smoothstep easing, and a Java 2D clipping region. It is a stylized animation rather than a simulation of granular material.
Source code
Save this code as ParticleHourglass.java:
import java.awt.*;
import java.awt.geom.*;
import java.util.Random;
import javax.swing.*;
public class ParticleHourglass extends JPanel {
private static final int W = 450, H = 800, COUNT = 650;
private static final double CYCLE = 12;
private final double[] x = new double[COUNT], y = new double[COUNT];
private final Timer timer = new Timer(16, _ -> repaint());
private long start;
public ParticleHourglass() {
setPreferredSize(new Dimension(W, H));
setBackground(new Color(8, 15, 27));
Random random = new Random(42);
for (int i = 0; i < COUNT; i++) {
// Uniform sampling in the upper triangular chamber.
double depth = Math.sqrt(random.nextDouble());
x[i] = (random.nextDouble() * 2 - 1) * 130 * depth;
y[i] = -210 * depth;
}
}
@Override public void addNotify() {
super.addNotify();
start = System.nanoTime();
timer.start();
}
@Override public void removeNotify() {
timer.stop();
super.removeNotify();
}
private static double clamp(double u) {
return Math.max(0, Math.min(1, u));
}
private static double ease(double u) {
return u * u * (3 - 2 * u);
}
@Override protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D g = (Graphics2D) graphics.create();
try {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
double scale = Math.min(getWidth() / (double) W,
getHeight() / (double) H);
g.translate(getWidth() / 2.0, getHeight() / 2.0);
g.scale(scale, scale);
double t = ((System.nanoTime() - start) / 1e9) % CYCLE;
// Fade before resetting the particle positions.
float alpha = (float) Math.min(clamp(t / .3), clamp(12 - t));
g.setComposite(AlphaComposite.SrcOver.derive(alpha));
Path2D chamber = new Path2D.Double();
chamber.moveTo(-140, -220);
chamber.lineTo(140, -220);
chamber.lineTo(0, 0);
chamber.closePath();
chamber.moveTo(0, 0);
chamber.lineTo(140, 220);
chamber.lineTo(-140, 220);
chamber.closePath();
Graphics2D sand = (Graphics2D) g.create();
try {
sand.clip(chamber);
for (int i = 0; i < COUNT; i++) {
double launch = .6 + 7.5 * i / (COUNT - 1.0);
double u = clamp((t - launch) / 1.15);
double px, py;
if (u < .5) {
double blend = ease(u * 2);
px = x[i] * (1 - blend);
py = y[i] * (1 - blend);
} else {
double blend = ease((u - .5) * 2);
px = -x[i] * blend;
py = -y[i] * blend;
}
sand.setColor(new Color(248,
171 + i % 60, 66 + i % 45));
sand.fill(new Ellipse2D.Double(px - 1.3, py - 1.3, 2.6, 2.6));
}
} finally {
sand.dispose();
}
g.setColor(new Color(118, 206, 225, 180));
g.setStroke(new BasicStroke(2f));
g.draw(chamber);
g.setColor(new Color(203, 225, 237));
g.fillRoundRect(-157, -233, 314, 12, 8, 8);
g.fillRoundRect(-157, 221, 314, 12, 8, 8);
} finally {
g.dispose();
}
}
static void main() {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("SwingLoops - Particle Hourglass");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new ParticleHourglass());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
Run the example
You need a JDK 25 and a desktop environment that supports Swing. Open a terminal in the source directory:
javac ParticleHourglass.java
java ParticleHourglass
Fill a triangular chamber
Each particle starts inside the upper triangle. The distance from the neck is sampled using a square root:
double depth = Math.sqrt(random.nextDouble());
x[i] = (random.nextDouble() * 2 - 1) * 130 * depth;
y[i] = -210 * depth;
The available width grows with depth. A uniform depth distribution would crowd the narrow region near the neck relative to its area. Taking the square root gives greater weight to wider slices, producing a uniform distribution over the interior of the sampling triangle.
A fixed random seed makes the arrangement repeatable between runs.
Give each particle a launch time
Particles do not start together. Their launch times are distributed over 7.5 seconds:
double launch = .6 + 7.5 * i / (COUNT - 1.0);
double u = clamp((t - launch) / 1.15);
The first particle begins at 0.6 seconds. The last begins at 8.1 seconds and finishes at 9.25 seconds. Before launch, its progress is clamped to zero. After arrival, it is clamped to one.
Launch order follows particle index, not physical height. Particles can cross one another and do not collide.
Route each particle through the neck
The journey has two stages. During the first half, interpolation brings the particle from its initial position to the origin. During the second half, it moves from the origin to the mirrored destination (-x, -y).
For each stage, smoothstep transforms progress:
return u * u * (3 - 2 * u);
Its slope is zero at both endpoints. That creates gentle starts and stops, including a brief slowing at the neck. This controlled motion does not model gravitational acceleration.
Mirroring the initial coordinates places every destination inside the lower triangle, so all particles have valid endpoints without a packing algorithm.
Keep particles inside the glass
The two triangular chambers form a Path2D. A copied graphics context uses that path as its clipping region:
sand.clip(chamber);
Clipping prevents particle circles from protruding outside the chamber boundary. At the exact neck, the chamber has zero width, so particles may be partially or fully clipped while crossing. The glass outline is drawn afterward using the unclipped parent context.
Repeat the sequence
The sequence lasts 12 seconds. It fades in over the first 0.3 seconds and fades out from 11 to 12 seconds. Particle positions reset while the drawing is transparent.
A Swing timer requests repaints approximately every 16 milliseconds. Motion is based on elapsed time from System.nanoTime(), so a delayed repaint does not alter the planned arrival times. The interface, painting, and timer callbacks run on Swing’s event-dispatch thread.
Customize the animation
Try changing COUNT from 650 to 400 for a sparser result. Increasing the launch-time spread produces a slower transfer, but keep the last arrival before the fade starts at 11 seconds.
You can also adjust particle size and colors. For more realistic sand behavior, a future implementation would need collision handling and a settling model; this example deliberately uses predefined paths.
The complete source code above is free to copy and run. Explore the SwingLoops repository for more Java animation examples.


