Path: blob/master/test/jdk/sun/java2d/DirectX/AccelPaintsTest/AccelPaintsTest.java
41153 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 6659345 819861327* @summary Tests that various paints work correctly when preceeded by a28* textured operaiton.29* @author [email protected]: area=Graphics30* @run main/othervm AccelPaintsTest31*/3233import java.awt.Color;34import java.awt.Dimension;35import java.awt.EventQueue;36import java.awt.GradientPaint;37import java.awt.Graphics;38import java.awt.Graphics2D;39import java.awt.GraphicsConfiguration;40import java.awt.GraphicsEnvironment;41import java.awt.LinearGradientPaint;42import java.awt.MultipleGradientPaint.CycleMethod;43import java.awt.Paint;44import java.awt.RadialGradientPaint;45import java.awt.Rectangle;46import java.awt.Shape;47import java.awt.TexturePaint;48import java.awt.Transparency;49import java.awt.geom.Rectangle2D;50import java.awt.image.BufferedImage;51import java.awt.image.VolatileImage;52import java.io.File;53import java.io.IOException;54import java.lang.reflect.InvocationTargetException;55import javax.imageio.ImageIO;56import javax.swing.JFrame;57import javax.swing.JPanel;5859public class AccelPaintsTest extends JPanel {60BufferedImage bi =61new BufferedImage(80, 100, BufferedImage.TYPE_INT_ARGB_PRE);6263RadialGradientPaint rgp =64new RadialGradientPaint(100, 100, 100, new float[] {0f, 0.2f, 0.6f, 1f},65new Color[] { Color.red,66Color.yellow,67Color.blue,68Color.green},69CycleMethod.REFLECT);70LinearGradientPaint lgp =71new LinearGradientPaint(30, 30, 120, 130, new float[] {0f, 0.2f, 0.6f, 1f},72new Color[] {Color.red,73Color.yellow,74Color.blue,75Color.green});76GradientPaint gp =77new GradientPaint(30, 30, Color.red, 120, 130, Color.yellow, true);7879TexturePaint tp =80new TexturePaint(bi, new Rectangle2D.Float(30, 30, 120, 130));818283public AccelPaintsTest() {84Graphics g = bi.getGraphics();85g.setColor(Color.blue);86g.fillRect(0, 0, bi.getWidth(), bi.getHeight());8788setPreferredSize(new Dimension(250, 4*120));89}9091private void renderWithPaint(Graphics2D g2d, Paint p) {92g2d.drawImage(bi, 130, 30, null);9394g2d.setPaint(p);95g2d.fillRect(30, 30, 80, 100);96}9798private void render(Graphics2D g2d) {99renderWithPaint(g2d, rgp);100g2d.translate(0, 100);101102renderWithPaint(g2d, lgp);103g2d.translate(0, 100);104105renderWithPaint(g2d, gp);106g2d.translate(0, 100);107108renderWithPaint(g2d, tp);109g2d.translate(0, 100);110}111112private void test() {113GraphicsConfiguration gc =114GraphicsEnvironment.getLocalGraphicsEnvironment().115getDefaultScreenDevice().getDefaultConfiguration();116if (gc.getColorModel().getPixelSize() < 16) {117System.out.println("<16 bit depth detected, test passed");118return;119}120121VolatileImage vi =122gc.createCompatibleVolatileImage(250, 4*120, Transparency.OPAQUE);123BufferedImage res;124do {125vi.validate(gc);126Graphics2D g2d = vi.createGraphics();127g2d.setColor(Color.white);128g2d.fillRect(0, 0, vi.getWidth(), vi.getHeight());129130render(g2d);131132res = vi.getSnapshot();133} while (vi.contentsLost());134135for (int y = 0; y < bi.getHeight(); y++) {136for (int x = 0; x < bi.getWidth(); x++) {137if (res.getRGB(x, y) == Color.black.getRGB()) {138System.err.printf("Test FAILED: found black at %d,%d\n",139x, y);140try {141String fileName = "AccelPaintsTest.png";142ImageIO.write(res, "png", new File(fileName));143System.err.println("Dumped rendering to " + fileName);144} catch (IOException e) {}145throw new RuntimeException("Test FAILED: found black");146}147}148}149}150151protected void paintComponent(Graphics g) {152super.paintComponent(g);153Graphics2D g2d = (Graphics2D)g;154155render(g2d);156}157158public static void main(String[] args)159throws InterruptedException, InvocationTargetException160{161162if (args.length > 0 && args[0].equals("-show")) {163EventQueue.invokeAndWait(new Runnable() {164public void run() {165JFrame f = new JFrame("RadialGradientTest");166f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);167AccelPaintsTest t = new AccelPaintsTest();168f.add(t);169f.pack();170f.setVisible(true);171}172});173} else {174AccelPaintsTest t = new AccelPaintsTest();175t.test();176System.out.println("Test Passed.");177}178}179}180181182