Author: allain.lalonde
Date: Sat Jul 18 05:27:47 2009
New Revision: 510

Modified:
     
piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/nodes/PImage.java
     
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/nodes/PImageTest.java

Log:
Unit Testing for PImage and removed a little cruft

Modified:  
piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/nodes/PImage.java
==============================================================================
---  
piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/nodes/PImage.java    
 
(original)
+++  
piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/nodes/PImage.java    
 
Sat Jul 18 05:27:47 2009
@@ -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

Modified:  
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/nodes/PImageTest.java
==============================================================================
---  
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/nodes/PImageTest.java
         
(original)
+++  
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/nodes/PImageTest.java
         
Sat Jul 18 05:27:47 2009
@@ -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 junit.framework.TestCase;
+import javax.imageio.ImageIO;

-import edu.umd.cs.piccolo.nodes.PImage;
+import junit.framework.TestCase;
  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);
+    }
+
  }

--~--~---------~--~----~------------~-------~--~----~
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to