diff --git a/extras/src/main/java/edu/umd/cs/piccolox/util/ShadowUtils.java b/extras/src/main/java/edu/umd/cs/piccolox/util/ShadowUtils.java index 49ef01f..f1b5b6a 100755 --- a/extras/src/main/java/edu/umd/cs/piccolox/util/ShadowUtils.java +++ b/extras/src/main/java/edu/umd/cs/piccolox/util/ShadowUtils.java @@ -51,20 +51,21 @@ // empty } - /** - * Create and return a new buffered image containing a shadow of the specified source image - * using the specifed shadow paint and gaussian blur radius. The dimensions of the returned image will be - * src.getWidth() + 4 * blurRadius x src.getHeight() + 4 * blurRadius - * to account for blurring beyond the bounds of the source image. Thus the source image - * will appear to be be offset by (2 * blurRadius, 2 * blurRadius) - * in the returned image. - * + * Create and return a new buffered image containing a shadow of the + * specified source image using the specifed shadow paint and gaussian blur + * radius. The dimensions of the returned image will be + * src.getWidth() + 4 * blurRadius x + * src.getHeight() + 4 * blurRadius to account for blurring + * beyond the bounds of the source image. Thus the source image will appear + * to be be offset by (2 * blurRadius, + * 2 * blurRadius) in the returned image. + * * @param src source image, must not be null * @param shadowPaint shadow paint * @param blurRadius gaussian blur radius, must be > 0 - * @return a new buffered image containing a shadow of the specified source image - * using the specifed shadow paint and gaussian blur radius + * @return a new buffered image containing a shadow of the specified source + * image using the specifed shadow paint and gaussian blur radius */ public static BufferedImage createShadow(final Image src, final Paint shadowPaint, final int blurRadius) { if (src == null) { @@ -100,19 +101,20 @@ /** * Create a new gaussian kernel with the specified blur radius. - * + * * @param blurRadius blur radius */ GaussianKernel(final int blurRadius) { super((2 * blurRadius) + 1, (2 * blurRadius) + 1, createKernel(blurRadius)); } - /** - * Create an array of floats representing a gaussian kernel with the specified radius. - * + * Create an array of floats representing a gaussian kernel with the + * specified radius. + * * @param r radius - * @return an array of floats representing a gaussian kernel with the specified radius + * @return an array of floats representing a gaussian kernel with the + * specified radius */ private static float[] createKernel(final int r) { int w = (2 * r) + 1; @@ -121,16 +123,17 @@ double n = Math.PI * m; double sum = 0.0d; - for (int i = 0; i < w; i++) { - for (int j = 0; j < w; j++) { - kernel[i * w + j] = (float) (Math.pow(Math.E, -((j - r) * (j - r) + (i - r) * (i - r)) / m) / n); - sum += kernel[i * w + j]; + for (int x = 0; x < w; x++) { + int xr2 = (x-r) * (x-r); + for (int y = 0; y < w; y++) { + int yr2 = (y-r) * (y-r); + kernel[x * w + y] = (float) (Math.pow(Math.E, -(yr2 + xr2) / m) / n); + sum += kernel[x * w + y]; } } - for (int i = 0; i < w; i++) { - for (int j = 0; j < w; j++) { - kernel[i * w + j] /= sum; - } + + for (int i = kernel.length - 1; i >= 0; i--) { + kernel[i] /= sum; } return kernel; } diff --git a/extras/src/test/java/edu/umd/cs/piccolox/PAppletTest.java b/extras/src/test/java/edu/umd/cs/piccolox/PAppletTest.java new file mode 100644 index 0000000..ef27c50 --- /dev/null +++ b/extras/src/test/java/edu/umd/cs/piccolox/PAppletTest.java @@ -0,0 +1,26 @@ +package edu.umd.cs.piccolox; + +import junit.framework.TestCase; +import edu.umd.cs.piccolo.PCanvas; + +public class PAppletTest extends TestCase { + private PApplet applet; + + public void setUp() { + applet = new PApplet(); + applet.init(); + applet.setVisible(false); + } + + public void tearDown() { + applet.setVisible(false); + } + + public void testCanvasIsValidWithDefaultConstructor() { + PCanvas canvas = applet.getCanvas(); + assertNotNull(canvas); + assertNotNull(canvas.getLayer()); + assertNotNull(canvas.getCamera()); + assertSame(canvas.getLayer(), canvas.getCamera().getLayer(0)); + } +} diff --git a/extras/src/test/java/edu/umd/cs/piccolox/PFrameTest.java b/extras/src/test/java/edu/umd/cs/piccolox/PFrameTest.java new file mode 100644 index 0000000..b906e9b --- /dev/null +++ b/extras/src/test/java/edu/umd/cs/piccolox/PFrameTest.java @@ -0,0 +1,46 @@ +package edu.umd.cs.piccolox; + +import java.awt.event.KeyListener; + +import junit.framework.TestCase; +import edu.umd.cs.piccolo.PCanvas; + +public class PFrameTest extends TestCase { + private PFrame frame; + + public void testCanvasIsValidWithDefaultConstructor() { + PFrame frame = new PFrame() { + public void setVisible(boolean visible) { + // why oh why is PFrame visible by default + } + }; + PCanvas canvas = frame.getCanvas(); + assertNotNull(canvas); + assertNotNull(canvas.getLayer()); + assertNotNull(canvas.getCamera()); + assertSame(canvas.getLayer(), canvas.getCamera().getLayer(0)); + } + + public void testDefaultsToWindowed() { + PFrame frame = new PFrame() { + public void setVisible(boolean visible) { + // why oh why is PFrame visible by default + } + }; + assertFalse(frame.isFullScreenMode()); + } + + public void testFullScreenModeInstallsEscapeListeners() { + PFrame frame = new PFrame(); + frame.setFullScreenMode(true); + + + KeyListener[] listeners = frame.getCanvas().getKeyListeners(); + assertEquals(1, listeners.length); + + KeyListener listener = listeners[0]; + assertNotNull(listener); + frame.setVisible(false); + frame.setFullScreenMode(false); + } +} diff --git a/extras/src/test/java/edu/umd/cs/piccolox/nodes/PShadowTest.java b/extras/src/test/java/edu/umd/cs/piccolox/nodes/PShadowTest.java index 8739373..3b8e175 100755 --- a/extras/src/test/java/edu/umd/cs/piccolox/nodes/PShadowTest.java +++ b/extras/src/test/java/edu/umd/cs/piccolox/nodes/PShadowTest.java @@ -40,43 +40,26 @@ * Unit test for PShadow. */ public final class PShadowTest extends TestCase { + private static final int TEST_IMAGE_WIDTH = 25; + private static final int TEST_IMAGE_HEIGHT = 10; + private BufferedImage src; + private Color shadowPaint; - public void testConstructor() { - BufferedImage src = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB); + public void setUp() { + shadowPaint = new Color(20, 20, 20, 200); + src = new BufferedImage(TEST_IMAGE_WIDTH, TEST_IMAGE_HEIGHT, BufferedImage.TYPE_INT_ARGB); Paint srcPaint = new Color(255, 0, 0, 200); - Paint shadowPaint = new Color(20, 20, 20, 200); + Graphics2D g = src.createGraphics(); g.setPaint(srcPaint); g.drawRect(25, 25, 50, 50); g.dispose(); + } - for (int blurRadius = 1; blurRadius < 33; blurRadius += 4) { - PShadow shadowNode = new PShadow(src, shadowPaint, blurRadius); - assertNotNull(shadowNode); - assertEquals(src.getWidth() + 4 * blurRadius, shadowNode.getWidth(), 0.001d); - assertEquals(src.getHeight() + 4 * blurRadius, shadowNode.getHeight(), 0.001d); - } - - try { - new PShadow(null, shadowPaint, 4); - fail("ctr(null, ...) expected IllegalArgumentException"); - } - catch (IllegalArgumentException e) { - // expected - } - try { - new PShadow(src, shadowPaint, 0); - fail("ctr(..., -1) expected IllegalArgumentException"); - } - catch (IllegalArgumentException e) { - // expected - } - try { - new PShadow(src, shadowPaint, -1); - fail("ctr(..., -1) expected IllegalArgumentException"); - } - catch (IllegalArgumentException e) { - // expected - } + public void testShadowCreatesCorrectImageSize() { + PShadow shadowNode = new PShadow(src, shadowPaint, 4); + assertNotNull(shadowNode); + assertEquals(TEST_IMAGE_WIDTH + 16, shadowNode.getWidth(), 0.001d); + assertEquals(TEST_IMAGE_HEIGHT + 16, shadowNode.getHeight(), 0.001d); } } \ No newline at end of file diff --git a/extras/src/test/java/edu/umd/cs/piccolox/util/ShadowUtilsTest.java b/extras/src/test/java/edu/umd/cs/piccolox/util/ShadowUtilsTest.java index fb97617..407cb3c 100755 --- a/extras/src/test/java/edu/umd/cs/piccolox/util/ShadowUtilsTest.java +++ b/extras/src/test/java/edu/umd/cs/piccolox/util/ShadowUtilsTest.java @@ -31,52 +31,69 @@ import java.awt.Color; import java.awt.Graphics2D; import java.awt.Paint; - import java.awt.image.BufferedImage; +import edu.umd.cs.piccolox.nodes.PShadow; + import junit.framework.TestCase; /** * Unit test for ShadowUtils. */ public final class ShadowUtilsTest extends TestCase { + private static final int TEST_IMAGE_SIZE = 25; + private static final Paint shadowPaint = new Color(20, 20, 20, 200); + private BufferedImage src; - public void testCreateShadow() { - BufferedImage src = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB); + public void setUp() { + src = new BufferedImage(TEST_IMAGE_SIZE, TEST_IMAGE_SIZE, BufferedImage.TYPE_INT_ARGB); Paint srcPaint = new Color(255, 0, 0, 200); - Paint shadowPaint = new Color(20, 20, 20, 200); Graphics2D g = src.createGraphics(); g.setPaint(srcPaint); g.drawRect(25, 25, 50, 50); g.dispose(); + } - for (int blurRadius = 1; blurRadius < 33; blurRadius += 4) { - BufferedImage dest = ShadowUtils.createShadow(src, shadowPaint, blurRadius); - assertNotNull(dest); - assertEquals(src.getWidth() + 4 * blurRadius, dest.getWidth()); - assertEquals(src.getHeight() + 4 * blurRadius, dest.getHeight()); - } + public void testCreateShadowAcceptsTinyShadow() { + BufferedImage dest = ShadowUtils.createShadow(src, shadowPaint, 1); + assertNotNull(dest); + assertEquals(TEST_IMAGE_SIZE + 4, dest.getWidth()); + assertEquals(TEST_IMAGE_SIZE + 4, dest.getHeight()); + } - try { - ShadowUtils.createShadow(null, shadowPaint, 4); - fail("createShadow(null, ...) expected IllegalArgumentException"); - } - catch (IllegalArgumentException e) { - // expected - } + public void testCreateShadowAcceptsHugeShadow() { + BufferedImage dest = ShadowUtils.createShadow(src, shadowPaint, 25); + assertNotNull(dest); + assertEquals(TEST_IMAGE_SIZE + 100, dest.getWidth()); + assertEquals(TEST_IMAGE_SIZE + 100, dest.getHeight()); + } + + public void testNonPositiveBlurRadiusFails() { try { ShadowUtils.createShadow(src, shadowPaint, 0); - fail("createShadow(..., 0) expected IllegalArgumentException"); + fail("Non positive blur radius should fail"); } catch (IllegalArgumentException e) { // expected } + try { ShadowUtils.createShadow(src, shadowPaint, -1); - fail("createShadow(..., -1) expected IllegalArgumentException"); + fail("Non positive blur radius should fail"); } catch (IllegalArgumentException e) { // expected } } + + public void testConstructorDoesNotAcceptNullSrc() { + try { + ShadowUtils.createShadow(null, Color.BLACK, 4); + fail("ctr(null, ...) expected IllegalArgumentException"); + } + catch (IllegalArgumentException e) { + // expected + } + } + } \ No newline at end of file