diff --git a/core/src/main/java/edu/umd/cs/piccolo/nodes/PImage.java b/core/src/main/java/edu/umd/cs/piccolo/nodes/PImage.java index dbc49f8..c278032 100644 --- a/core/src/main/java/edu/umd/cs/piccolo/nodes/PImage.java +++ b/core/src/main/java/edu/umd/cs/piccolo/nodes/PImage.java @@ -151,32 +151,33 @@ if (image != null) { setBounds(0, 0, getImage().getWidth(null), getImage().getHeight(null)); invalidatePaint(); - } - else { - image = null; - } + } firePropertyChange(PROPERTY_CODE_IMAGE, PROPERTY_IMAGE, old, image); } protected void paint(PPaintContext paintContext) { - if (getImage() != null) { - double iw = image.getWidth(null); - double ih = image.getHeight(null); - PBounds b = getBoundsReference(); - Graphics2D g2 = paintContext.getGraphics(); - - if (b.x != 0 || b.y != 0 || b.width != iw || b.height != ih) { - g2.translate(b.x, b.y); - g2.scale(b.width / iw, b.height / ih); - g2.drawImage(image, 0, 0, null); - g2.scale(iw / b.width, ih / b.height); - g2.translate(-b.x, -b.y); - } - else { - g2.drawImage(image, 0, 0, null); - } + if (getImage() == null) { + return; } + + double iw = image.getWidth(null); + double ih = image.getHeight(null); + + PBounds b = getBoundsReference(); + Graphics2D g2 = paintContext.getGraphics(); + + if (b.x != 0 || b.y != 0 || b.width != iw || b.height != ih) { + g2.translate(b.x, b.y); + g2.scale(b.width / iw, b.height / ih); + g2.drawImage(image, 0, 0, null); + g2.scale(iw / b.width, ih / b.height); + g2.translate(-b.x, -b.y); + } + else { + g2.drawImage(image, 0, 0, null); + } + } // **************************************************************** @@ -209,27 +210,23 @@ * returned. */ public static BufferedImage toBufferedImage(Image image, boolean alwaysCreateCopy) { - if (image == null) + if (image == null) { return null; + } if (!alwaysCreateCopy && image instanceof BufferedImage) { return (BufferedImage) image; } - else { - GraphicsConfiguration graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment() - .getDefaultScreenDevice().getDefaultConfiguration(); - BufferedImage result = graphicsConfiguration.createCompatibleImage(image.getWidth(null), image - .getHeight(null)); - Graphics2D g2 = result.createGraphics(); - g2.drawImage(image, 0, 0, null); - g2.dispose(); - return result; - } - } - // **************************************************************** - // Debugging - methods for debugging - // **************************************************************** + GraphicsConfiguration graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment() + .getDefaultScreenDevice().getDefaultConfiguration(); + BufferedImage result = graphicsConfiguration.createCompatibleImage(image.getWidth(null), image.getHeight(null)); + Graphics2D g2 = result.createGraphics(); + g2.drawImage(image, 0, 0, null); + g2.dispose(); + return result; + + } /** * Returns a string representing the state of this node. This method is diff --git a/core/src/test/java/edu/umd/cs/piccolo/nodes/PImageTest.java b/core/src/test/java/edu/umd/cs/piccolo/nodes/PImageTest.java index a1acb03..b3a49a7 100644 --- a/core/src/test/java/edu/umd/cs/piccolo/nodes/PImageTest.java +++ b/core/src/test/java/edu/umd/cs/piccolo/nodes/PImageTest.java @@ -28,12 +28,17 @@ */ package edu.umd.cs.piccolo.nodes; +import java.awt.Graphics; +import java.awt.Graphics2D; import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; import junit.framework.TestCase; - -import edu.umd.cs.piccolo.nodes.PImage; import edu.umd.cs.piccolo.util.PBounds; +import edu.umd.cs.piccolo.util.PPaintContext; public class PImageTest extends TestCase { @@ -52,4 +57,54 @@ PImage aNode = new PImage(new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB)); assertNotNull(aNode.toString()); } + + public void testToBufferedImageReturnsCopyIfToldTo() { + BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); + BufferedImage copy = PImage.toBufferedImage(img, true); + assertNotSame(img, copy); + } + + public void testCanBeCreatedFromFile() throws IOException { + BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); + File imgFile = File.createTempFile("test", ".jpeg"); + imgFile.deleteOnExit(); + ImageIO.write(img, "JPEG", imgFile); + + PImage imageNode = new PImage(imgFile.getAbsolutePath()); + assertEquals(100, imageNode.getImage().getWidth(null)); + assertEquals(100, imageNode.getImage().getHeight(null)); + } + + public void testCanBeCreatedFromUrl() throws IOException { + BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); + File imgFile = File.createTempFile("test", ".jpeg"); + imgFile.deleteOnExit(); + ImageIO.write(img, "JPEG", imgFile); + + PImage imageNode = new PImage(imgFile.toURL()); + assertEquals(100, imageNode.getImage().getWidth(null)); + assertEquals(100, imageNode.getImage().getHeight(null)); + } + + public void testImageCanBeSetFromFile() throws IOException { + BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); + File imgFile = File.createTempFile("test", ".jpeg"); + imgFile.deleteOnExit(); + ImageIO.write(img, "JPEG", imgFile); + + PImage imageNode = new PImage(); + imageNode.setImage(imgFile.getAbsolutePath()); + assertEquals(100, imageNode.getImage().getWidth(null)); + assertEquals(100, imageNode.getImage().getHeight(null)); + } + + public void testPaintAnEmptyImageNodeDoesNothing() { + PImage imageNode = new PImage(); + + BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); + + PPaintContext paintContext = new PPaintContext(img.createGraphics()); + imageNode.paint(paintContext); + } + }