下面列出了java.awt.image.VolatileImage#validate() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initImages(int w, int h) {
if (images == null) {
images = new Image[6];
GraphicsConfiguration gc = getGraphicsConfiguration();
for (int i = OPAQUE; i <= TRANSLUCENT; i++) {
VolatileImage vi =
gc.createCompatibleVolatileImage(w,h/images.length,i);
images[i-1] = vi;
vi.validate(gc);
String s = "LCD AA Text rendered to " + tr[i - 1] + " HW destination";
render(vi, i, s);
s = "LCD AA Text rendered to " + tr[i - 1] + " SW destination";
images[i-1+3] = gc.createCompatibleImage(w, h/images.length, i);
render(images[i-1+3], i, s);
}
}
}
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.");
}
private static void renderText(Frame frame, Font f1) {
VolatileImage vi = frame.createVolatileImage(256, 32);
vi.validate(frame.getGraphicsConfiguration());
Graphics2D g = vi.createGraphics();
g.setFont(f1);
g.drawString(text, 0, vi.getHeight()/2);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.drawString(text, 0, vi.getHeight()/2);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
g.drawString(text, 0, vi.getHeight()/2);
Toolkit.getDefaultToolkit().sync();
}
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(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.");
}
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.");
}
private static void renderOffscreen(VolatileImage vImg,
GraphicsConfiguration conf,
double scaleX,
double scaleY)
{
int attempts = 0;
do {
if (attempts > 10) {
throw new RuntimeException("Too many attempts!");
}
if (vImg.validate(conf) == VolatileImage.IMAGE_INCOMPATIBLE) {
// old vImg doesn't work with new GraphicsConfig; re-create it
vImg = createVolatileImage(conf);
}
Graphics2D g = vImg.createGraphics();
//
// miscellaneous rendering commands...
//
g.setColor(BACKGROUND_COLOR);
g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
g.scale(scaleX, scaleY);
g.setColor(FILL_COLOR);
g.fillRect(X, Y, W, H);
for (int i = 0; i < N; i++) {
g.copyArea(X + i * DX, Y + i * DY, W, H, DX, DY);
}
g.dispose();
attempts++;
} while (vImg.contentsLost());
}
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;
}
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(final String[] args) {
BufferedImage img = new BufferedImage(200, 40, TYPE_INT_ARGB);
draw(img);
validate(img);
if (GraphicsEnvironment.isHeadless()) {
return;
}
GraphicsConfiguration gc =
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration();
VolatileImage vi = gc.createCompatibleVolatileImage(200, 40);
BufferedImage snapshot;
int attempt = 0;
while (true) {
if (++attempt > 10) {
throw new RuntimeException("Too many attempts: " + attempt);
}
vi.validate(gc);
draw(vi);
snapshot = vi.getSnapshot();
if (!vi.contentsLost()) {
break;
}
}
validate(snapshot);
}
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 testGC(GraphicsConfiguration gc) {
if (!(gc instanceof AccelGraphicsConfig)) {
System.out.println("Test passed: no hw accelerated configs found.");
return;
}
System.out.println("AccelGraphicsConfig exists, testing.");
AccelGraphicsConfig agc = (AccelGraphicsConfig) gc;
printAGC(agc);
testContext(agc);
VolatileImage vi = gc.createCompatibleVolatileImage(10, 10);
vi.validate(gc);
if (vi instanceof DestSurfaceProvider) {
System.out.println("Passed: VI is DestSurfaceProvider");
Surface s = ((DestSurfaceProvider) vi).getDestSurface();
if (s instanceof AccelSurface) {
System.out.println("Passed: Obtained Accel Surface");
printSurface((AccelSurface) s);
}
Graphics g = vi.getGraphics();
if (g instanceof DestSurfaceProvider) {
System.out.println("Passed: VI graphics is " +
"DestSurfaceProvider");
printSurface(((DestSurfaceProvider) g).getDestSurface());
}
} else {
System.out.println("VI is not DestSurfaceProvider");
}
testVICreation(agc, CAPS_RT_TEXTURE_ALPHA, TRANSLUCENT, RT_TEXTURE);
testVICreation(agc, CAPS_RT_TEXTURE_OPAQUE, OPAQUE, RT_TEXTURE);
testVICreation(agc, CAPS_RT_PLAIN_ALPHA, TRANSLUCENT, RT_PLAIN);
testVICreation(agc, agc.getContextCapabilities().getCaps(), OPAQUE,
TEXTURE);
testForNPEDuringCreation(agc);
}
private static void renderText(Frame frame, Font f1) {
VolatileImage vi = frame.createVolatileImage(256, 32);
vi.validate(frame.getGraphicsConfiguration());
Graphics2D g = vi.createGraphics();
g.setFont(f1);
g.drawString(text, 0, vi.getHeight()/2);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.drawString(text, 0, vi.getHeight()/2);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
g.drawString(text, 0, vi.getHeight()/2);
Toolkit.getDefaultToolkit().sync();
}
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.");
}
private void test() {
GraphicsConfiguration gc =
GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().getDefaultConfiguration();
if (gc.getColorModel().getPixelSize() < 16) {
System.out.println("<16 bit depth detected, test passed");
return;
}
VolatileImage vi =
gc.createCompatibleVolatileImage(250, 4*120, Transparency.OPAQUE);
BufferedImage res;
do {
vi.validate(gc);
Graphics2D g2d = vi.createGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, vi.getWidth(), vi.getHeight());
render(g2d);
res = vi.getSnapshot();
} while (vi.contentsLost());
for (int y = 0; y < bi.getHeight(); y++) {
for (int x = 0; x < bi.getWidth(); x++) {
if (res.getRGB(x, y) == Color.black.getRGB()) {
System.err.printf("Test FAILED: found black at %d,%d\n",
x, y);
try {
String fileName = "AccelPaintsTest.png";
ImageIO.write(res, "png", new File(fileName));
System.err.println("Dumped rendering to " + fileName);
} catch (IOException e) {}
throw new RuntimeException("Test FAILED: found black");
}
}
}
}
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();
}
}
}
}
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(String[] args) {
GraphicsConfiguration gc =
GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().getDefaultConfiguration();
if (gc.getColorModel().getPixelSize() <= 8) {
System.out.println("8-bit color model, test considered passed");
return;
}
/*
* Set up images:
* 1.) VolatileImge for rendering to,
* 2.) BufferedImage for reading back the contents of the VI
* 3.) The image triggering the problem
*/
VolatileImage vImg = null;
BufferedImage readBackBImg;
// create a BITMASK ICM such that the transparent color is
// tr. black (and it's the first in the color map so a buffered image
// created with this ICM is transparent
byte r[] = { 0x00, (byte)0xff};
byte g[] = { 0x00, (byte)0xff};
byte b[] = { 0x00, (byte)0xff};
IndexColorModel icm = new IndexColorModel(8, 2, r, g, b, 0);
WritableRaster wr = icm.createCompatibleWritableRaster(25, 25);
BufferedImage tImg = new BufferedImage(icm, wr, false, null);
do {
if (vImg == null ||
vImg.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE)
{
vImg = gc.createCompatibleVolatileImage(tImg.getWidth(),
tImg.getHeight());
}
Graphics viG = vImg.getGraphics();
viG.setColor(Color.red);
viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());
viG.drawImage(tImg, 0, 0, Color.green, null);
viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());
viG.drawImage(tImg, 0, 0, Color.white, null);
readBackBImg = vImg.getSnapshot();
} while (vImg.contentsLost());
for (int x = 0; x < readBackBImg.getWidth(); x++) {
for (int y = 0; y < readBackBImg.getHeight(); y++) {
int currPixel = readBackBImg.getRGB(x, y);
if (currPixel != Color.white.getRGB()) {
String fileName = "DrawImageBgTest.png";
try {
ImageIO.write(readBackBImg, "png", new File(fileName));
System.err.println("Dumped image to " + fileName);
} catch (IOException ex) {}
throw new
RuntimeException("Test Failed: found wrong color: 0x"+
Integer.toHexString(currPixel));
}
}
}
System.out.println("Test Passed.");
}
/**
* Paints a region of a component
*
* @param paintingComponent Component to paint
* @param bufferComponent Component to obtain buffer for
* @param g Graphics to paint to
* @param x X-coordinate
* @param y Y-coordinate
* @param w Width
* @param h Height
* @return true if painting was successful.
*/
public boolean paint(JComponent paintingComponent,
JComponent bufferComponent, Graphics g,
int x, int y, int w, int h) {
// First attempt to use VolatileImage buffer for performance.
// If this fails (which should rarely occur), fallback to a
// standard Image buffer.
boolean paintCompleted = false;
Image offscreen;
int sw = w + 1;
int sh = h + 1;
if (repaintManager.useVolatileDoubleBuffer() &&
(offscreen = getValidImage(repaintManager.
getVolatileOffscreenBuffer(bufferComponent, sw, sh))) != null) {
VolatileImage vImage = (java.awt.image.VolatileImage)offscreen;
GraphicsConfiguration gc = bufferComponent.
getGraphicsConfiguration();
for (int i = 0; !paintCompleted &&
i < RepaintManager.VOLATILE_LOOP_MAX; i++) {
if (vImage.validate(gc) ==
VolatileImage.IMAGE_INCOMPATIBLE) {
repaintManager.resetVolatileDoubleBuffer(gc);
offscreen = repaintManager.getVolatileOffscreenBuffer(
bufferComponent, sw, sh);
vImage = (java.awt.image.VolatileImage)offscreen;
}
paintDoubleBuffered(paintingComponent, vImage, g, x, y,
w, h);
paintCompleted = !vImage.contentsLost();
}
}
// VolatileImage painting loop failed, fallback to regular
// offscreen buffer
if (!paintCompleted && (offscreen = getValidImage(
repaintManager.getOffscreenBuffer(
bufferComponent, w, h))) != null) {
paintDoubleBuffered(paintingComponent, offscreen, g, x, y, w,
h);
paintCompleted = true;
}
return paintCompleted;
}