Path: blob/master/test/jdk/sun/java2d/DirectX/TransformedPaintTest/TransformedPaintTest.java
41154 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 6689025 8023483 819861327* @summary Tests that transformed Paints are rendered correctly28* @author [email protected]: area=Graphics29* @run main/othervm TransformedPaintTest30*/3132import java.awt.Color;33import java.awt.Dimension;34import java.awt.EventQueue;35import java.awt.GradientPaint;36import java.awt.Graphics;37import java.awt.Graphics2D;38import java.awt.GraphicsConfiguration;39import java.awt.GraphicsEnvironment;40import java.awt.LinearGradientPaint;41import java.awt.MultipleGradientPaint.CycleMethod;42import java.awt.Paint;43import java.awt.RadialGradientPaint;44import java.awt.TexturePaint;45import java.awt.geom.Rectangle2D;46import java.awt.image.BufferedImage;47import java.awt.image.VolatileImage;48import java.io.File;49import java.io.IOException;50import java.lang.reflect.InvocationTargetException;51import javax.imageio.ImageIO;52import javax.swing.JFrame;53import javax.swing.JPanel;5455public class TransformedPaintTest {5657private static enum PaintType {58COLOR,59GRADIENT,60LINEAR_GRADIENT,61RADIAL_GRADIENT,62TEXTURE63};6465private static final int CELL_SIZE = 100;66private static final int R_WIDTH = 3 * CELL_SIZE;67private static final int R_HEIGHT = PaintType.values().length * CELL_SIZE;6869private Paint createPaint(PaintType type, int startx, int starty,70int w, int h)71{72// make sure that the blue color doesn't show up when filling a73// w by h rect74w++; h++;7576int endx = startx + w;77int endy = starty + h;78Rectangle2D.Float r = new Rectangle2D.Float(startx, starty, w, h);79switch (type) {80case COLOR: return Color.red;81case GRADIENT: return82new GradientPaint(startx, starty, Color.red,83endx, endy, Color.green);84case LINEAR_GRADIENT: return85new LinearGradientPaint(startx, starty, endx, endy,86new float[] { 0.0f, 0.999f, 1.0f },87new Color[] { Color.red, Color.green, Color.blue });88case RADIAL_GRADIENT: return89new RadialGradientPaint(startx, starty,90(float)Math.sqrt(w * w + h * h),91new float[] { 0.0f, 0.999f, 1.0f },92new Color[] { Color.red, Color.green, Color.blue },93CycleMethod.NO_CYCLE);94case TEXTURE: {95BufferedImage bi =96new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);97Graphics2D g = (Graphics2D) bi.getGraphics();98g.setPaint(createPaint(PaintType.LINEAR_GRADIENT, 0, 0, w, h));99g.fillRect(0, 0, w, h);100return new TexturePaint(bi, r);101}102}103return Color.green;104}105106private void renderLine(PaintType type, Graphics2D g,107int startx, int starty, int w, int h)108{109Paint p = createPaint(type, startx, starty, w, h);110g.setPaint(p);111112// first, no transform113g.fillRect(startx, starty, w, h);114115// translation only116g.translate(w, 0);117g.fillRect(startx, starty, w, h);118g.translate(-w, 0);119120// complex transform121g.translate(startx + w*2, starty);122g.rotate(Math.toRadians(90), w/2, h/2);123g.translate(-startx, -starty);124g.fillRect(startx, starty, w, h);125}126127private void render(Graphics2D g, int w, int h) {128int paintTypes = PaintType.values().length;129int ystep = h / paintTypes;130int y = 0;131132for (PaintType type : PaintType.values()) {133renderLine(type, (Graphics2D)g.create(),1340, y, h / paintTypes, h / paintTypes);135y += ystep;136}137}138139private void checkBI(BufferedImage bi) {140for (int y = 0; y < bi.getHeight(); y++) {141for (int x = 0; x < bi.getWidth(); x++) {142if (bi.getRGB(x, y) == Color.blue.getRGB()) {143try {144String fileName = "TransformedPaintTest_res.png";145ImageIO.write(bi, "png", new File(fileName));146System.err.println("Dumped image to: " + fileName);147} catch (IOException ex) {}148throw new RuntimeException("Test failed, blue color found");149}150}151}152}153154private void runTest() {155GraphicsConfiguration gc = GraphicsEnvironment.156getLocalGraphicsEnvironment().getDefaultScreenDevice().157getDefaultConfiguration();158159if (gc.getColorModel().getPixelSize() < 16) {160System.out.println("8-bit desktop depth found, test passed");161return;162}163164VolatileImage vi = gc.createCompatibleVolatileImage(R_WIDTH, R_HEIGHT);165BufferedImage bi = null;166do {167vi.validate(gc);168Graphics2D g = vi.createGraphics();169render(g, vi.getWidth(), vi.getHeight());170bi = vi.getSnapshot();171} while (vi.contentsLost());172173checkBI(bi);174System.out.println("Test PASSED.");175}176177private static void showFrame(final TransformedPaintTest t) {178JFrame f = new JFrame("TransformedPaintTest");179f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);180final BufferedImage bi =181new BufferedImage(R_WIDTH, R_HEIGHT, BufferedImage.TYPE_INT_RGB);182JPanel p = new JPanel() {183@Override184protected void paintComponent(Graphics g) {185super.paintComponent(g);186Graphics2D g2d = (Graphics2D) g;187t.render(g2d, R_WIDTH, R_HEIGHT);188t.render(bi.createGraphics(), R_WIDTH, R_HEIGHT);189g2d.drawImage(bi, R_WIDTH + 5, 0, null);190191g.setColor(Color.black);192g.drawString("Rendered to Back Buffer", 10, 20);193g.drawString("Rendered to BufferedImage", R_WIDTH + 15, 20);194}195};196p.setPreferredSize(new Dimension(2 * R_WIDTH + 5, R_HEIGHT));197f.add(p);198f.pack();199f.setVisible(true);200}201202public static void main(String[] args) throws203InterruptedException, InvocationTargetException204{205boolean show = (args.length > 0 && "-show".equals(args[0]));206207final TransformedPaintTest t = new TransformedPaintTest();208if (show) {209EventQueue.invokeAndWait(new Runnable() {210public void run() {211showFrame(t);212}213});214} else {215t.runTest();216}217}218}219220221