- java.lang.Object
-
- javafx.scene.image.PixelBuffer<T>
-
- Type Parameters:
T- the type ofBufferthat stores the pixel data. OnlyByteBufferandIntBufferare supported.
public class PixelBuffer<T extends Buffer> extends Object
ThePixelBufferclass represents pixel data that is constructed from ajava.nio.Buffersupplied by the application. AWritableImagecan use thisPixelBufferdirectly without copying the pixel data. ThisPixelBuffercan be shared among multipleWritableImages. Pixel data should be stored either in anIntBufferusing aPixelFormatof typeINT_ARGB_PREor in aByteBufferusing aPixelFormatof typeBYTE_BGRA_PRE. When theBufferis updated using thePixelBuffer.updateBuffermethod, allWritableImages that were created using thisPixelBufferare 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 aPixelBufferusing the specifiedBufferandPixelFormat.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TgetBuffer()Returns thebufferof thisPixelBuffer.intgetHeight()Returns theheightof thisPixelBuffer.PixelFormat<T>getPixelFormat()Returns thePixelFormatof thisPixelBuffer.intgetWidth()Returns thewidthof thisPixelBuffer.voidupdateBuffer(Callback<PixelBuffer<T>,Rectangle2D> callback)Invokes the specifiedCallbackmethod and updates the dirty region of allWritableImages that were created using thisPixelBuffer.
-
-
-
Constructor Detail
-
PixelBuffer
public PixelBuffer(int width, int height, T buffer, PixelFormat<T> pixelFormat)Constructs aPixelBufferusing the specifiedBufferandPixelFormat. The type of the specifiedPixelFormatmust be eitherPixelFormat.Type.INT_ARGB_PREorPixelFormat.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_PRErequires anIntBufferwith minimum capacity ofwidth * height, and thePixelFormat.Type.BYTE_BGRA_PRErequires aByteBufferwith minimum capacity ofwidth * height * 4.- Parameters:
width- width in pixels of thisPixelBufferheight- height in pixels of thisPixelBufferbuffer- the buffer that stores the pixel datapixelFormat- the format of pixels in thebuffer- Throws:
IllegalArgumentException- if eitherwidthorheightis negative or zero, or if the type ofpixelFormatis unsupported, or ifbufferdoes not have sufficient memory, or if the type ofbufferandpixelFormatdo not matchNullPointerException- ifbufferorpixelFormatisnull
-
-
Method Detail
-
getBuffer
public T getBuffer()
Returns thebufferof thisPixelBuffer.- Returns:
- the
bufferof thisPixelBuffer
-
getWidth
public int getWidth()
Returns thewidthof thisPixelBuffer.- Returns:
- the
widthof thisPixelBuffer
-
getHeight
public int getHeight()
Returns theheightof thisPixelBuffer.- Returns:
- the
heightof thisPixelBuffer
-
getPixelFormat
public PixelFormat<T> getPixelFormat()
Returns thePixelFormatof thisPixelBuffer.- Returns:
- the
PixelFormatof thisPixelBuffer
-
updateBuffer
public void updateBuffer(Callback<PixelBuffer<T>,Rectangle2D> callback)
Invokes the specifiedCallbackmethod and updates the dirty region of allWritableImages that were created using thisPixelBuffer. TheCallbackmethod is expected to update the buffer and return aRectangle2Dthat encloses the dirty region, or returnnullto 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- theCallbackmethod that updates the buffer- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.NullPointerException- ifcallbackisnull
-
-