430N/A/*
2362N/A * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
430N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
430N/A *
430N/A * This code is free software; you can redistribute it and/or modify it
430N/A * under the terms of the GNU General Public License version 2 only, as
430N/A * published by the Free Software Foundation.
430N/A *
430N/A * This code is distributed in the hope that it will be useful, but WITHOUT
430N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
430N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
430N/A * version 2 for more details (a copy is included in the LICENSE file that
430N/A * accompanied this code).
430N/A *
430N/A * You should have received a copy of the GNU General Public License version
430N/A * 2 along with this work; if not, write to the Free Software Foundation,
430N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
430N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
430N/A */
430N/A
430N/A/*
430N/A * @test
430N/A * @bug 6689025
430N/A * @summary Tests that transformed Paints are rendered correctly
430N/A * @author Dmitri.Trembovetski@sun.com: area=Graphics
430N/A * @run main/othervm TransformedPaintTest
430N/A * @run main/othervm -Dsun.java2d.opengl=True TransformedPaintTest
430N/A */
430N/A
430N/Aimport java.awt.Color;
430N/Aimport java.awt.Dimension;
430N/Aimport java.awt.EventQueue;
430N/Aimport java.awt.GradientPaint;
430N/Aimport java.awt.Graphics;
430N/Aimport java.awt.Graphics2D;
430N/Aimport java.awt.GraphicsConfiguration;
430N/Aimport java.awt.GraphicsEnvironment;
430N/Aimport java.awt.LinearGradientPaint;
430N/Aimport java.awt.MultipleGradientPaint.CycleMethod;
430N/Aimport java.awt.Paint;
430N/Aimport java.awt.RadialGradientPaint;
430N/Aimport java.awt.TexturePaint;
430N/Aimport java.awt.geom.Rectangle2D;
430N/Aimport java.awt.image.BufferedImage;
430N/Aimport java.awt.image.VolatileImage;
430N/Aimport java.io.File;
430N/Aimport java.io.IOException;
430N/Aimport java.lang.reflect.InvocationTargetException;
430N/Aimport javax.imageio.ImageIO;
430N/Aimport javax.swing.JFrame;
430N/Aimport javax.swing.JPanel;
430N/A
430N/Apublic class TransformedPaintTest {
430N/A
430N/A private static enum PaintType {
430N/A COLOR,
430N/A GRADIENT,
430N/A LINEAR_GRADIENT,
430N/A RADIAL_GRADIENT,
430N/A TEXTURE
430N/A };
430N/A
430N/A private static final int CELL_SIZE = 100;
430N/A private static final int R_WIDTH = 3 * CELL_SIZE;
430N/A private static final int R_HEIGHT = PaintType.values().length * CELL_SIZE;
430N/A
430N/A private Paint createPaint(PaintType type, int startx, int starty,
430N/A int w, int h)
430N/A {
430N/A // make sure that the blue color doesn't show up when filling a
430N/A // w by h rect
430N/A w++; h++;
430N/A
430N/A int endx = startx + w;
430N/A int endy = starty + h;
430N/A Rectangle2D.Float r = new Rectangle2D.Float(startx, starty, w, h);
430N/A switch (type) {
430N/A case COLOR: return Color.red;
430N/A case GRADIENT: return
430N/A new GradientPaint(startx, starty, Color.red,
430N/A endx, endy, Color.green);
430N/A case LINEAR_GRADIENT: return
430N/A new LinearGradientPaint(startx, starty, endx, endy,
430N/A new float[] { 0.0f, 0.999f, 1.0f },
430N/A new Color[] { Color.red, Color.green, Color.blue });
430N/A case RADIAL_GRADIENT: return
430N/A new RadialGradientPaint(startx, starty,
430N/A (float)Math.sqrt(w * w + h * h),
430N/A new float[] { 0.0f, 0.999f, 1.0f },
430N/A new Color[] { Color.red, Color.green, Color.blue },
430N/A CycleMethod.NO_CYCLE);
430N/A case TEXTURE: {
430N/A BufferedImage bi =
430N/A new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
430N/A Graphics2D g = (Graphics2D) bi.getGraphics();
430N/A g.setPaint(createPaint(PaintType.LINEAR_GRADIENT, 0, 0, w, h));
430N/A g.fillRect(0, 0, w, h);
430N/A return new TexturePaint(bi, r);
430N/A }
430N/A }
430N/A return Color.green;
430N/A }
430N/A
430N/A private void renderLine(PaintType type, Graphics2D g,
430N/A int startx, int starty, int w, int h)
430N/A {
430N/A Paint p = createPaint(type, startx, starty, w, h);
430N/A g.setPaint(p);
430N/A
430N/A // first, no transform
430N/A g.fillRect(startx, starty, w, h);
430N/A
430N/A // translation only
430N/A g.translate(w, 0);
430N/A g.fillRect(startx, starty, w, h);
430N/A g.translate(-w, 0);
430N/A
430N/A // complex transform
430N/A g.translate(startx + w*2, starty);
430N/A g.rotate(Math.toRadians(90), w/2, h/2);
430N/A g.translate(-startx, -starty);
430N/A g.fillRect(startx, starty, w, h);
430N/A }
430N/A
430N/A private void render(Graphics2D g, int w, int h) {
430N/A int paintTypes = PaintType.values().length;
430N/A int ystep = h / paintTypes;
430N/A int y = 0;
430N/A
430N/A for (PaintType type : PaintType.values()) {
430N/A renderLine(type, (Graphics2D)g.create(),
430N/A 0, y, h / paintTypes, h / paintTypes);
430N/A y += ystep;
430N/A }
430N/A }
430N/A
430N/A private void checkBI(BufferedImage bi) {
430N/A for (int y = 0; y < bi.getHeight(); y++) {
430N/A for (int x = 0; x < bi.getWidth(); x++) {
430N/A if (bi.getRGB(x, y) == Color.blue.getRGB()) {
430N/A try {
430N/A String fileName = "TransformedPaintTest_res.png";
430N/A ImageIO.write(bi, "png", new File(fileName));
430N/A System.err.println("Dumped image to: " + fileName);
430N/A } catch (IOException ex) {}
430N/A throw new RuntimeException("Test failed, blue color found");
430N/A }
430N/A }
430N/A }
430N/A }
430N/A
430N/A private void runTest() {
430N/A GraphicsConfiguration gc = GraphicsEnvironment.
430N/A getLocalGraphicsEnvironment().getDefaultScreenDevice().
430N/A getDefaultConfiguration();
430N/A
430N/A if (gc.getColorModel().getPixelSize() < 16) {
430N/A System.out.println("8-bit desktop depth found, test passed");
430N/A return;
430N/A }
430N/A
430N/A VolatileImage vi = gc.createCompatibleVolatileImage(R_WIDTH, R_HEIGHT);
430N/A BufferedImage bi = null;
430N/A do {
430N/A vi.validate(gc);
430N/A Graphics2D g = vi.createGraphics();
430N/A render(g, vi.getWidth(), vi.getHeight());
430N/A bi = vi.getSnapshot();
430N/A } while (vi.contentsLost());
430N/A
430N/A checkBI(bi);
430N/A System.out.println("Test PASSED.");
430N/A }
430N/A
430N/A private static void showFrame(final TransformedPaintTest t) {
430N/A JFrame f = new JFrame("TransformedPaintTest");
430N/A f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
430N/A final BufferedImage bi =
430N/A new BufferedImage(R_WIDTH, R_HEIGHT, BufferedImage.TYPE_INT_RGB);
430N/A JPanel p = new JPanel() {
430N/A @Override
430N/A protected void paintComponent(Graphics g) {
430N/A super.paintComponent(g);
430N/A Graphics2D g2d = (Graphics2D) g;
430N/A t.render(g2d, R_WIDTH, R_HEIGHT);
430N/A t.render(bi.createGraphics(), R_WIDTH, R_HEIGHT);
430N/A g2d.drawImage(bi, R_WIDTH + 5, 0, null);
430N/A
430N/A g.setColor(Color.black);
430N/A g.drawString("Rendered to Back Buffer", 10, 20);
430N/A g.drawString("Rendered to BufferedImage", R_WIDTH + 15, 20);
430N/A }
430N/A };
430N/A p.setPreferredSize(new Dimension(2 * R_WIDTH + 5, R_HEIGHT));
430N/A f.add(p);
430N/A f.pack();
430N/A f.setVisible(true);
430N/A }
430N/A
430N/A public static void main(String[] args) throws
430N/A InterruptedException, InvocationTargetException
430N/A {
430N/A boolean show = (args.length > 0 && "-show".equals(args[0]));
430N/A
430N/A final TransformedPaintTest t = new TransformedPaintTest();
430N/A if (show) {
430N/A EventQueue.invokeAndWait(new Runnable() {
430N/A public void run() {
430N/A showFrame(t);
430N/A }
430N/A });
430N/A } else {
430N/A t.runTest();
430N/A }
430N/A }
430N/A}