You can create blobs that appear to merge by adding together scalar fields and coloring pixels according to the total. In this SwingLoops example, five moving centers produce luminous shapes that join and separate without collision detection or a fluid engine.

Source code
Save this code as MetaballFusion.java:
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
public class MetaballFusion extends JPanel {
private static final int W = 180, H = 320, COUNT = 5;
private static final double CYCLE = 12.0;
private final BufferedImage image =
new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
private final int[] pixels =
((DataBufferInt) image.getRaster().getDataBuffer()).getData();
private final double[] cx = new double[COUNT], cy = new double[COUNT];
private final Timer timer = new Timer(33, _ -> repaint());
private long start;
public MetaballFusion() {
setPreferredSize(new Dimension(450, 800));
setBackground(new Color(6, 11, 24));
}
@Override public void addNotify() {
super.addNotify();
start = System.nanoTime();
timer.start();
}
@Override public void removeNotify() {
timer.stop();
super.removeNotify();
}
private static double smooth(double a, double b, double x) {
double u = Math.max(0, Math.min(1, (x - a) / (b - a)));
return u * u * (3 - 2 * u);
}
private void render(double seconds) {
double phase = 2 * Math.PI * (seconds % CYCLE) / CYCLE;
for (int i = 0; i < COUNT; i++) {
double offset = i * 2 * Math.PI / COUNT;
cx[i] = W / 2.0 + 48 * Math.cos(phase + offset);
cy[i] = H / 2.0 + 70 * Math.sin(2 * phase + offset);
}
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
double field = 0;
for (int i = 0; i < COUNT; i++) {
double dx = x - cx[i], dy = y - cy[i];
field += 196.0 / (dx * dx + dy * dy + 1);
}
double fill = smooth(0.94, 1.06, field);
double edge = Math.exp(-Math.pow((field - 1) / 0.12, 2));
double inside = fill * (0.58 + 0.25 *
Math.sin(x * 0.035 + y * 0.02 + phase));
int red = (int) Math.min(255, 6 + 48 * inside + 116 * edge);
int green = (int) Math.min(255, 11 + 192 * inside + 150 * edge);
int blue = (int) Math.min(255, 24 + 210 * inside + 170 * edge);
pixels[y * W + x] = (red << 16) | (green << 8) | blue;
}
}
}
@Override protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
render((System.nanoTime() - start) / 1e9);
Graphics2D g = (Graphics2D) graphics.create();
try {
double scale = Math.min(getWidth() / (double) W,
getHeight() / (double) H);
g.translate((getWidth() - W * scale) / 2,
(getHeight() - H * scale) / 2);
g.scale(scale, scale);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(image, 0, 0, null);
} finally {
g.dispose();
}
}
static void main() {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("SwingLoops - Metaball Fusion");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new MetaballFusion());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
Run the example
Install a JDK and use a desktop environment that supports Swing. In the directory containing the source file, run:
javac MetaballFusion.java
java MetaballFusion
Compilation was verified with JDK 25. Visual playback has not yet been verified. Close the window to exit.
Build a scalar field
At each pixel, the program calculates the distance from each moving center and adds a contribution:
field += 196.0 / (dx * dx + dy * dy + 1);
A nearby center contributes a large value; a distant center contributes less. The added one prevents division by zero at a center.
The visible surface is defined by a field value near one. For an isolated center, that value would occur at a distance of approximately 14 pixels. The combined fields of neighboring centers enlarge and deform that boundary.
Why do the blobs merge?
Suppose a pixel between two centers receives 0.6 from each. Neither contribution reaches the threshold on its own, but their sum is 1.2. That pixel belongs to the filled region.
As the centers approach, more pixels between them cross the threshold and form a connecting neck. When they separate, those pixels fall below the threshold again. The program does not need a special instruction to join or split shapes.
Color the boundary
A smoothstep function changes the fill from zero to one over the interval 0.94–1.06:
double fill = smooth(0.94, 1.06, field);
A narrow Gaussian-shaped function adds a bright edge around the threshold:
double edge = Math.exp(-Math.pow((field - 1) / 0.12, 2));
Its value is greatest when the field equals one and decreases on either side. This is a glow in field-value space rather than a constant-width blur in screen pixels.
The interior also uses a gentle sinusoidal color variation. The final RGB channels are clamped before being packed into an integer pixel.
Move the centers
Each center follows a sinusoidal path with a different phase offset:
cx[i] = W / 2.0 + 48 * Math.cos(phase + offset);
cy[i] = H / 2.0 + 70 * Math.sin(2 * phase + offset);
The vertical motion completes two oscillations for each horizontal oscillation. After 12 seconds, both coordinates and the interior coloring return to their starting states.
Render with Swing
The calculation runs at 180 × 320 pixels to limit per-frame work. A BufferedImage backed by an integer array lets the program write colors directly. Java 2D scales the image with bilinear interpolation for display.
A Swing timer requests repaints approximately every 33 milliseconds. Motion depends on elapsed time, not the number of frames. The interface and painting run on the event-dispatch thread. More fields or a higher internal resolution increase painting work and may reduce responsiveness.
Customize the effect
Try increasing the strength from 196 to 256. Because the boundary depends on the sum, this makes the blobs larger and encourages merging. Then increase the horizontal orbit amplitude from 48 to 60 to pull them farther apart.
This example is an artistic field visualization, not a simulation of liquid pressure or surface tension. The complete source is free to copy and run.
Explore the SwingLoops repository for more Java animation examples.