Class PImage

java.lang.Object
edu.umd.cs.piccolo.PNode
edu.umd.cs.piccolo.nodes.PImage
All Implemented Interfaces:
Printable, Serializable, Cloneable

public class PImage extends PNode
PImage is a wrapper around a java.awt.Image. If this node is copied or serialized that image will be converted into a BufferedImage if it is not already one.

Version:
1.0
Author:
Jesse Grosjean
See Also:
  • Field Details

    • PROPERTY_IMAGE

      public static final String PROPERTY_IMAGE
      The property name that identifies a change of this node's image (see getImage). Both old and new value will be set correctly to Image objects in any property change event.
      See Also:
    • PROPERTY_CODE_IMAGE

      public static final int PROPERTY_CODE_IMAGE
      The property code that identifies a change of this node's image (see getImage). Both old and new value will be set correctly to Image objects in any property change event.
      See Also:
  • Constructor Details

    • PImage

      public PImage()
      Constructs a PImage without a java.awt.Image attached.
    • PImage

      public PImage(String fileName)
      Construct a new PImage by loading the given fileName and wrapping the resulting java.awt.Image.
      Parameters:
      fileName - of the image to wrap
    • PImage

      public PImage(Image image)
      Construct a new PImage wrapping the given java.awt.Image.
      Parameters:
      image - image that this PImage will wrap
    • PImage

      public PImage(URL url)
      Construct a new PImage by loading the given url and wrapping the resulting java.awt.Image. If the url is null, create an empty PImage; this behaviour is useful when fetching resources that may be missing.
      Parameters:
      url - URL of image resource to load
  • Method Details

    • getImage

      public Image getImage()
      Returns the image that is shown by this node, or null if none.
      Returns:
      java.awt.Image being wrapped by this node
    • setImage

      public void setImage(String fileName)
      Set the image that is wrapped by this PImage node. This method will also load the image using a MediaTracker before returning.
      Parameters:
      fileName - file to be wrapped by this PImage
    • setImage

      public void setImage(Image newImage)
      Set the image that is wrapped by this PImage node. This method will also load the image using a MediaTracker before returning.
      Parameters:
      newImage - image to be displayed by this PImage
    • paint

      protected void paint(PPaintContext paintContext)
      Renders the wrapped Image, stretching it appropriately if the bounds of this PImage doesn't match the bounds of the image.
      Overrides:
      paint in class PNode
      Parameters:
      paintContext - context into which the rendering will occur
    • toBufferedImage

      public static BufferedImage toBufferedImage(Image image, boolean alwaysCreateCopy)
      Converts the provided image into a BufferedImage. If alwaysCreateCopy is false then if the image is already a buffered image it will not be copied and instead the original image will just be returned.
      Parameters:
      image - the image to be converted
      alwaysCreateCopy - if true, will create a copy even if image is already a BufferedImage
      Returns:
      a BufferedImage equivalent to the Image provided