1 /*
   2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import sun.swing.SwingUtilities2;
  25 
  26 import java.awt.Color;
  27 import java.awt.Graphics2D;
  28 import java.awt.Rectangle;
  29 import java.awt.image.BufferedImage;
  30 import java.io.File;
  31 import java.io.IOException;
  32 
  33 import javax.imageio.ImageIO;
  34 
  35 /**
  36  * @test
  37  * @bug 8032219
  38  * @author Sergey Bylokhov
  39  * @modules java.desktop/sun.swing
  40  */
  41 public final class DrawRect {
  42 
  43     private static final int size = 50;
  44 
  45     private static final Rectangle[] rects = {
  46             new Rectangle(0, 0, 1, 1),
  47             new Rectangle(0, 0, 1, 2),
  48             new Rectangle(0, 0, 2, 1),
  49             new Rectangle(10, 10, 10, 10),
  50             new Rectangle(10, 10, -1, -1),
  51             new Rectangle(-1, -1, 10, 10),
  52             new Rectangle(-1, -1, -10, -10),
  53             new Rectangle(0, 0, size, size),
  54     };
  55 
  56     private static final Rectangle[] vlines = {new Rectangle(0, 0, 0, 0),
  57             new Rectangle(0, 0, 0, 1),
  58             new Rectangle(0, 0, 0, -1),
  59             new Rectangle(1, 1, 0, 1),
  60             new Rectangle(1, 1, 0, -1),
  61             new Rectangle(15, 15, 0, 10),
  62             new Rectangle(15, 15, 0, -10),
  63     };
  64     private static final Rectangle[] hlines = {new Rectangle(0, 0, 0, 0),
  65             new Rectangle(0, 0, 1, 0),
  66             new Rectangle(0, 0, -1, 0),
  67             new Rectangle(1, 1, 1, 0),
  68             new Rectangle(1, 1, -1, 0),
  69             new Rectangle(15, 15, 10, 0),
  70             new Rectangle(15, 15, -10, 0),
  71     };
  72 
  73     public static void main(final String[] args) throws IOException {
  74         BufferedImage gold = new BufferedImage(size, size,
  75                                                BufferedImage.TYPE_INT_ARGB);
  76         Graphics2D g = gold.createGraphics();
  77         BufferedImage bi = new BufferedImage(size, size,
  78                                              BufferedImage.TYPE_INT_ARGB);
  79         Graphics2D g2d = bi.createGraphics();
  80         g2d.setColor(new Color(0, 250, 0, 100));
  81         g2d.setBackground(Color.BLACK);
  82         g.setColor(new Color(0, 250, 0, 100));
  83         g.setBackground(Color.BLACK);
  84         // Rectangle
  85         for (final Rectangle r : rects) {
  86             g.clearRect(0, 0, size, size);
  87             g2d.clearRect(0, 0, size, size);
  88             g.drawRect(r.x, r.y, r.width, r.height);
  89             SwingUtilities2.drawRect(g2d, r.x, r.y, r.width, r.height);
  90             test(gold, bi);
  91         }
  92         // Vertical Line
  93         for (final Rectangle l : vlines) {
  94             g.clearRect(0, 0, size, size);
  95             g2d.clearRect(0, 0, size, size);
  96             g.drawLine(l.x, l.y, l.x + l.width, l.y + l.height);
  97             SwingUtilities2.drawVLine(g2d, l.x, l.y, l.y + l.height);
  98             test(gold, bi);
  99         }
 100         // Horizontal Line
 101         for (final Rectangle l : hlines) {
 102             g.clearRect(0, 0, size, size);
 103             g2d.clearRect(0, 0, size, size);
 104             g.drawLine(l.x, l.y, l.x + l.width, l.y + l.height);
 105             SwingUtilities2.drawHLine(g2d, l.x, l.x+l.width, l.y);
 106             test(gold, bi);
 107         }
 108 
 109         g.dispose();
 110         g2d.dispose();
 111     }
 112 
 113     private static void test(final BufferedImage gold, final BufferedImage bi)
 114             throws IOException {
 115         for (int x = 0; x < size; x++) {
 116             for (int y = 0; y < size; y++) {
 117                 if (gold.getRGB(x, y) != bi.getRGB(x, y)) {
 118                     ImageIO.write(gold, "png", new File("gold.png"));
 119                     ImageIO.write(bi, "png", new File("image.png"));
 120                     throw new RuntimeException("wrong color");
 121                 }
 122             }
 123         }
 124     }
 125 }