Class ImageGraphics

java.lang.Object
imagingbook.common.image.ImageGraphics
All Implemented Interfaces:
AutoCloseable

public class ImageGraphics extends Object implements AutoCloseable

This class defines functionality for drawing anti-aliased "pixel" graphics in images of type ByteProcessor, ShortProcessor or ColorProcessor (there is no support for FloatProcessor). It uses the capabilities of AWT's Graphics2D class by drawing to a BufferedImage, which is a copy of the original image. After performing the drawing operations the BufferedImage is copied back to the original. Thus all operations possible on a Graphics2D instance are available, including the drawing of Shape objects with floating-point coordinates, arbitrary strokes and anti-aliasing, which is not available with ImageJ's built-in graphics operations (for class ImageProcessor).

Since drawing involves copying the image multiple times, graphic operations should be grouped for efficiency reasons. Here is an example for the intended form of use:

 ImageProcessor ip = ... ;   // some ByteProcessor, ShortProcessor or ColorProcessor
 try (ImageGraphics ig = new ImageGraphics(ip)) {
        ig.setColor(255);
        ig.setLineWidth(1.0);
        ig.drawLine(40, 100.5, 250, 101.5);
        ig.drawOval(230.6, 165.2, 150, 150);
        ...
 }

Note the use of double coordinates throughout. The original image (ip in the above example) is automatically updated at the end of the try() ... clause (by ImageGraphics implementing the AutoCloseable interface). The getGraphics2D() method exposes the underlying Graphics2D instance of the ImageGraphics object, which can then be used to perform arbitrary AWT graphic operations. Thus, the above example could alternatively be implemented as follows:

 ImageProcessor ip = ... ;   // some ByteProcessor, ShortProcessor or ColorProcessor
 try (ImageGraphics ig = new ImageGraphics(ip)) {
        Graphics2D g2 = ig.getGraphics2D();
        g2.setColor(Color.white);
        g2.setStroke(new BasicStroke(1.0f));
        g2.draw(new Line2D.Double(40, 100.5, 250, 101.5));
        g2.draw(new Ellipse2D.Double(230.6, 165.2, 150, 150));
        ...
 }

This class also defines several convenience methods for drawing shapes with floating-point (double) coordinates, as well as for setting colors and stroke parameters. If intermediate updates are needed (e.g., for animations), the update() method can be invoked any time.

Version:
2020-01-07
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor.
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    void
    drawLine(double x1, double y1, double x2, double y2)
    Draws a straight line segment specified with double coordinate values (convenience method).
    void
    drawOval(double x, double y, double w, double h)
    Draws an ellipse specified with double coordinate values (convenience method).
    void
    drawPolygon(Point2D... points)
    Draws a closed polygon specified by a sequence of Point2D objects with arbitrary coordinate values (convenience method).
    void
    drawRectangle(double x, double y, double w, double h)
    Draws a rectangle specified with double coordinate values (convenience method).
    Returns the underlying Graphics2D object, which can be used to perform arbitrary graphics operations.
    void
    setAntialiasing(boolean onoff)
    Turn anti-aliasing on/off for this ImageGraphics instance (turned on by default).
    void
    setColor(int gray)
    Sets this graphics context's current color to the specified (gray) color, with RGB = (gray, gray, gray).
    void
    setColor(Color color)
    Sets this graphics context's current color to the specified color.
    void
    Sets the end cap style of the current stroke to "BUTT".
    void
    Sets the end cap style of the current stroke to "ROUND".
    void
    Sets the end cap style of the current stroke to "SQUARE".
    void
    Sets the line segment join style of the current stroke to "BEVEL".
    void
    Sets the line segment join style of the current stroke to "MITER".
    void
    Sets the line segment join style of the current stroke to "ROUND".
    void
    setLineWidth(double width)
    Sets the line width of the current stroke.
    void
    Sets the stroke to be used for all subsequent graphics operations.
    void
    Forces the image to be updated by copying the (modified) BufferedImage back to the input image.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • setAntialiasing

      public void setAntialiasing(boolean onoff)
      Turn anti-aliasing on/off for this ImageGraphics instance (turned on by default). The new setting will only affect subsequent graphics operations.
      Parameters:
      onoff - set true to turn on
    • getGraphics2D

      Returns the underlying Graphics2D object, which can be used to perform arbitrary graphics operations.
      Returns:
      the Graphics2D object
    • update

      public void update()
      Forces the image to be updated by copying the (modified) BufferedImage back to the input image. There is usually no need to call this (expensive) method explicitly. It is called automatically and only once at end of the try() ... clause, as described in the ImageGraphics class documentation above.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • drawLine

      public void drawLine(double x1, double y1, double x2, double y2)
      Draws a straight line segment specified with double coordinate values (convenience method).
      Parameters:
      x1 - x-coordinate of start point
      y1 - y-coordinate of start point
      x2 - x-coordinate of end point
      y2 - y-coordinate of end point
      See Also:
    • drawOval

      public void drawOval(double x, double y, double w, double h)
      Draws an ellipse specified with double coordinate values (convenience method).
      Parameters:
      x - x-coordinate of the upper-left corner of the framing rectangle
      y - y-coordinate of the upper-left corner of the framing rectangle
      w - width
      h - height
      See Also:
    • drawRectangle

      public void drawRectangle(double x, double y, double w, double h)
      Draws a rectangle specified with double coordinate values (convenience method).
      Parameters:
      x - x-coordinate of the upper-left corner
      y - y-coordinate of the upper-left corner
      w - width
      h - height
      See Also:
    • drawPolygon

      public void drawPolygon(Point2D... points)
      Draws a closed polygon specified by a sequence of Point2D objects with arbitrary coordinate values (convenience method). Note that the the polygon is automatically closed, i.e., N+1 segments are drawn if the number of given points is N.
      Parameters:
      points - a sequence of 2D points
      See Also:
    • setColor

      public void setColor(Color color)
      Sets this graphics context's current color to the specified color. All subsequent graphics operations using this graphics context use this specified color.
      Parameters:
      color - the new rendering color
      See Also:
    • setColor

      public void setColor(int gray)
      Sets this graphics context's current color to the specified (gray) color, with RGB = (gray, gray, gray).
      Parameters:
      gray - the gray value
    • setStroke

      public void setStroke(BasicStroke stroke)
      Sets the stroke to be used for all subsequent graphics operations.
      Parameters:
      stroke - a BasicStroke instance
      See Also:
    • setLineWidth

      public void setLineWidth(double width)
      Sets the line width of the current stroke. All other stroke properties remain unchanged.
      Parameters:
      width - the line width
      See Also:
    • setEndCapButt

      public void setEndCapButt()
      Sets the end cap style of the current stroke to "BUTT". All other stroke properties remain unchanged.
      See Also:
    • setEndCapRound

      public void setEndCapRound()
      Sets the end cap style of the current stroke to "ROUND". All other stroke properties remain unchanged.
      See Also:
    • setEndCapSquare

      public void setEndCapSquare()
      Sets the end cap style of the current stroke to "SQUARE". All other stroke properties remain unchanged.
      See Also:
    • setLineJoinBevel

      public void setLineJoinBevel()
      Sets the line segment join style of the current stroke to "BEVEL". All other stroke properties remain unchanged.
      See Also:
    • setLineJoinMiter

      public void setLineJoinMiter()
      Sets the line segment join style of the current stroke to "MITER". All other stroke properties remain unchanged.
      See Also:
    • setLineJoinRound

      public void setLineJoinRound()
      Sets the line segment join style of the current stroke to "ROUND". All other stroke properties remain unchanged.
      See Also: