/*
 * @(#)XformVolatile.java	1.2 04/03/03
 * 
 * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * -Redistribution of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 * 
 * -Redistribution in binary form must reproduce the above copyright notice, 
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may 
 * be used to endorse or promote products derived from this software without 
 * specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL 
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST 
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, 
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY 
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, 
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */

/*
 * @test @(#)XformVolatile.java	1.2 04/03/03
 * @bug 4970836
 * @summary Verifies that transformed VolatileImage copies work properly with
 * the OGL pipeline.
 * @run main/othervm XformVolatile
 * @author campbelc
 */

import java.awt.*;
import java.awt.image.*;

public class XformVolatile extends Panel {

    private static boolean done;
    private static volatile BufferedImage capture;
    private static void doCapture(Component test) {
        // Grab the screen region
	try {
            Robot robot = new Robot();
            Point pt1 = test.getLocationOnScreen();
            Rectangle rect = new Rectangle(pt1.x, pt1.y, 200, 200);
            capture = robot.createScreenCapture(rect);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void paint(Graphics g) {

        Graphics2D g2d = (Graphics2D)g;

        VolatileImage img = createVolatileImage(200, 200);
        img.validate(getGraphicsConfiguration());
        Graphics2D goff = img.createGraphics();
        goff.setColor(Color.blue);
        goff.fillRect(0, 0, 200, 200);
        goff.setColor(Color.red);
        goff.fillPolygon(new int[] {10, 100, 190},
                         new int[] {190, 10, 190}, 3);
        goff.dispose();

        g2d.setColor(Color.black);
        g2d.fillRect(0, 0, getWidth(), getHeight());

        g2d.rotate(Math.toRadians(3.0));
        g2d.drawImage(img, 0, 0, null);

        img.flush();
        img = null;

        try{Thread.sleep(1000);}catch(Exception exx){}
        Toolkit.getDefaultToolkit().sync();

        synchronized (this) {
            if (!done) {
                doCapture(this);
                done = true;
            }
            notifyAll();
        }
    }

    public static void main(String[] args) {
        boolean show = (args.length == 1) && ("-show".equals(args[0]));

        XformVolatile test = new XformVolatile();
        Frame frame = new Frame();
        frame.add(test);
        frame.pack();
        frame.setSize(300, 300);
        frame.setVisible(true);

        // Wait until the component's been painted
        synchronized (test) {
            while (!done) {
                try {
                    test.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException("Failed: Interrupted");
                }
            }
        }

        if (!show) {
            frame.dispose();
        }
        if (capture == null) {
            throw new RuntimeException("Failed: capture is null");
        }

        // Test inner and outer pixels
        int pixel1 = capture.getRGB(5, 175);
        if (pixel1 != 0xff0000ff) {
            throw new RuntimeException("Failed: Incorrect color for " +
                                       "outer pixel at 5,175 (actual=" +
                                       Integer.toHexString(pixel1) +
                                       " expected=0xff0000ff)");
        }
        int pixel2 = capture.getRGB(5, 188);
        if (pixel2 != 0xffff0000) {
            throw new RuntimeException("Failed: Incorrect color for " +
                                       "inner pixel at 5,188 (actual=" +
                                       Integer.toHexString(pixel2) +
                                       " expected=0xffff0000)");
        }
    }
}
