diff --git a/core/src/main/java/edu/umd/cs/piccolo/POffscreenCanvas.java b/core/src/main/java/edu/umd/cs/piccolo/POffscreenCanvas.java index bf37269..cb8aa9b 100644 --- a/core/src/main/java/edu/umd/cs/piccolo/POffscreenCanvas.java +++ b/core/src/main/java/edu/umd/cs/piccolo/POffscreenCanvas.java @@ -55,12 +55,13 @@ /** Render quality. */ private int renderQuality = DEFAULT_RENDER_QUALITY; - /** Whether this node's background color should be drawn when rendering. */ + /** True if this offscreen canvas is opaque. */ private boolean opaque; - /** Background color to paint before the contents of the scene are rendered. */ + /** Background color for this offscreen canvas. */ private Color backgroundColor; + /** * Create a new offscreen canvas the specified width and height. * @@ -81,11 +82,11 @@ backgroundColor = null; } + /** * Render this offscreen canvas to the specified graphics. - * - * @param graphics graphics to render this offscreen canvas to, must not be - * null + * + * @param graphics graphics to render this offscreen canvas to, must not be null */ public void render(final Graphics2D graphics) { if (graphics == null) { @@ -94,7 +95,7 @@ if (opaque && backgroundColor != null) { graphics.setBackground(backgroundColor); - graphics.clearRect(0, 0, (int)bounds.getWidth(), (int)bounds.getHeight()); + graphics.clearRect(0, 0, (int) bounds.getWidth(), (int) bounds.getHeight()); } final PPaintContext paintContext = new PPaintContext(graphics); @@ -181,27 +182,54 @@ } /** - * Return the root of the scene this POffscreenCanvas is viewing. + * Return the root node of the scene graph for this offscreen canvas. The + * root node will be null if the camera for this offscreen canvas is null. * - * @return root element of the scene being viewed + * @return the root node of the scene graph for this offscreen canvas */ public PRoot getRoot() { - return camera.getRoot(); + return camera == null ? null : camera.getRoot(); } + /** + * Return true if this offscreen canvas is opaque. Defaults to false. + * + * @return true if this offscreen canvas is opaque + */ public boolean isOpaque() { return opaque; } - public void setOpaque(boolean opaque) { + /** + * Set to true if this offscreen canvas is opaque. + * + * @param opaque true if this offscreen canvas is opaque + */ + public void setOpaque(final boolean opaque) { this.opaque = opaque; } + /** + * Return the background color for this offscreen canvas. If this + * offscreen canvas is opaque, the background color will be painted + * before the contents of the scene are rendered. + * + * @see #isOpaque + * @return the background color for this offscreen canvas + */ public Color getBackground() { return backgroundColor; } - public void setBackground(Color backgroundColor) { + /** + * Set the background color for this offscreen canvas to backgroundColor. + * If this offscreen canvas is opaque, the background color will be painted + * before the contents of the scene are rendered. + * + * @see #isOpaque + * @param backgroundColor background color for this offscreen canvas + */ + public void setBackground(final Color backgroundColor) { this.backgroundColor = backgroundColor; } } \ No newline at end of file diff --git a/core/src/test/java/edu/umd/cs/piccolo/POffscreenCanvasTest.java b/core/src/test/java/edu/umd/cs/piccolo/POffscreenCanvasTest.java index 6f97b14..8528dd2 100644 --- a/core/src/test/java/edu/umd/cs/piccolo/POffscreenCanvasTest.java +++ b/core/src/test/java/edu/umd/cs/piccolo/POffscreenCanvasTest.java @@ -36,6 +36,7 @@ import junit.framework.TestCase; import edu.umd.cs.piccolo.PCamera; import edu.umd.cs.piccolo.nodes.PPath; +import edu.umd.cs.piccolo.util.PBounds; import edu.umd.cs.piccolo.util.PPaintContext; /** @@ -104,6 +105,34 @@ } } + public void testRenderEmptyOpaqueNullBackgroundColor() { + final POffscreenCanvas canvas = new POffscreenCanvas(100, 200); + final BufferedImage image = new BufferedImage(100, 200, BufferedImage.TYPE_INT_ARGB); + final Graphics2D graphics = image.createGraphics(); + canvas.setOpaque(true); + canvas.render(graphics); + for (int x = 0; x < 100; x++) { + for (int y = 0; y < 200; y++) { + assertEquals(0, image.getRGB(x, y)); + } + } + } + + public void testRenderEmptyOpaque() { + final POffscreenCanvas canvas = new POffscreenCanvas(100, 200); + final BufferedImage image = new BufferedImage(100, 200, BufferedImage.TYPE_INT_ARGB); + final Graphics2D graphics = image.createGraphics(); + canvas.setOpaque(true); + canvas.setBackground(Color.RED); + canvas.render(graphics); + for (int x = 0; x < 100; x++) { + for (int y = 0; y < 200; y++) { + // red pixel, RGBA is 255, 0, 0, 255 + assertEquals(-65536, image.getRGB(x, y)); + } + } + } + public void testRenderFull() { final POffscreenCanvas canvas = new POffscreenCanvas(100, 200); final PPath rect = PPath.createRectangle(0.0f, 0.0f, 200.0f, 300.0f); @@ -149,6 +178,12 @@ } } + public void testRepaint() { + final POffscreenCanvas canvas = new POffscreenCanvas(100, 200); + canvas.repaint(null); + canvas.repaint(new PBounds(0.0, 0.0, 50.0, 50.0)); + } + public void testPaintImmediately() { final POffscreenCanvas canvas = new POffscreenCanvas(100, 200); canvas.paintImmediately(); @@ -170,4 +205,29 @@ canvas.setInteracting(true); canvas.setInteracting(false); } + + public void testRoot() { + final POffscreenCanvas canvas = new POffscreenCanvas(100, 200); + assertNotNull(canvas.getRoot()); + } + + public void testRootIsNullWhenCameraIsNull() { + final POffscreenCanvas canvas = new POffscreenCanvas(100, 200); + canvas.setCamera(null); + assertEquals(null, canvas.getRoot()); + } + + public void testOpaque() { + final POffscreenCanvas canvas = new POffscreenCanvas(100, 200); + assertFalse(canvas.isOpaque()); + canvas.setOpaque(true); + assertTrue(canvas.isOpaque()); + } + + public void testBackground() { + final POffscreenCanvas canvas = new POffscreenCanvas(100, 200); + assertEquals(null, canvas.getBackground()); + canvas.setBackground(Color.RED); + assertEquals(Color.RED, canvas.getBackground()); + } } \ No newline at end of file