public Object initTest(TestEnvironment env, Result result) { Context ctx = new Context(); ctx.bimg = ((BufImg) env.getModifier(bufimgsrcroot)).getImage(); if (env.isEnabled(doRenderTo)) { Graphics2D g2d = ctx.bimg.createGraphics(); g2d.setColor(Color.white); g2d.fillRect(3, 0, 1, 1); g2d.dispose(); } if (env.isEnabled(doRenderFrom)) { GraphicsConfiguration cfg = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(); VolatileImage vimg = cfg.createCompatibleVolatileImage(8, 1); vimg.validate(cfg); Graphics2D g2d = vimg.createGraphics(); for (int i = 0; i < 100; i++) { g2d.drawImage(ctx.bimg, 0, 0, null); } g2d.dispose(); vimg.flush(); } result.setUnits(1); result.setUnitName(getUnitName()); return ctx; }
private static void test(BufferedImage bi, int type) throws IOException { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration(); VolatileImage vi = gc.createCompatibleVolatileImage(511, 255, type); BufferedImage gold = gc.createCompatibleImage(511, 255, type); // draw to compatible Image Graphics2D big = gold.createGraphics(); // force scaled blit big.drawImage(bi, 7, 11, 127, 111, 7, 11, 127 * 2, 111, null); big.dispose(); // draw to volatile image BufferedImage snapshot; while (true) { vi.validate(gc); if (vi.validate(gc) != VolatileImage.IMAGE_OK) { try { Thread.sleep(100); } catch (final InterruptedException ignored) { } continue; } Graphics2D vig = vi.createGraphics(); // force scaled blit vig.drawImage(bi, 7, 11, 127, 111, 7, 11, 127 * 2, 111, null); vig.dispose(); snapshot = vi.getSnapshot(); if (vi.contentsLost()) { try { Thread.sleep(100); } catch (final InterruptedException ignored) { } continue; } break; } // validate images for (int x = 7; x < 127; ++x) { for (int y = 11; y < 111; ++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."); } } } }
/** * Paints the image at the specified location. This method assumes scaling has already been done, * and simply paints the background image "as-is." * * @param g The graphics context. * @param x The x-coordinate at which to paint. * @param y The y-coordinate at which to paint. */ protected void paintImage(Graphics g, int x, int y) { if (bgImage != null) { do { int rc = bgImage.validate(null); // getGraphicsConfiguration()); if (rc == VolatileImage.IMAGE_RESTORED) { // FIXME: If the image needs to be restored are its width // and height still valid?? If not, we'll need to cache // these values... renderImage(bgImage.getWidth(), bgImage.getHeight(), getScalingHint()); } g.drawImage(bgImage, x, y, null); } while (bgImage.contentsLost()); } }
private VolatileImage createVolatileImage(int width, int height, int transparency) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration(); VolatileImage image = null; image = gc.createCompatibleVolatileImage(width, height, transparency); int valid = image.validate(gc); if (valid == VolatileImage.IMAGE_INCOMPATIBLE) { image = this.createVolatileImage(width, height, transparency); return image; } return image; }