Publish light and dark Swing screenshots on GitHub
Capture light and dark versions of a Swing screen without opening a window. Use the HTML picture element to show the matching PNG on GitHub.
I rendered the same Swing screen in light and dark mode without opening a window. GitHub Markdown can use a
<picture>element to show the PNG that matches the reader’s theme.
Generate a matched pair of screenshots
I keep the restaurant data, filters, selection, and viewport the same across both screenshots. Select a different restaurant and both the highlighted row and the detail panel change. Capture a narrower window and the filter controls can move. Keeping those inputs fixed lets a reader compare colors in the same spots on both images.
The Explore gallery in the Restaurant Information System (RSIS) seeds a temporary database once and uses it for both captures. It calls the same render method twice at 1,440 by 920 pixels and passes the palette explicitly on each call. That makes the images reproducible even on a machine with a different desktop theme. I covered where those colors come from in the RSIS theme implementation. ExploreScreenGallery source
1
./gradlew exploreGallery componentGallery
exploreGallery writes explore-light.png and explore-dark.png to build/screen-gallery/. I use those files to show the full layout. When the focus is on individual buttons, filters, or their states, I use light.png and dark.png from componentGallery in build/component-gallery/. Both Gradle tasks set java.awt.headless=true and run from the test source set. RSIS Gradle tasks
Figure 1. The light Explore screenshot at 1,440 by 920 pixels.
Figure 2. The dark capture keeps the search results, selection, and dimensions from Figure 1.
Paint a panel into a PNG without a window
The gallery builds the screen inside a standalone JPanel. I set the panel’s size, lay out each child container, then pass the BufferedImage graphics context to printAll(). Setting the outer size gives the layout managers room to assign positions and dimensions to the children. If you skip layout, a label or nested panel can still have a width of zero when you paint it.
Run Swing setup, layout, and painting on the event dispatch thread (EDT). The gallery applies the palette before creating any components. It builds the screen in one EDT call, then paints it in another so the map panel has time to switch to its no-API-key state. Two layout passes let the labels wrap and the split pane settle at the capture width. RSIS capture sequence · Java 17 Component.printAll
Save this Java 17 example as PanelCapture.java. It writes two PNGs of the same welcome label on light and dark backgrounds. If your panel contains nested containers, call doLayout() on each of them as the RSIS gallery does.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import javax.imageio.ImageIO;
import javax.swing.*;
class PanelCapture {
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(() -> {
capture("explore-light.png", Color.WHITE, Color.BLACK);
capture("explore-dark.png", new Color(32, 35, 40), Color.WHITE);
});
}
private static void capture(String file, Color background, Color foreground) {
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(background);
JLabel label = new JLabel("Restaurants", SwingConstants.CENTER);
label.setForeground(foreground);
panel.add(label);
panel.setSize(640, 320);
panel.doLayout();
var image = new BufferedImage(640, 320, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
try {
panel.printAll(graphics);
} finally {
graphics.dispose();
}
try {
ImageIO.write(image, "png", new File(file));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
1
java -Djava.awt.headless=true PanelCapture.java
Select the image in GitHub Markdown
Put both PNGs in screenshots/ next to the README. A <picture> element lets you give the dark image a media condition in <source>. When that condition doesn’t match, <img> provides the light image. Use an alt description that works for both files, such as the filters and results shown here. GitHub’s light and dark image guide · GitHub README relative path rules
1
2
3
4
5
6
<picture>
<source media="(prefers-color-scheme: dark)"
srcset="./screenshots/explore-dark.png">
<img src="./screenshots/explore-light.png"
alt="RSIS restaurant search with district filters and results">
</picture>
For PNGs attached to an issue or pull request, copy the URLs GitHub inserts in the editor. Put the dark URL in srcset and the light URL in src. Before reusing a private repository attachment in a public document, check whether the intended reader can open it. GitHub file attachment documentation
RSIS chooses its palette at startup. On GitHub, changing the page appearance swaps between PNGs you already saved. You can check that behavior with the Swing app closed.
Choose what a screenshot can verify
| What I need to show | Capture method |
|---|---|
| The same layout across palettes, or a seeded empty state | Headless gallery |
| Native window decoration, live focus, or a popup opening | Screenshot or recording from the desktop app |
| A direct comparison of the two palettes | Side-by-side screenshots in the article |
If the point is to show a popup opening or focus moving from a filter to a result, capture a running app. For a README image pair, preview the Markdown on GitHub, then switch the page between light and dark. When an image fails to load, open its URL directly first. That tells you whether the path or permissions are the problem before you touch the media query.
Check both themes before sharing
- Capture both images with the same data, viewport, and selection.
- Check that the intended reader can open the light and dark PNGs.
- Switch themes on the rendered GitHub page and check that the image changes.
- Write alt text that describes the screen shown in both PNGs.