1 /*
   2  * Copyright (c) 2014, 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  */
  40 public final class DrawRect {
  41 
  42     private static final int size = 50;
  43 
  44     private static final Rectangle[] rects = {
  45             new Rectangle(0, 0, 1, 1),
  46             new Rectangle(0, 0, 1, 2),
  47             new Rectangle(0, 0, 2, 1),
  48             new Rectangle(10, 10, 10, 10),
  49             new Rectangle(10, 10, -1, -1),
  50             new Rectangle(-1, -1, 10, 10),
  51             new Rectangle(-1, -1, -10, -10),
  52             new Rectangle(0, 0, size, size),
  53     };
  54 
  55     private static final Rectangle[] vlines = {new Rectangle(0, 0, 0, 0),
  56             new Rectangle(0, 0, 0, 1),
  57             new Rectangle(0, 0, 0, -1),
  58             new Rectangle(1, 1, 0, 1),
  59             new Rectangle(1, 1, 0, -1),
  60             new Rectangle(15, 15, 0, 10),
  61             new Rectangle(15, 15, 0, -10),
  62     };
  63     private static final Rectangle[] hlines = {new Rectangle(0, 0, 0, 0),
  64             new Rectangle(0, 0, 1, 0),
  65             new Rectangle(0, 0, -1, 0),
  66             new Rectangle(1, 1, 1, 0),
  67             new Rectangle(1, 1, -1, 0),
  68             new Rectangle(15, 15, 10, 0),
  69             new Rectangle(15, 15, -10, 0),
  70     };
  71 
  72     public static void main(final String[] args) throws IOException {
  73         BufferedImage gold = new BufferedImage(size, size,
  74                                                BufferedImage.TYPE_INT_ARGB);
  75         Graphics2D g = gold.createGraphics();
  76         BufferedImage bi = new BufferedImage(size, size,
  77                                              BufferedImage.TYPE_INT_ARGB);
  78         Graphics2D g2d = bi.createGraphics();
  79         g2d.setColor(new Color(0, 250, 0, 100));
  80         g2d.setBackground(Color.BLACK);
  81         g.setColor(new Color(0, 250, 0, 100));
  82         g.setBackground(Color.BLACK);
  83         // Rectangle
  84         for (final Rectangle r : rects) {
  85             g.clearRect(0, 0, size, size);
  86             g2d.clearRect(0, 0, size, size);
  87             g.drawRect(r.x, r.y, r.width, r.height);
  88             SwingUtilities2.drawRect(g2d, r.x, r.y, r.width, r.height);
  89             test(gold, bi);
  90         }
  91         // Vertical Line
  92         for (final Rectangle l : vlines) {
  93             g.clearRect(0, 0, size, size);
  94             g2d.clearRect(0, 0, size, size);
  95             g.drawLine(l.x, l.y, l.x + l.width, l.y + l.height);
  96             SwingUtilities2.drawVLine(g2d, l.x, l.y, l.y + l.height);
  97             test(gold, bi);
  98         }
  99         // Horizontal Line
 100         for (final Rectangle l : hlines) {
 101             g.clearRect(0, 0, size, size);
 102             g2d.clearRect(0, 0, size, size);
 103             g.drawLine(l.x, l.y, l.x + l.width, l.y + l.height);
 104             SwingUtilities2.drawHLine(g2d, l.x, l.x+l.width, l.y);
 105             test(gold, bi);
 106         }
 107 
 108         g.dispose();
 109         g2d.dispose();
 110     }
 111 
 112     private static void test(final BufferedImage gold, final BufferedImage bi)
 113             throws IOException {
 114         for (int x = 0; x < size; x++) {
 115             for (int y = 0; y < size; y++) {
 116                 if (gold.getRGB(x, y) != bi.getRGB(x, y)) {
 117                     ImageIO.write(gold, "png", new File("gold.png"));
 118                     ImageIO.write(bi, "png", new File("image.png"));
 119                     throw new RuntimeException("wrong color");
 120                 }
 121             }
 122         }
 123     }
 124 }