My use case that I'm attempting to create is a way to render a Compose application directly to a file, ideally without requiring an actual GUI window, e.g. for rendering the app on a server that does not have any UI.
Using Jetpack Compose (Desktop) I attempted different solutions to paint the window to a file, but it all ends up with a white or empty image:
val ge = GraphicsEnvironment.getLocalGraphicsEnvironment()
val window = ComposeWindow(graphicsConfiguration = ge.defaultScreenDevice.defaultConfiguration)
window.setSize(200,200)
window.add(JLabel("Beispiel JLabel"))
window.setContent { App() }
window.repaint()
window.doLayout()
window.isVisible = true
val img = BufferedImage(window.width, window.height, BufferedImage.TYPE_INT_ARGB)
val g: Graphics2D = img.createGraphics()
window.printAll(g)
g.dispose()
val path: java.nio.file.Path = java.nio.file.Path.of("output.png")
ImageIO.write(img, "png", path.toFile())
Ideally, I would be able to skip creating a window altogether and let it render directly to e.g. a Skija surface. Could a unit test using Android as a target help here, as they provide a test rule for it?