Class ShapeOverlayAdapter

java.lang.Object
imagingbook.common.ij.overlay.ShapeOverlayAdapter

public class ShapeOverlayAdapter extends Object

This is an adapter for ImageJ's Overlay class to ease the insertion of AWT Shape elements. Shapes are only geometric descriptions but have no stroke width, color etc. attached. These are determined by ShapeOverlayAdapter's current state when insertion is done with addShape(Shape) (i.e., without explicit stroke information). Alternatively, the shape's stroke and color may be explicitly specified by calling addShape(Shape, ColoredStroke). When a shape is inserted into the associated overlay it is converted to an ImageJ ShapeRoi instance whose stroke/fill properties are set.

Similarly, text elements can be added using addText(double, double, String) or addText(double, double, String, Font, Color).

By default, shapes added to ShapeOverlayAdapter are automatically translated by 0.5 units in x/y direction, such that integer coordinates are shifted to pixel centers. This can be deactivated using constructor ShapeOverlayAdapter(Overlay, boolean, boolean).

Usage example 1 (adding shapes using stroke/color state):

 ImagePlus im = ...
 ShapeOverlayAdapter ola = new ShapeOverlayAdapter();
 ola.setStroke(new ColoredStroke(0.25, Color.blue));
 ola.addShape(new Line2D.Double(x1, y1, x2, y2));
 ...                    // add more shapes with same stroke
 ola.setStroke(new ColoredStroke(0.4, Color.pink));
 ola.addShape(new Line2D.Double(x3, y3, x4, y4));
 ...                    // add more shapes with same stroke
 im.setOverlay(ola.getOverlay());
 im.show();
 

Usage example 2 (adding shapes with element-wise stroke/color specification), mixed use of Overlay and ShapeOverlayAdapter:

 ImagePlus im = ...
 Overlay oly = new Overlay();
 ShapeOverlayAdapter ola = new ShapeOverlayAdapter(oly);
 ColoredStroke mystroke = new ColoredStroke(0.25, Color.blue);
 ola.addShape(new Line2D.Double(x1, y1, x2, y2), mystroke);
 ...                            // add more shapes
 im.setOverlay(oly);
 im.show();
 

Usage example 3 (adding text):

 ImagePlus im = ...
 ShapeOverlayAdapter ola = new ShapeOverlayAdapter();
 ola.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 8));
 ola.setTextColor(Color.green);
 ola.addText(x, y, "text using current font/color state");
 ola.addText(x, y, "text with explicit font and color", new Font(Font.SERIF, Font.PLAIN, 10), Color.black);
 ...
 im.setOverlay(ola.getOverlay());
 im.show();
 
Version:
2022/06/09
See Also: