Brush { public void draw (Shape s, Graphics g, Rectangle bounds) { if (g instanceof Graphics2D) { Graphics2D g2 = (Graphics2D) g; AffineTransform transform = new AffineTransform(); Rectangle originalBounds = s.getBounds(); transform.translate(bounds.x, bounds.y); transform.scale((double)bounds.width / (double) originalBounds.width, (double)bounds.height / (double) originalBounds.height); transform.translate(-originalBounds.x, -originalBounds.y); drawTransformed(transform.createTransformedShape(s), g2); } }  void draw(Shape s, Graphics g, Rectangle bounds);  }
@Test public void testFill() { Brush b = new Fill(FOREGROUND); b.draw(ELLIPSE, g2, new Rectangle(0,0,WIDTH,HEIGHT)); for (int x = 0; x < WIDTH; x++) { for (int y = 0; y < HEIGHT; y++) { int dx = (WIDTH/2) - x; int dy = (HEIGHT/2) - y; double d = Math.sqrt(dx*dx + dy*dy); if (d < 149.0) AssertJUnit.assertEquals(image.getRGB(x, y), FOREGROUND.getRGB()); if (d > 151.0) AssertJUnit.assertEquals(image.getRGB(x, y), BACKGROUND.getRGB()); } } }
@Test public void testClippedFill() { int[] axes = { AxisClip.X_AXIS, AxisClip.Y_AXIS }; int[] dirs = { AxisClip.INCREASING, AxisClip.DECREASING }; for (int axis : axes) { for (int dir : dirs) { Fill b = new ClippedFill(FOREGROUND, axis, dir); b.setInterval(0.0, 1.0); for (double state = 0.25; state <= 0.75; state += 0.25) { b.setState(Double.toString(state)); Assert.assertEquals(Double.parseDouble(b.getState().toString()), state); g2.setColor(BACKGROUND); g2.fillRect(image.getMinX(), image.getMinY(), image.getWidth(), image.getHeight()); b.draw(ELLIPSE, g2, new Rectangle(0,0,WIDTH,HEIGHT)); for (int x = 0; x < WIDTH; x++) { for (int y = 0; y < HEIGHT; y++) { int i = (axis == AxisClip.X_AXIS) ? x : y; int max = (axis == AxisClip.X_AXIS) ? WIDTH : HEIGHT; if (dir == AxisClip.DECREASING) i = max - i; double expectedState = state * (double) max; if (i > expectedState + 1) { AssertJUnit.assertEquals(image.getRGB(x,y), BACKGROUND.getRGB()); } else if (i < expectedState - 1){ int dx = (WIDTH/2) - x; int dy = (HEIGHT/2) - y; double d = Math.sqrt(dx*dx + dy*dy); if (d < 149.0) AssertJUnit.assertEquals(image.getRGB(x, y), FOREGROUND.getRGB()); if (d > 151.0) AssertJUnit.assertEquals(image.getRGB(x, y), BACKGROUND.getRGB()); } } } } b.setState(Double.toString(1.5)); Assert.assertEquals(Double.parseDouble(b.getState().toString()), 1.0); b.setState(Double.toString(-0.5)); Assert.assertEquals(Double.parseDouble(b.getState().toString()), 0.0); } } }
@Test public void testScalingFill() { Fill b = new ScalingFill(FOREGROUND); b.setInterval(0.0, 1.0); for (double state = 0.25; state <= 0.75; state += 0.25) { b.setState(Double.toString(state)); Assert.assertEquals(Double.parseDouble(b.getState().toString()), state); g2.setColor(BACKGROUND); g2.fillRect(image.getMinX(), image.getMinY(), image.getWidth(), image.getHeight()); b.draw(ELLIPSE, g2, new Rectangle(0,0,WIDTH,HEIGHT)); for (int x = 0; x < WIDTH; x++) { for (int y = 0; y < HEIGHT; y++) { double expectedRadius = state * (double) WIDTH / 2; int dx = (WIDTH/2) - x; int dy = (HEIGHT/2) - y; double d = Math.sqrt(dx*dx + dy*dy); if (d < expectedRadius - 1) AssertJUnit.assertEquals(image.getRGB(x, y), FOREGROUND.getRGB()); if (d > expectedRadius + 1) AssertJUnit.assertEquals(image.getRGB(x, y), BACKGROUND.getRGB()); } } } b.setState(Double.toString(1.5)); Assert.assertEquals(Double.parseDouble(b.getState().toString()), 1.0); b.setState(Double.toString(-0.5)); Assert.assertEquals(Double.parseDouble(b.getState().toString()), 0.0); }
@Test public void testConditionalFill() { ConditionalBrush a = new ConditionalBrush(new Fill(FOREGROUND), CONDITION_A); ConditionalBrush b = new ConditionalBrush(new Fill(CONDITIONAL), CONDITION_B); String[] conditions = { CONDITION_A, CONDITION_B, CONDITION_A, CONDITION_B }; AssertJUnit.assertTrue(a instanceof StateSensitive); AssertJUnit.assertTrue(b instanceof StateSensitive); for (String condition : conditions) { a.setState(condition); b.setState(condition); a.draw(ELLIPSE, g2, new Rectangle(0,0,WIDTH,HEIGHT)); b.draw(ELLIPSE, g2, new Rectangle(0,0,WIDTH,HEIGHT)); for (int x = 0; x < WIDTH; x++) { for (int y = 0; y < HEIGHT; y++) { int dx = (WIDTH/2) - x; int dy = (HEIGHT/2) - y; double d = Math.sqrt(dx*dx + dy*dy); Color expected = (condition == CONDITION_A) ? FOREGROUND : CONDITIONAL; if (d < 149.0) AssertJUnit.assertEquals(image.getRGB(x, y), expected.getRGB()); if (d > 151.0) AssertJUnit.assertEquals(image.getRGB(x, y), BACKGROUND.getRGB()); } } } }