ImageLayer
An offscreen raster that records Java2D drawing once and draws it again as often as you like, through a transform the caller can change without re-recording.
The graphics device may hold a recording in video memory, and may discard it, such as when the display changes. A recording lost that way is dropped when it is next drawn, filtered, read back or asked for with hasContent: draw draws nothing, filter does nothing, toBufferedImage fails and hasContent answers false. Swing repaints what it lost along with it, so an owner that records on every paint needs nothing more, and one that keeps a recording across paints checks hasContent when it paints and records again.
Record a block of drawing (record(width, height, scale, block)), the pixels of a live component (record(component, scale)), or drawing aligned to the pixels of a destination it is drawn back onto (record(destination, width, height, block)), then draw the result with draw - typically from a draw block, but any Graphics2D takes it. A layer holds one recording: a second record replaces it.
The properties are plain fields, as those of androidx's GraphicsLayer are: writing one, or recording, changes nothing already painted. The next draw uses them, so whatever paints the layer repaints for a change the way it does for any other state it draws from.
A layer is Java2D, so recording drawing, drawing it back and reading it back are as free of the event dispatch thread as the images underneath. Recording a live component is not: that reads a Swing component tree, and runs on the event dispatch thread like everything else that touches one. A layer is used from one thread at a time: recording, filtering and releasing it reuse the surfaces and buffers of earlier recordings, so a draw or read-back on another thread may see the pixels of a later recording.
See also
Properties
Whether this layer holds a recording to draw, dropping one the graphics device discarded.
The effect draw applies to the recording, or null to draw it as recorded. Defaults to null.
The rotation draw draws at, in degrees about pivotOffset; a positive angle turns clockwise on screen. Defaults to 0f.
The horizontal scale draw draws at, about pivotOffset. Defaults to 1f.
The vertical scale draw draws at, about pivotOffset. Defaults to 1f.
The horizontal offset draw draws at, added to its x. Defaults to 0f.
The vertical offset draw draws at, added to its y. Defaults to 0f.
Functions
Draws the recording onto destination at x, y, under this layer's transform and alpha. Draws nothing while this layer has no content.
Replaces the recording with the result of applying operation to it. Does nothing while this layer has no content.
Records component and its descendants at the bounds the component already carries, replacing whatever this layer held.
Records what block paints within width x height into device pixels aligned to the pixels it would paint on destination, under the destination's transform and clip, replacing whatever this layer held.
Frees the recorded buffer and the buffers kept for reuse, leaving this layer empty: hasContent is false, size is 0x0, draw draws nothing and toBufferedImage fails.
Returns a copy of the recorded pixels at device resolution - the user-space size multiplied by the scale it was recorded at - untransformed and with no alpha or renderEffect applied.