下面列出了java.awt.image.VolatileImage#IMAGE_OK 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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());
}
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());
}
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());
}
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 test(final BufferedImage bi) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
VolatileImage vi = gc.createCompatibleVolatileImage(500, 200,
TRANSLUCENT);
BufferedImage gold = gc.createCompatibleImage(500, 200, TRANSLUCENT);
// draw to compatible Image
draw(bi, gold);
// draw to volatile image
int attempt = 0;
BufferedImage snapshot;
while (true) {
if (++attempt > 10) {
throw new RuntimeException("Too many attempts: " + attempt);
}
vi.validate(gc);
if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(bi, vi);
snapshot = vi.getSnapshot();
if (vi.contentsLost()) {
continue;
}
break;
}
// validate images
for (int x = 0; x < gold.getWidth(); ++x) {
for (int y = 0; y < gold.getHeight(); ++y) {
if (gold.getRGB(x, y) != snapshot.getRGB(x, y)) {
ImageIO.write(gold, "png", new File("gold.png"));
ImageIO.write(snapshot, "png", new File("bi.png"));
throw new RuntimeException("Test failed.");
}
}
}
}
private static void test(final BufferedImage bi) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
VolatileImage vi = gc.createCompatibleVolatileImage(500, 200,
TRANSLUCENT);
BufferedImage gold = gc.createCompatibleImage(500, 200, TRANSLUCENT);
// draw to compatible Image
draw(bi, gold);
// draw to volatile image
int attempt = 0;
BufferedImage snapshot;
while (true) {
if (++attempt > 10) {
throw new RuntimeException("Too many attempts: " + attempt);
}
vi.validate(gc);
if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(bi, vi);
snapshot = vi.getSnapshot();
if (vi.contentsLost()) {
continue;
}
break;
}
// validate images
for (int x = 0; x < gold.getWidth(); ++x) {
for (int y = 0; y < gold.getHeight(); ++y) {
if (gold.getRGB(x, y) != snapshot.getRGB(x, y)) {
ImageIO.write(gold, "png", new File("gold.png"));
ImageIO.write(snapshot, "png", new File("bi.png"));
throw new RuntimeException("Test failed.");
}
}
}
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
VolatileImage ri = this.renderImage;
if (ri != null) {
calcRect();
if (ri.validate(View.getDefaultConfiguration()) != VolatileImage.IMAGE_OK) {
ri = View.createRenderImage(getWidth(), getHeight(), Transparency.TRANSLUCENT);
render();
}
if (ri != null) {
g2d.drawImage(ri, 0, 0, null);
}
}
g2d.setColor(Color.red);
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(0);
df.setGroupingUsed(false);
float frameLoss = 100 - (getFpsIs() / fpsShouldBe * 100);
if (Configuration._debugMode.get()) {
g2d.drawString("frameLoss:" + df.format(frameLoss) + "%", 20, 20);
}
}
private static void test(final BufferedImage bi) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
VolatileImage vi = gc.createCompatibleVolatileImage(500, 200,
TRANSLUCENT);
BufferedImage gold = gc.createCompatibleImage(500, 200, TRANSLUCENT);
// draw to compatible Image
draw(bi, gold);
// draw to volatile image
int attempt = 0;
BufferedImage snapshot;
while (true) {
if (++attempt > 10) {
throw new RuntimeException("Too many attempts: " + attempt);
}
vi.validate(gc);
if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(bi, vi);
snapshot = vi.getSnapshot();
if (vi.contentsLost()) {
continue;
}
break;
}
// validate images
for (int x = 0; x < gold.getWidth(); ++x) {
for (int y = 0; y < gold.getHeight(); ++y) {
if (gold.getRGB(x, y) != snapshot.getRGB(x, y)) {
ImageIO.write(gold, "png", new File("gold.png"));
ImageIO.write(snapshot, "png", new File("bi.png"));
throw new RuntimeException("Test failed.");
}
}
}
}
private static void test(final BufferedImage bi) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
VolatileImage vi = gc.createCompatibleVolatileImage(500, 200,
TRANSLUCENT);
BufferedImage gold = gc.createCompatibleImage(500, 200, TRANSLUCENT);
// draw to compatible Image
draw(bi, gold);
// draw to volatile image
int attempt = 0;
BufferedImage snapshot;
while (true) {
if (++attempt > 10) {
throw new RuntimeException("Too many attempts: " + attempt);
}
vi.validate(gc);
if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(bi, vi);
snapshot = vi.getSnapshot();
if (vi.contentsLost()) {
continue;
}
break;
}
// validate images
for (int x = 0; x < gold.getWidth(); ++x) {
for (int y = 0; y < gold.getHeight(); ++y) {
if (gold.getRGB(x, y) != snapshot.getRGB(x, y)) {
ImageIO.write(gold, "png", new File("gold.png"));
ImageIO.write(snapshot, "png", new File("bi.png"));
throw new RuntimeException("Test failed.");
}
}
}
}
private static void test(final BufferedImage bi) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
VolatileImage vi = gc.createCompatibleVolatileImage(500, 200,
TRANSLUCENT);
BufferedImage gold = gc.createCompatibleImage(500, 200, TRANSLUCENT);
// draw to compatible Image
draw(bi, gold);
// draw to volatile image
int attempt = 0;
BufferedImage snapshot;
while (true) {
if (++attempt > 10) {
throw new RuntimeException("Too many attempts: " + attempt);
}
vi.validate(gc);
if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(bi, vi);
snapshot = vi.getSnapshot();
if (vi.contentsLost()) {
continue;
}
break;
}
// validate images
for (int x = 0; x < gold.getWidth(); ++x) {
for (int y = 0; y < gold.getHeight(); ++y) {
if (gold.getRGB(x, y) != snapshot.getRGB(x, y)) {
ImageIO.write(gold, "png", new File("gold.png"));
ImageIO.write(snapshot, "png", new File("bi.png"));
throw new RuntimeException("Test failed.");
}
}
}
}
public static void main(final String[] args) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
AffineTransform at;
for (int size : SIZES) {
at = AffineTransform.getScaleInstance(size, size);
for (Shape clip : SHAPES) {
clip = at.createTransformedShape(clip);
for (Shape to : SHAPES) {
to = at.createTransformedShape(to);
// Prepare test images
BufferedImage snapshot;
VolatileImage source = getVolatileImage(gc, size);
VolatileImage target = getVolatileImage(gc, size);
int attempt = 0;
while (true) {
if (++attempt > 10) {
throw new RuntimeException("Too many attempts: " + attempt);
}
// Prepare source images
source.validate(gc);
Graphics2D g2d = source.createGraphics();
g2d.setColor(Color.RED);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (source.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
// Prepare target images
target.validate(gc);
g2d = target.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (target.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(clip, to, source, target);
snapshot = target.getSnapshot();
if (source.contentsLost() || target.contentsLost()) {
continue;
}
break;
}
// Prepare gold images
BufferedImage goldS = getSourceGold(gc, size);
BufferedImage goldT = getTargetGold(gc, size);
draw(clip, to, goldS, goldT);
validate(snapshot, goldT);
source.flush();
target.flush();
}
}
}
}
public static void main(final String[] args) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
AffineTransform at;
for (int size : SIZES) {
at = AffineTransform.getScaleInstance(size, size);
for (Shape clip : SHAPES) {
clip = at.createTransformedShape(clip);
for (Shape to : SHAPES) {
to = at.createTransformedShape(to);
// Prepare test images
BufferedImage snapshot;
BufferedImage bi = getBufferedImage(size);
VolatileImage vi = getVolatileImage(gc, size);
while (true) {
vi.validate(gc);
Graphics2D g2d = vi.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(clip, to, bi, vi);
snapshot = vi.getSnapshot();
if (vi.contentsLost()) {
continue;
}
break;
}
// Prepare gold images
BufferedImage goldvi = getCompatibleImage(gc, size);
BufferedImage goldbi = getBufferedImage(size);
draw(clip, to, goldbi, goldvi);
validate(snapshot, goldvi);
vi.flush();
}
}
}
}
public static void main(final String[] args) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
AffineTransform at;
for (int size : SIZES) {
at = AffineTransform.getScaleInstance(size, size);
for (Shape clip : SHAPES) {
clip = at.createTransformedShape(clip);
for (Shape to : SHAPES) {
to = at.createTransformedShape(to);
// Prepare test images
BufferedImage snapshot;
VolatileImage source = getVolatileImage(gc, size);
VolatileImage target = getVolatileImage(gc, size);
int attempt = 0;
while (true) {
if (++attempt > 10) {
throw new RuntimeException("Too many attempts: " + attempt);
}
// Prepare source images
source.validate(gc);
Graphics2D g2d = source.createGraphics();
g2d.setColor(Color.RED);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (source.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
// Prepare target images
target.validate(gc);
g2d = target.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (target.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(clip, to, source, target);
snapshot = target.getSnapshot();
if (source.contentsLost() || target.contentsLost()) {
continue;
}
break;
}
// Prepare gold images
BufferedImage goldS = getSourceGold(gc, size);
BufferedImage goldT = getTargetGold(gc, size);
draw(clip, to, goldS, goldT);
validate(snapshot, goldT);
source.flush();
target.flush();
}
}
}
}
/**
* Get the image ready for rendering. This method is called to make
* sure that the accelerated SurfaceData exists and is
* ready to be used. Users call this method prior to any set of
* rendering to or from the image, to make sure the image is ready
* and compatible with the given GraphicsConfiguration.
*
* The image may not be "ready" if either we had problems creating
* it in the first place (e.g., there was no space in vram) or if
* the surface became lost (e.g., some other app or the OS caused
* vram surfaces to be removed).
*
* Note that we want to return RESTORED in any situation where the
* SurfaceData is different than it was last time. So whether it's
* software or hardware, if we have a different SurfaceData object,
* then the contents have been altered and we must reflect that
* change to the user.
*/
public int validate(GraphicsConfiguration gc) {
int returnCode = VolatileImage.IMAGE_OK;
boolean lostSurfaceTmp = lostSurface;
lostSurface = false;
if (isAccelerationEnabled()) {
if (!isConfigValid(gc)) {
// If we're asked to render to a different device than the
// one we were created under, return INCOMPATIBLE error code.
// Note that a null gc simply ignores the incompatibility
// issue
returnCode = VolatileImage.IMAGE_INCOMPATIBLE;
} else if (sdAccel == null) {
// We either had problems creating the surface or the display
// mode changed and we nullified the old one. Try it again.
sdAccel = initAcceleratedSurface();
if (sdAccel != null) {
// set the current SurfaceData to accelerated version
sdCurrent = sdAccel;
// we don't need the system memory surface anymore, so
// let's release it now (it can always be restored later)
sdBackup = null;
returnCode = VolatileImage.IMAGE_RESTORED;
} else {
sdCurrent = getBackupSurface();
}
} else if (sdAccel.isSurfaceLost()) {
try {
restoreAcceleratedSurface();
// set the current SurfaceData to accelerated version
sdCurrent = sdAccel;
// restoration successful: accel surface no longer lost
sdAccel.setSurfaceLost(false);
// we don't need the system memory surface anymore, so
// let's release it now (it can always be restored later)
sdBackup = null;
returnCode = VolatileImage.IMAGE_RESTORED;
} catch (sun.java2d.InvalidPipeException e) {
// Set the current SurfaceData to software version so that
// drawing can continue. Note that we still have
// the lostAccelSurface flag set so that we will continue
// to attempt to restore the accelerated surface.
sdCurrent = getBackupSurface();
}
} else if (lostSurfaceTmp) {
// Something else triggered this loss/restoration. Could
// be a palette change that didn't require a SurfaceData
// recreation but merely a re-rendering of the pixels.
returnCode = VolatileImage.IMAGE_RESTORED;
}
} else if (sdAccel != null) {
// if the "acceleration enabled" state changed to disabled,
// switch to software surface
sdCurrent = getBackupSurface();
sdAccel = null;
returnCode = VolatileImage.IMAGE_RESTORED;
} else if (lostSurfaceTmp) {
// A software surface has been restored. This could be due to
// display mode change on a non-accelerated volatile image.
returnCode = VolatileImage.IMAGE_RESTORED;
}
if ((returnCode != VolatileImage.IMAGE_INCOMPATIBLE) &&
(sdCurrent != sdPrevious))
{
// contents have changed - return RESTORED to user
sdPrevious = sdCurrent;
returnCode = VolatileImage.IMAGE_RESTORED;
}
if (returnCode == VolatileImage.IMAGE_RESTORED) {
// clear the current surface with the background color,
// only if the surface has been restored
initContents();
}
return returnCode;
}
public static void main(final String[] args) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
AffineTransform at;
for (int size : SIZES) {
at = AffineTransform.getScaleInstance(size, size);
for (Shape clip : SHAPES) {
clip = at.createTransformedShape(clip);
for (Shape to : SHAPES) {
to = at.createTransformedShape(to);
// Prepare test images
BufferedImage snapshot;
VolatileImage source = getVolatileImage(gc, size);
VolatileImage target = getVolatileImage(gc, size);
int attempt = 0;
while (true) {
if (++attempt > 10) {
throw new RuntimeException("Too many attempts: " + attempt);
}
// Prepare source images
source.validate(gc);
Graphics2D g2d = source.createGraphics();
g2d.setColor(Color.RED);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (source.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
// Prepare target images
target.validate(gc);
g2d = target.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (target.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(clip, to, source, target);
snapshot = target.getSnapshot();
if (source.contentsLost() || target.contentsLost()) {
continue;
}
break;
}
// Prepare gold images
BufferedImage goldS = getSourceGold(gc, size);
BufferedImage goldT = getTargetGold(gc, size);
draw(clip, to, goldS, goldT);
validate(snapshot, goldT);
source.flush();
target.flush();
}
}
}
}
public static void main(final String[] args) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
AffineTransform at;
for (int size : SIZES) {
at = AffineTransform.getScaleInstance(size, size);
for (Shape clip : SHAPES) {
clip = at.createTransformedShape(clip);
for (Shape to : SHAPES) {
to = at.createTransformedShape(to);
// Prepare test images
BufferedImage snapshot;
VolatileImage source = getVolatileImage(gc, size);
VolatileImage target = getVolatileImage(gc, size);
int attempt = 0;
while (true) {
if (++attempt > 10) {
throw new RuntimeException("Too many attempts: " + attempt);
}
// Prepare source images
source.validate(gc);
Graphics2D g2d = source.createGraphics();
g2d.setColor(Color.RED);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (source.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
// Prepare target images
target.validate(gc);
g2d = target.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (target.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(clip, to, source, target);
snapshot = target.getSnapshot();
if (source.contentsLost() || target.contentsLost()) {
continue;
}
break;
}
// Prepare gold images
BufferedImage goldS = getSourceGold(gc, size);
BufferedImage goldT = getTargetGold(gc, size);
draw(clip, to, goldS, goldT);
validate(snapshot, goldT);
source.flush();
target.flush();
}
}
}
}
/**
* Get the image ready for rendering. This method is called to make
* sure that the accelerated SurfaceData exists and is
* ready to be used. Users call this method prior to any set of
* rendering to or from the image, to make sure the image is ready
* and compatible with the given GraphicsConfiguration.
*
* The image may not be "ready" if either we had problems creating
* it in the first place (e.g., there was no space in vram) or if
* the surface became lost (e.g., some other app or the OS caused
* vram surfaces to be removed).
*
* Note that we want to return RESTORED in any situation where the
* SurfaceData is different than it was last time. So whether it's
* software or hardware, if we have a different SurfaceData object,
* then the contents have been altered and we must reflect that
* change to the user.
*/
public int validate(GraphicsConfiguration gc) {
int returnCode = VolatileImage.IMAGE_OK;
boolean lostSurfaceTmp = lostSurface;
lostSurface = false;
if (isAccelerationEnabled()) {
if (!isConfigValid(gc)) {
// If we're asked to render to a different device than the
// one we were created under, return INCOMPATIBLE error code.
// Note that a null gc simply ignores the incompatibility
// issue
returnCode = VolatileImage.IMAGE_INCOMPATIBLE;
} else if (sdAccel == null) {
// We either had problems creating the surface or the display
// mode changed and we nullified the old one. Try it again.
sdAccel = initAcceleratedSurface();
if (sdAccel != null) {
// set the current SurfaceData to accelerated version
sdCurrent = sdAccel;
// we don't need the system memory surface anymore, so
// let's release it now (it can always be restored later)
sdBackup = null;
returnCode = VolatileImage.IMAGE_RESTORED;
} else {
sdCurrent = getBackupSurface();
}
} else if (sdAccel.isSurfaceLost()) {
try {
restoreAcceleratedSurface();
// set the current SurfaceData to accelerated version
sdCurrent = sdAccel;
// restoration successful: accel surface no longer lost
sdAccel.setSurfaceLost(false);
// we don't need the system memory surface anymore, so
// let's release it now (it can always be restored later)
sdBackup = null;
returnCode = VolatileImage.IMAGE_RESTORED;
} catch (sun.java2d.InvalidPipeException e) {
// Set the current SurfaceData to software version so that
// drawing can continue. Note that we still have
// the lostAccelSurface flag set so that we will continue
// to attempt to restore the accelerated surface.
sdCurrent = getBackupSurface();
}
} else if (lostSurfaceTmp) {
// Something else triggered this loss/restoration. Could
// be a palette change that didn't require a SurfaceData
// recreation but merely a re-rendering of the pixels.
returnCode = VolatileImage.IMAGE_RESTORED;
}
} else if (sdAccel != null) {
// if the "acceleration enabled" state changed to disabled,
// switch to software surface
sdCurrent = getBackupSurface();
sdAccel = null;
returnCode = VolatileImage.IMAGE_RESTORED;
}
if ((returnCode != VolatileImage.IMAGE_INCOMPATIBLE) &&
(sdCurrent != sdPrevious))
{
// contents have changed - return RESTORED to user
sdPrevious = sdCurrent;
returnCode = VolatileImage.IMAGE_RESTORED;
}
if (returnCode == VolatileImage.IMAGE_RESTORED) {
// clear the current surface with the background color,
// only if the surface has been restored
initContents();
}
return returnCode;
}
public static void main(final String[] args) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
AffineTransform at;
for (int size : SIZES) {
at = AffineTransform.getScaleInstance(size, size);
for (Shape clip : SHAPES) {
clip = at.createTransformedShape(clip);
for (Shape to : SHAPES) {
to = at.createTransformedShape(to);
// Prepare test images
BufferedImage snapshot;
BufferedImage bi = getBufferedImage(size);
VolatileImage vi = getVolatileImage(gc, size);
while (true) {
vi.validate(gc);
Graphics2D g2d = vi.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(clip, to, bi, vi);
snapshot = vi.getSnapshot();
if (vi.contentsLost()) {
continue;
}
break;
}
// Prepare gold images
BufferedImage goldvi = getCompatibleImage(gc, size);
BufferedImage goldbi = getBufferedImage(size);
draw(clip, to, goldbi, goldvi);
validate(snapshot, goldvi);
vi.flush();
}
}
}
}
public static void main(final String[] args) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
AffineTransform at;
for (int size : SIZES) {
at = AffineTransform.getScaleInstance(size, size);
for (Shape clip : SHAPES) {
clip = at.createTransformedShape(clip);
for (Shape to : SHAPES) {
to = at.createTransformedShape(to);
// Prepare test images
BufferedImage snapshot;
BufferedImage bi = getBufferedImage(size);
VolatileImage vi = getVolatileImage(gc, size);
while (true) {
vi.validate(gc);
Graphics2D g2d = vi.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(clip, to, bi, vi);
snapshot = vi.getSnapshot();
if (vi.contentsLost()) {
continue;
}
break;
}
// Prepare gold images
BufferedImage goldvi = getCompatibleImage(gc, size);
BufferedImage goldbi = getBufferedImage(size);
draw(clip, to, goldbi, goldvi);
validate(snapshot, goldvi);
vi.flush();
}
}
}
}
public static void main(final String[] args) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
AffineTransform at;
for (int size : SIZES) {
at = AffineTransform.getScaleInstance(size, size);
for (Shape clip : SHAPES) {
clip = at.createTransformedShape(clip);
for (Shape to : SHAPES) {
to = at.createTransformedShape(to);
// Prepare test images
BufferedImage snapshot;
VolatileImage source = getVolatileImage(gc, size);
VolatileImage target = getVolatileImage(gc, size);
int attempt = 0;
while (true) {
if (++attempt > 10) {
throw new RuntimeException("Too many attempts: " + attempt);
}
// Prepare source images
source.validate(gc);
Graphics2D g2d = source.createGraphics();
g2d.setColor(Color.RED);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (source.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
// Prepare target images
target.validate(gc);
g2d = target.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, size, size);
g2d.dispose();
if (target.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(clip, to, source, target);
snapshot = target.getSnapshot();
if (source.contentsLost() || target.contentsLost()) {
continue;
}
break;
}
// Prepare gold images
BufferedImage goldS = getSourceGold(gc, size);
BufferedImage goldT = getTargetGold(gc, size);
draw(clip, to, goldS, goldT);
validate(snapshot, goldT);
source.flush();
target.flush();
}
}
}
}