Path: blob/master/test/jdk/java/awt/GradientPaint/GradientTransformTest.java
41149 views
/*1* Copyright (c) 2013, 2016, 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*/2223import java.awt.*;24import java.awt.MultipleGradientPaint.*;25import java.awt.geom.*;26import java.awt.image.*;2728/**29* @test30* @key headful31* @bug 802348332* @summary tests if the transform-parameter is applied correctly when creating33* a gradient.34* @author ceisserer35*/36public class GradientTransformTest extends Frame {37BufferedImage srcImg;38Image dstImg;3940public GradientTransformTest() {41srcImg = createSrcImage();42dstImg = getGraphicsConfiguration().createCompatibleVolatileImage(20,4320);44}4546protected void renderToVI(BufferedImage src, Image dst) {47Graphics2D g = (Graphics2D) dst.getGraphics();4849g.setColor(Color.WHITE);50g.fillRect(0, 0, dst.getWidth(null), dst.getHeight(null));5152AffineTransform at = new AffineTransform();53at.translate(-100, 0);5455g.setPaint(new LinearGradientPaint(new Point2D.Float(100, 0),56new Point2D.Float(120, 0), new float[] { 0.0f, 0.75f, 1.0f },57new Color[] { Color.red, Color.green, Color.blue },58CycleMethod.NO_CYCLE, ColorSpaceType.SRGB, at));5960g.fillRect(-10, -10, 30, 30);61}6263public void paint(Graphics g1) {64Graphics2D g = (Graphics2D) g1;65renderToVI(createSrcImage(), dstImg);66g.drawImage(dstImg, 20, 20, null);67}6869public void showFrame() {70setSize(500, 500);71setVisible(true);72}7374public void test() {75renderToVI(createSrcImage(), dstImg);7677BufferedImage validationImg = new BufferedImage(20, 20,78BufferedImage.TYPE_INT_RGB);79Graphics2D valG = (Graphics2D) validationImg.getGraphics();80valG.drawImage(dstImg, 0, 0, null);8182// Loop over all pixel, and count the different pixel values83// encountered.84boolean gradientTranslated = false;85for (int x = 0; x < validationImg.getWidth() && !gradientTranslated; x++) {86for (int y = 0; y < validationImg.getHeight()87&& !gradientTranslated; y++) {88int rgb = validationImg.getRGB(x, y);89if (rgb != -65279) {90gradientTranslated = true;91}92}93}9495if (gradientTranslated) {96System.out.println("Passed!");97} else {98throw new RuntimeException("Test FAILED!");99}100}101102protected BufferedImage createSrcImage() {103BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);104Graphics2D g = (Graphics2D) bi.getGraphics();105g.setColor(Color.YELLOW);106g.fillRect(0, 0, 10, 10);107g.setColor(Color.black);108g.drawLine(0, 0, 10, 10);109return bi;110}111112public static void main(String[] args) throws Exception {113boolean show = (args.length > 0 && "-show".equals(args[0]));114final GradientTransformTest t = new GradientTransformTest();115116if (show) {117EventQueue.invokeAndWait(new Runnable() {118public void run() {119t.showFrame();120}121});122} else {123t.test();124}125}126}127128129