Canvas
A composable that hands you a DrawScope over a blank Swing surface so you can draw whatever you like.
Repaint is snapshot-observed. Any snapshot state you read directly inside onDraw is tracked; when such state changes the surface repaints and re-invokes onDraw automatically. Read your state where you use it, at paint time:
var radius by remember { mutableFloatStateOf(10f) }
Canvas(modifier = SwingModifier.preferredSize(Dimension(200, 200))) {
drawCircle(Color.RED, radius = radius)
}The drawing is a draw node appended to modifier, so it runs inside every decoration modifier declares. The surface is non-opaque and paints no background of its own: only what onDraw renders appears. Size it with the preferred-size modifier (see org.jetbrains.compose.swing.modifier.layout.preferredSize); without one it asks only for the paint outsets its decoration needs.
Parameters
the SwingModifier applied to the underlying component.
optional RenderingHints configured on the surface's Graphics2D, defaulting to DefaultCanvasRenderingHints (antialiasing enabled). Pass null to use the graphics context's existing hints without additions.
receives the DrawScope whose drawing area is the component's layout bounds less its border. Called on the Swing event dispatch thread during painting.