Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/awt/GradientPaint/GradientTransformTest.java
48795 views
/*1* Copyright (c) 2013, 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* @bug 802348331* @summary tests if the transform-parameter is applied correctly when creating32* a gradient.33* @author ceisserer34*/35public class GradientTransformTest extends Frame {36BufferedImage srcImg;37Image dstImg;3839public GradientTransformTest() {40srcImg = createSrcImage();41dstImg = getGraphicsConfiguration().createCompatibleVolatileImage(20,4220);43}4445protected void renderToVI(BufferedImage src, Image dst) {46Graphics2D g = (Graphics2D) dst.getGraphics();4748g.setColor(Color.WHITE);49g.fillRect(0, 0, dst.getWidth(null), dst.getHeight(null));5051AffineTransform at = new AffineTransform();52at.translate(-100, 0);5354g.setPaint(new LinearGradientPaint(new Point2D.Float(100, 0),55new Point2D.Float(120, 0), new float[] { 0.0f, 0.75f, 1.0f },56new Color[] { Color.red, Color.green, Color.blue },57CycleMethod.NO_CYCLE, ColorSpaceType.SRGB, at));5859g.fillRect(-10, -10, 30, 30);60}6162public void paint(Graphics g1) {63Graphics2D g = (Graphics2D) g1;64renderToVI(createSrcImage(), dstImg);65g.drawImage(dstImg, 20, 20, null);66}6768public void showFrame() {69setSize(500, 500);70setVisible(true);71}7273public void test() {74renderToVI(createSrcImage(), dstImg);7576BufferedImage validationImg = new BufferedImage(20, 20,77BufferedImage.TYPE_INT_RGB);78Graphics2D valG = (Graphics2D) validationImg.getGraphics();79valG.drawImage(dstImg, 0, 0, null);8081// Loop over all pixel, and count the different pixel values82// encountered.83boolean gradientTranslated = false;84for (int x = 0; x < validationImg.getWidth() && !gradientTranslated; x++) {85for (int y = 0; y < validationImg.getHeight()86&& !gradientTranslated; y++) {87int rgb = validationImg.getRGB(x, y);88if (rgb != -65279) {89gradientTranslated = true;90}91}92}9394if (gradientTranslated) {95System.out.println("Passed!");96} else {97throw new RuntimeException("Test FAILED!");98}99}100101protected BufferedImage createSrcImage() {102BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);103Graphics2D g = (Graphics2D) bi.getGraphics();104g.setColor(Color.YELLOW);105g.fillRect(0, 0, 10, 10);106g.setColor(Color.black);107g.drawLine(0, 0, 10, 10);108return bi;109}110111public static void main(String[] args) throws Exception {112boolean show = (args.length > 0 && "-show".equals(args[0]));113final GradientTransformTest t = new GradientTransformTest();114115if (show) {116EventQueue.invokeAndWait(new Runnable() {117public void run() {118t.showFrame();119}120});121} else {122t.test();123}124}125}126127128