java.lang.Object
javafx.scene.image.PixelBuffer<T>
- Type Parameters:
T
- the type ofBuffer
that stores the pixel data. OnlyByteBuffer
andIntBuffer
are supported.
public class PixelBuffer<T extends Buffer> extends Object
The
PixelBuffer
class represents pixel data that is constructed from
a java.nio.Buffer
supplied by the application.
A WritableImage
can use this PixelBuffer
directly without copying the pixel data.
This PixelBuffer
can be shared among multiple WritableImage
s.
Pixel data should be stored either in an IntBuffer
using a PixelFormat
of type
INT_ARGB_PRE
or in a ByteBuffer
using a PixelFormat
of type BYTE_BGRA_PRE
.
When the Buffer
is updated using the PixelBuffer.updateBuffer
method,
all WritableImage
s that were created using this PixelBuffer
are redrawn.
Example code that shows how to create a PixelBuffer
:
// Creating a PixelBuffer using BYTE_BGRA_PRE pixel format.
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(width * height * 4);
PixelFormat<ByteBuffer> pixelFormat = PixelFormat.getByteBgraPreInstance();
PixelBuffer<ByteBuffer> pixelBuffer = new PixelBuffer<>(width, height, byteBuffer, pixelFormat);
Image img = new WritableImage(pixelBuffer);
// Creating a PixelBuffer using INT_ARGB_PRE pixel format.
IntBuffer intBuffer = IntBuffer.allocate(width * height);
PixelFormat<IntBuffer> pixelFormat = PixelFormat.getIntArgbPreInstance();
PixelBuffer<IntBuffer> pixelBuffer = new PixelBuffer<>(width, height, intBuffer, pixelFormat);
Image img = new WritableImage(pixelBuffer);
- Since:
- 13
- See Also:
WritableImage(PixelBuffer)
-
Constructor Summary
Constructors Constructor Description PixelBuffer(int width, int height, T buffer, PixelFormat<T> pixelFormat)
Constructs aPixelBuffer
using the specifiedBuffer
andPixelFormat
. -
Method Summary
Modifier and Type Method Description T
getBuffer()
Returns thebuffer
of thisPixelBuffer
.int
getHeight()
Returns theheight
of thisPixelBuffer
.PixelFormat<T>
getPixelFormat()
Returns thePixelFormat
of thisPixelBuffer
.int
getWidth()
Returns thewidth
of thisPixelBuffer
.void
updateBuffer(Callback<PixelBuffer<T>,Rectangle2D> callback)
Invokes the specifiedCallback
method and updates the dirty region of allWritableImage
s that were created using thisPixelBuffer
.
-
Constructor Details
-
PixelBuffer
Constructs aPixelBuffer
using the specifiedBuffer
andPixelFormat
. The type of the specifiedPixelFormat
must be eitherPixelFormat.Type.INT_ARGB_PRE
orPixelFormat.Type.BYTE_BGRA_PRE
.The constructor does not allocate memory to store the pixel data. The application must provide a buffer with sufficient memory for the combination of dimensions
(width, height)
and the type ofPixelFormat
. ThePixelFormat.Type.INT_ARGB_PRE
requires anIntBuffer
with minimum capacity ofwidth * height
, and thePixelFormat.Type.BYTE_BGRA_PRE
requires aByteBuffer
with minimum capacity ofwidth * height * 4
.- Parameters:
width
- width in pixels of thisPixelBuffer
height
- height in pixels of thisPixelBuffer
buffer
- the buffer that stores the pixel datapixelFormat
- the format of pixels in thebuffer
- Throws:
IllegalArgumentException
- if eitherwidth
orheight
is negative or zero, or if the type ofpixelFormat
is unsupported, or ifbuffer
does not have sufficient memory, or if the type ofbuffer
andpixelFormat
do not matchNullPointerException
- ifbuffer
orpixelFormat
isnull
-
-
Method Details
-
getBuffer
Returns thebuffer
of thisPixelBuffer
.- Returns:
- the
buffer
of thisPixelBuffer
-
getWidth
public int getWidth()Returns thewidth
of thisPixelBuffer
.- Returns:
- the
width
of thisPixelBuffer
-
getHeight
public int getHeight()Returns theheight
of thisPixelBuffer
.- Returns:
- the
height
of thisPixelBuffer
-
getPixelFormat
Returns thePixelFormat
of thisPixelBuffer
.- Returns:
- the
PixelFormat
of thisPixelBuffer
-
updateBuffer
Invokes the specifiedCallback
method and updates the dirty region of allWritableImage
s that were created using thisPixelBuffer
. TheCallback
method is expected to update the buffer and return aRectangle2D
that encloses the dirty region, or returnnull
to indicate that the entire buffer is dirty.This method must be called on the JavaFX Application Thread.
Example code that shows how to use this method:
Callback<PixelBuffer<ByteBuffer>, Rectangle2D> callback = pixelBuffer -> { ByteBuffer buffer = pixelBuffer.getBuffer(); // Update the buffer. return new Rectangle2D(x, y, dirtyWidth, dirtyHeight); }; pixelBuffer.updateBuffer(callback);
- Parameters:
callback
- theCallback
method that updates the buffer- Throws:
IllegalStateException
- if this method is called on a thread other than the JavaFX Application Thread.NullPointerException
- ifcallback
isnull
-