下面列出了怎么用java.awt.image.VolatileImage的API类实例代码及写法,或者点击链接到github查看源代码。
private void initBackbuffer() {
createBackbuffer();
int res = bb.validate(getGraphicsConfiguration());
if (res == VolatileImage.IMAGE_INCOMPATIBLE) {
bb = null;
createBackbuffer();
bb.validate(getGraphicsConfiguration());
res = VolatileImage.IMAGE_RESTORED;
}
if (res == VolatileImage.IMAGE_RESTORED) {
Graphics g = bb.getGraphics();
g.setColor(new Color(rnd.nextInt(0x00ffffff)));
g.fillRect(0, 0, bb.getWidth(), bb.getHeight());
volSprite = createVolatileImage(100, 100);
}
volSprite.validate(getGraphicsConfiguration());
}
protected BufferedImage getBufferedImage(Image img) {
if (img instanceof BufferedImage) {
// Otherwise we expect a BufferedImage to behave as a standard BI
return (BufferedImage)img;
} else if (img instanceof ToolkitImage) {
// This can be null if the image isn't loaded yet.
// This is fine as in that case our caller will return
// as it will only draw a fully loaded image
return ((ToolkitImage)img).getBufferedImage();
} else if (img instanceof VolatileImage) {
// VI needs to make a new BI: this is unavoidable but
// I don't expect VI's to be "huge" in any case.
return ((VolatileImage)img).getSnapshot();
} else {
// may be null or may be some non-standard Image which
// shouldn't happen as Image is implemented by the platform
// not by applications
// If you add a new Image implementation to the platform you
// will need to support it here similarly to VI.
return null;
}
}
private static boolean render(BufferedImage src, VolatileImage dst) {
int cnt = 5;
do {
Graphics2D g = dst.createGraphics();
g.setColor(dstColor);
g.fillRect(0, 0, dst.getWidth(), dst.getHeight());
g.drawImage(src, 0, 0, null);
g.dispose();
} while (dst.contentsLost() && (--cnt > 0));
if (cnt == 0) {
System.err.println("Test failed: unable to render to volatile destination");
return false;
}
BufferedImage s = dst.getSnapshot();
return s.getRGB(1,1) == srcColor.getRGB();
}
public static void main(final String[] args) {
final GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsConfiguration gc =
ge.getDefaultScreenDevice().getDefaultConfiguration();
final VolatileImage vi = gc.createCompatibleVolatileImage(200, 200);
final SunGraphics2D sg2d = (SunGraphics2D) vi.createGraphics();
sg2d.constrain(0, 61, 100, 100);
final AffineTransform expected = sg2d.cloneTransform();
sg2d.setTransform(sg2d.getTransform());
final AffineTransform actual = sg2d.cloneTransform();
sg2d.dispose();
vi.flush();
if (!expected.equals(actual)) {
System.out.println("Expected = " + expected);
System.out.println("Actual = " + actual);
throw new RuntimeException("Wrong transform");
}
}
private static void initVI(GraphicsConfiguration gc) {
int res;
if (destVI == null) {
res = VolatileImage.IMAGE_INCOMPATIBLE;
} else {
res = destVI.validate(gc);
}
if (res == VolatileImage.IMAGE_INCOMPATIBLE) {
if (destVI != null) destVI.flush();
destVI = gc.createCompatibleVolatileImage(IMAGE_SIZE, IMAGE_SIZE);
destVI.validate(gc);
res = VolatileImage.IMAGE_RESTORED;
}
if (res == VolatileImage.IMAGE_RESTORED) {
Graphics vig = destVI.getGraphics();
vig.setColor(Color.red);
vig.fillRect(0, 0, destVI.getWidth(), destVI.getHeight());
vig.dispose();
}
}
public void render(Graphics g) {
do {
height = getBounds().height;
width = getBounds().width;
if (vimg == null) {
vimg = createVolatileImage(width, height);
renderOffscreen();
}
int returnCode = vimg.validate(getGraphicsConfiguration());
if (returnCode == VolatileImage.IMAGE_RESTORED) {
renderOffscreen();
} else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {
vimg = getGraphicsConfiguration().
createCompatibleVolatileImage(width, height);
renderOffscreen();
} else if (returnCode == VolatileImage.IMAGE_OK) {
renderOffscreen();
}
g.drawImage(vimg, 0, 0, this);
} while (vimg.contentsLost());
}
private int updateOffscreenImage() {
// Update offscreen image reference
if (offscreenImage == null) offscreenImage = offscreenImageReference.get();
// Offscreen image not available
if (offscreenImage == null) return VolatileImage.IMAGE_INCOMPATIBLE;
// Buffered image is always valid
if (bufferType != BUFFER_VOLATILE_IMAGE) return VolatileImage.IMAGE_OK;
// Determine GraphicsConfiguration context
GraphicsConfiguration gConfiguration = getGraphicsConfiguration();
if (gConfiguration == null) return VolatileImage.IMAGE_INCOMPATIBLE;
// Return Volatile image state
return ((VolatileImage)offscreenImage).validate(gConfiguration);
}
public void render(Graphics g) {
do {
height = getBounds().height;
width = getBounds().width;
if (vimg == null) {
vimg = createVolatileImage(width, height);
renderOffscreen();
}
int returnCode = vimg.validate(getGraphicsConfiguration());
if (returnCode == VolatileImage.IMAGE_RESTORED) {
renderOffscreen();
} else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {
vimg = getGraphicsConfiguration().
createCompatibleVolatileImage(width, height);
renderOffscreen();
} else if (returnCode == VolatileImage.IMAGE_OK) {
renderOffscreen();
}
g.drawImage(vimg, 0, 0, this);
} while (vimg.contentsLost());
}
private static void initVI(GraphicsConfiguration gc) {
int res;
if (destVI == null) {
res = VolatileImage.IMAGE_INCOMPATIBLE;
} else {
res = destVI.validate(gc);
}
if (res == VolatileImage.IMAGE_INCOMPATIBLE) {
if (destVI != null) destVI.flush();
destVI = gc.createCompatibleVolatileImage(IMAGE_SIZE, IMAGE_SIZE);
destVI.validate(gc);
res = VolatileImage.IMAGE_RESTORED;
}
if (res == VolatileImage.IMAGE_RESTORED) {
Graphics vig = destVI.getGraphics();
vig.setColor(Color.red);
vig.fillRect(0, 0, destVI.getWidth(), destVI.getHeight());
vig.dispose();
}
}
public static void main(final String[] args) {
final GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsConfiguration gc =
ge.getDefaultScreenDevice().getDefaultConfiguration();
final VolatileImage vi = gc.createCompatibleVolatileImage(200, 200);
final SunGraphics2D sg2d = (SunGraphics2D) vi.createGraphics();
sg2d.constrain(0, 61, 100, 100);
final AffineTransform expected = sg2d.cloneTransform();
sg2d.setTransform(sg2d.getTransform());
final AffineTransform actual = sg2d.cloneTransform();
sg2d.dispose();
vi.flush();
if (!expected.equals(actual)) {
System.out.println("Expected = " + expected);
System.out.println("Actual = " + actual);
throw new RuntimeException("Wrong transform");
}
}
private void runTest() {
GraphicsConfiguration gc = GraphicsEnvironment.
getLocalGraphicsEnvironment().getDefaultScreenDevice().
getDefaultConfiguration();
if (gc.getColorModel().getPixelSize() < 16) {
System.out.println("8-bit desktop depth found, test passed");
return;
}
VolatileImage vi = gc.createCompatibleVolatileImage(R_WIDTH, R_HEIGHT);
BufferedImage bi = null;
do {
vi.validate(gc);
Graphics2D g = vi.createGraphics();
render(g, vi.getWidth(), vi.getHeight());
bi = vi.getSnapshot();
} while (vi.contentsLost());
checkBI(bi);
System.out.println("Test PASSED.");
}
protected BufferedImage getBufferedImage(Image img) {
if (img instanceof BufferedImage) {
// Otherwise we expect a BufferedImage to behave as a standard BI
return (BufferedImage)img;
} else if (img instanceof ToolkitImage) {
// This can be null if the image isn't loaded yet.
// This is fine as in that case our caller will return
// as it will only draw a fully loaded image
return ((ToolkitImage)img).getBufferedImage();
} else if (img instanceof VolatileImage) {
// VI needs to make a new BI: this is unavoidable but
// I don't expect VI's to be "huge" in any case.
return ((VolatileImage)img).getSnapshot();
} else {
// may be null or may be some non-standard Image which
// shouldn't happen as Image is implemented by the platform
// not by applications
// If you add a new Image implementation to the platform you
// will need to support it here similarly to VI.
return null;
}
}
public static void main(String[] args) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
VolatileImage vi = gc.createCompatibleVolatileImage(16, 16);
vi.validate(gc);
BufferedImage bi =
new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB);
int data[] = ((DataBufferInt)bi.getRaster().getDataBuffer()).getData();
data[0] = 0x0000007f;
data[1] = 0x0000007f;
data[2] = 0xff00007f;
data[3] = 0xff00007f;
Graphics2D g = vi.createGraphics();
g.setComposite(AlphaComposite.SrcOver.derive(0.999f));
g.drawImage(bi, 0, 0, null);
bi = vi.getSnapshot();
if (bi.getRGB(0, 0) != bi.getRGB(1, 1)) {
throw new RuntimeException("Test FAILED: color at 0x0 ="+
Integer.toHexString(bi.getRGB(0, 0))+" differs from 1x1 ="+
Integer.toHexString(bi.getRGB(1,1)));
}
System.out.println("Test PASSED.");
}
/**
* {@inheritDoc}
*
* @see sun.java2d.pipe.hw.AccelGraphicsConfig#createCompatibleVolatileImage
*/
@Override
public VolatileImage
createCompatibleVolatileImage(int width, int height,
int transparency, int type)
{
if (type == FLIP_BACKBUFFER || type == WINDOW || type == UNDEFINED ||
transparency == Transparency.BITMASK)
{
return null;
}
if (type == FBOBJECT) {
if (!isCapPresent(CAPS_EXT_FBOBJECT)) {
return null;
}
} else if (type == PBUFFER) {
boolean isOpaque = transparency == Transparency.OPAQUE;
if (!isOpaque && !isCapPresent(CAPS_STORED_ALPHA)) {
return null;
}
}
SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,
transparency, type);
Surface sd = vi.getDestSurface();
if (!(sd instanceof AccelSurface) ||
((AccelSurface)sd).getType() != type)
{
vi.flush();
vi = null;
}
return vi;
}
/**
* {@inheritDoc}
*
* @see sun.java2d.pipe.hw.AccelGraphicsConfig#createCompatibleVolatileImage
*/
@Override
public VolatileImage
createCompatibleVolatileImage(int width, int height,
int transparency, int type)
{
if (type == FLIP_BACKBUFFER || type == WINDOW || type == UNDEFINED ||
transparency == Transparency.BITMASK)
{
return null;
}
if (type == FBOBJECT) {
if (!isCapPresent(CAPS_EXT_FBOBJECT)) {
return null;
}
} else if (type == PBUFFER) {
boolean isOpaque = transparency == Transparency.OPAQUE;
if (!isOpaque && !isCapPresent(CAPS_STORED_ALPHA)) {
return null;
}
}
SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,
transparency, type);
Surface sd = vi.getDestSurface();
if (!(sd instanceof AccelSurface) ||
((AccelSurface)sd).getType() != type)
{
vi.flush();
vi = null;
}
return vi;
}
static VolatileImage getVImage(GraphicsConfiguration gc,
int w, int h)
{
VolatileImage image =
gc.createCompatibleVolatileImage(w, h, Transparency.OPAQUE);
image.validate(gc);
initImage(gc, image);
return image;
}
public static void main(String[] args) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
VolatileImage vi = gc.createCompatibleVolatileImage(16, 16);
vi.validate(gc);
BufferedImage bi =
new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB);
int data[] = ((DataBufferInt)bi.getRaster().getDataBuffer()).getData();
data[0] = 0x0000007f;
data[1] = 0x0000007f;
data[2] = 0xff00007f;
data[3] = 0xff00007f;
Graphics2D g = vi.createGraphics();
g.setComposite(AlphaComposite.SrcOver.derive(0.999f));
g.drawImage(bi, 0, 0, null);
bi = vi.getSnapshot();
if (bi.getRGB(0, 0) != bi.getRGB(1, 1)) {
throw new RuntimeException("Test FAILED: color at 0x0 ="+
Integer.toHexString(bi.getRGB(0, 0))+" differs from 1x1 ="+
Integer.toHexString(bi.getRGB(1,1)));
}
System.out.println("Test PASSED.");
}
/**
* Creates a VolatileImage that essentially wraps the target Component's
* backbuffer (the provided backbuffer handle is essentially ignored).
*/
@Override
public VolatileImage createBackBufferImage(Component target,
long backBuffer)
{
return new SunVolatileImage(target,
target.getWidth(), target.getHeight(),
Boolean.TRUE);
}
/**
* Returns a scale factor of the image. This is utility method, which
* fetches information from the SurfaceData of the image.
*
* @see SurfaceData#getDefaultScale
*/
public static int getImageScale(final Image img) {
if (!(img instanceof VolatileImage)) {
return 1;
}
final SurfaceManager sm = getManager(img);
return sm.getPrimarySurfaceData().getDefaultScale();
}
/**
* Creates a VolatileImage that essentially wraps the target Component's
* backbuffer (the provided backbuffer handle is essentially ignored).
*/
@Override
public VolatileImage createBackBufferImage(Component target,
long backBuffer)
{
return new SunVolatileImage(target,
target.getWidth(), target.getHeight(),
Boolean.TRUE);
}
/**
* {@inheritDoc}
*
* @see sun.java2d.pipe.hw.AccelGraphicsConfig#createCompatibleVolatileImage
*/
@Override
public VolatileImage
createCompatibleVolatileImage(int width, int height,
int transparency, int type)
{
if (type == FLIP_BACKBUFFER || type == WINDOW || type == UNDEFINED ||
transparency == Transparency.BITMASK)
{
return null;
}
boolean isOpaque = transparency == Transparency.OPAQUE;
if (type == RT_TEXTURE) {
int cap = isOpaque ? CAPS_RT_TEXTURE_OPAQUE : CAPS_RT_TEXTURE_ALPHA;
if (!device.isCapPresent(cap)) {
return null;
}
} else if (type == RT_PLAIN) {
if (!isOpaque && !device.isCapPresent(CAPS_RT_PLAIN_ALPHA)) {
return null;
}
}
SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,
transparency, type);
Surface sd = vi.getDestSurface();
if (!(sd instanceof AccelSurface) ||
((AccelSurface)sd).getType() != type)
{
vi.flush();
vi = null;
}
return vi;
}
/**
* {@inheritDoc}
*
* @see sun.java2d.pipe.hw.AccelGraphicsConfig#createCompatibleVolatileImage
*/
@Override
public VolatileImage
createCompatibleVolatileImage(int width, int height,
int transparency, int type)
{
if (type == FLIP_BACKBUFFER || type == WINDOW || type == UNDEFINED ||
transparency == Transparency.BITMASK)
{
return null;
}
if (type == FBOBJECT) {
if (!isCapPresent(CAPS_EXT_FBOBJECT)) {
return null;
}
} else if (type == PBUFFER) {
boolean isOpaque = transparency == Transparency.OPAQUE;
if (!isOpaque && !isCapPresent(CAPS_STORED_ALPHA)) {
return null;
}
}
SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,
transparency, type);
Surface sd = vi.getDestSurface();
if (!(sd instanceof AccelSurface) ||
((AccelSurface)sd).getType() != type)
{
vi.flush();
vi = null;
}
return vi;
}
/**
* {@inheritDoc}
*
* @see sun.java2d.pipe.hw.AccelGraphicsConfig#createCompatibleVolatileImage
*/
@Override
public VolatileImage
createCompatibleVolatileImage(int width, int height,
int transparency, int type)
{
if (type == FLIP_BACKBUFFER || type == WINDOW || type == UNDEFINED ||
transparency == Transparency.BITMASK)
{
return null;
}
if (type == FBOBJECT) {
if (!isCapPresent(CAPS_EXT_FBOBJECT)) {
return null;
}
} else if (type == PBUFFER) {
boolean isOpaque = transparency == Transparency.OPAQUE;
if (!isOpaque && !isCapPresent(CAPS_STORED_ALPHA)) {
return null;
}
}
SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,
transparency, type);
Surface sd = vi.getDestSurface();
if (!(sd instanceof AccelSurface) ||
((AccelSurface)sd).getType() != type)
{
vi.flush();
vi = null;
}
return vi;
}
@Override
public void flip(int x1, int y1, int x2, int y2,
BufferCapabilities.FlipContents flipAction)
{
VolatileImage backBuffer = this.backBuffer;
if (backBuffer == null) {
throw new IllegalStateException("Buffers have not been created");
}
Win32GraphicsConfig gc =
(Win32GraphicsConfig)getGraphicsConfiguration();
gc.flip(this, (Component)target, backBuffer, x1, y1, x2, y2, flipAction);
}
public static void main(final String[] args) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
AffineTransform at;
for (final int size : SIZES) {
for (final int scale : SCALES) {
final int sw = size * scale;
at = AffineTransform.getScaleInstance(sw, sw);
for (Shape clip : SHAPES) {
clip = at.createTransformedShape(clip);
for (Shape to : SHAPES) {
to = at.createTransformedShape(to);
// Prepare test images
VolatileImage vi = getVolatileImage(gc, size);
BufferedImage bi = getBufferedImage(sw);
// Prepare gold images
BufferedImage goldvi = getCompatibleImage(gc, size);
BufferedImage goldbi = getBufferedImage(sw);
draw(clip, to, vi, bi, scale);
draw(clip, to, goldvi, goldbi, scale);
validate(bi, goldbi);
}
}
}
}
}
/**
* Return a volatile offscreen buffer that should be used as a
* double buffer with the specified component <code>c</code>.
* The image returned will be an instance of VolatileImage, or null
* if a VolatileImage object could not be instantiated.
* This buffer might be smaller than <code>(proposedWidth,proposedHeight)</code>.
* This happens when the maximum double buffer size has been set for this
* repaint manager.
*
* @see java.awt.image.VolatileImage
* @since 1.4
*/
public Image getVolatileOffscreenBuffer(Component c,
int proposedWidth,int proposedHeight) {
RepaintManager delegate = getDelegate(c);
if (delegate != null) {
return delegate.getVolatileOffscreenBuffer(c, proposedWidth,
proposedHeight);
}
// If the window is non-opaque, it's double-buffered at peer's level
Window w = (c instanceof Window) ? (Window)c : SwingUtilities.getWindowAncestor(c);
if (!w.isOpaque()) {
Toolkit tk = Toolkit.getDefaultToolkit();
if ((tk instanceof SunToolkit) && (((SunToolkit)tk).needUpdateWindow())) {
return null;
}
}
GraphicsConfiguration config = c.getGraphicsConfiguration();
if (config == null) {
config = GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().getDefaultConfiguration();
}
Dimension maxSize = getDoubleBufferMaximumSize();
int width = proposedWidth < 1 ? 1 :
(proposedWidth > maxSize.width? maxSize.width : proposedWidth);
int height = proposedHeight < 1 ? 1 :
(proposedHeight > maxSize.height? maxSize.height : proposedHeight);
VolatileImage image = volatileMap.get(config);
if (image == null || image.getWidth() < width ||
image.getHeight() < height) {
if (image != null) {
image.flush();
}
image = config.createCompatibleVolatileImage(width, height,
volatileBufferType);
volatileMap.put(config, image);
}
return image;
}
private static VolatileImage getVolatileImage(GraphicsConfiguration gc,
int size) {
VolatileImage vi = gc.createCompatibleVolatileImage(size, size);
Graphics2D g2d = vi.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, size, size);
return vi;
}
@Override
public VolatileImage createCompatibleVolatileImage(int width, int height,
int transparency,
int type) {
if (type == FLIP_BACKBUFFER || type == WINDOW || type == UNDEFINED ||
transparency == Transparency.BITMASK)
{
return null;
}
if (type == FBOBJECT) {
if (!isCapPresent(CAPS_EXT_FBOBJECT)) {
return null;
}
} else if (type == PBUFFER) {
boolean isOpaque = transparency == Transparency.OPAQUE;
if (!isOpaque && !isCapPresent(CAPS_STORED_ALPHA)) {
return null;
}
}
SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,
transparency, type);
Surface sd = vi.getDestSurface();
if (!(sd instanceof AccelSurface) ||
((AccelSurface)sd).getType() != type)
{
vi.flush();
vi = null;
}
return vi;
}
private static void renderToVolatileImage(VolatileImage dst) {
Graphics2D g = dst.createGraphics();
do {
System.out.println("Render to volatile image..");
try {
MyComp.renderTest(g, dst.getHeight(), dst.getHeight());
} catch (Throwable e) {
throw new RuntimeException("Test FAILED.", e);
}
} while (dst.contentsLost());
System.out.println("Done.");
}
@Override
public VolatileImage createCompatibleVolatileImage(int width, int height,
int transparency,
int type) {
if (type == FLIP_BACKBUFFER || type == WINDOW || type == UNDEFINED ||
transparency == Transparency.BITMASK)
{
return null;
}
if (type == FBOBJECT) {
if (!isCapPresent(CAPS_EXT_FBOBJECT)) {
return null;
}
} else if (type == PBUFFER) {
boolean isOpaque = transparency == Transparency.OPAQUE;
if (!isOpaque && !isCapPresent(CAPS_STORED_ALPHA)) {
return null;
}
}
SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,
transparency, type);
Surface sd = vi.getDestSurface();
if (!(sd instanceof AccelSurface) ||
((AccelSurface)sd).getType() != type)
{
vi.flush();
vi = null;
}
return vi;
}