1 /* 2 * Copyright (c) 2011, 2013, 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 /* 25 * @test 26 * @summary Test to insure a crash case does not regress. 27 * @summary com.apple.junit.java.graphics.Polygons 28 */ 29 30 import junit.framework.*; 31 import java.awt.*; 32 33 public class HexagonTest01 extends TestCase { 34 35 static final int kTimeOutMillis = 400; 36 37 class Hexagon extends Polygon { 38 private void init(final int x, final int y, final int size) { 39 for (double theta = 0; theta <= 2 * Math.PI; theta += Math.PI/3) { 40 int px = (int) (x + size * Math.cos(theta)); 41 int py = (int) (y + size * Math.sin(theta)); 42 addPoint(px, py); 43 } 44 } 45 46 public Hexagon(final int x, final int y, final int size) { 47 super(); 48 init(x,y,size); 49 } 50 51 public Hexagon(final Point origin, final int size) { 52 super(); 53 init((int) origin.getX(), (int) origin.getY(), size); 54 } 55 } 56 57 58 class HexGrid { 59 Hexagon[][] grid; 60 61 public HexGrid(int width, int height, int radius) { 62 grid = new Hexagon[width][height]; 63 double apothem = radius * Math.sqrt(3) / 2; 64 65 for (int xi = 0; xi < width; xi++) { 66 for (int yi = 0; yi < height; yi++) { 67 double xoff = (xi * (3 * radius)); 68 double yoff = (yi * (apothem)); 69 if (yi % 2 == 1) { 70 xoff += 1.5 * radius; 71 } 72 grid[xi][yi] = new Hexagon((int)xoff , (int) yoff, radius); 73 } 74 } 75 } 76 77 public void draw( Graphics2D g) { 78 for (int i = 0; i < grid.length; i++) { 79 for (int j = 0; j < grid[i].length; j++) { 80 g.setColor( new Color( 255, (i * 20) % 255, (j * 15) % 255) ); 81 g.fill( grid[i][j]); 82 g.setColor(Color.black); 83 g.draw( grid[i][j]); 84 } 85 } 86 } 87 } 88 89 class DisplayFrame extends Frame { 90 public DisplayFrame() { 91 super("Test"); 92 setSize(700, 600); 93 } 94 95 public void paint( Graphics g) { 96 Graphics2D 97 g2d = (Graphics2D) g; 98 g.setColor(Color.white); 99 g.fillRect(0, 0, getWidth(), getHeight()); 100 g.translate(75, 75); 101 g2d.setColor(Color.black); 102 g2d.setStroke(stroke); 103 grid.draw(g2d); } 104 } 105 106 final BasicStroke stroke = new BasicStroke( 1.2f); 107 final HexGrid grid = new HexGrid(4,12,50); 108 private DisplayFrame testFrame; 109 110 public void testThatIDontCrash() throws Exception { 111 testFrame = new DisplayFrame(); 112 testFrame.setVisible(true); 113 Thread.sleep(kTimeOutMillis); 114 testFrame.dispose(); 115 } 116 117 118 // boilerplate below 119 public static Test suite() { 120 return new TestSuite(HexagonTest01.class); 121 } 122 123 public static void main (String[] args) throws RuntimeException { 124 TestResult tr = junit.textui.TestRunner.run(suite()); 125 if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) { 126 throw new RuntimeException("### FAILED: unexpected JUnit errors or failures."); 127 } 128 } 129 }