- All Implemented Interfaces:
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
ConstructorsConstructorDescriptionConstructor.ImageGraphics(ImageProcessor ip, Color color, BasicStroke stroke) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voiddrawLine(double x1, double y1, double x2, double y2) Draws a straight line segment specified withdoublecoordinate values (convenience method).voiddrawOval(double x, double y, double w, double h) Draws an ellipse specified withdoublecoordinate values (convenience method).voiddrawPolygon(Point2D... points) Draws a closed polygon specified by a sequence ofPoint2Dobjects with arbitrary coordinate values (convenience method).voiddrawRectangle(double x, double y, double w, double h) Draws a rectangle specified withdoublecoordinate values (convenience method).Returns the underlyingGraphics2Dobject, which can be used to perform arbitrary graphics operations.voidsetAntialiasing(boolean onoff) Turn anti-aliasing on/off for thisImageGraphicsinstance (turned on by default).voidsetColor(int gray) Sets this graphics context's current color to the specified (gray) color, with RGB = (gray, gray, gray).voidSets this graphics context's current color to the specified color.voidSets the end cap style of the current stroke to "BUTT".voidSets the end cap style of the current stroke to "ROUND".voidSets the end cap style of the current stroke to "SQUARE".voidSets the line segment join style of the current stroke to "BEVEL".voidSets the line segment join style of the current stroke to "MITER".voidSets the line segment join style of the current stroke to "ROUND".voidsetLineWidth(double width) Sets the line width of the current stroke.voidsetStroke(BasicStroke stroke) Sets the stroke to be used for all subsequent graphics operations.voidupdate()Forces the image to be updated by copying the (modified)BufferedImageback to the input image.
-
Constructor Details
-
ImageGraphics
Constructor. The supplied image must be of typeByteProcessor,ShortProcessororColorProcessor. AnIllegalArgumentExceptionis thrown for images of typeFloatProcessor.- Parameters:
ip- image to draw on
-
ImageGraphics
Constructor. The supplied image must be of typeByteProcessor,ShortProcessororColorProcessor. AnIllegalArgumentExceptionis thrown for images of typeFloatProcessor.- Parameters:
ip- image to draw oncolor- the initial drawing colorstroke- the initial stroke
-
-
Method Details
-
setAntialiasing
Turn anti-aliasing on/off for thisImageGraphicsinstance (turned on by default). The new setting will only affect subsequent graphics operations.- Parameters:
onoff- set true to turn on
-
getGraphics2D
Returns the underlyingGraphics2Dobject, which can be used to perform arbitrary graphics operations.- Returns:
- the
Graphics2Dobject
-
update
Forces the image to be updated by copying the (modified)BufferedImageback 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 thetry() ...clause, as described in theImageGraphicsclass documentation above. -
close
- Specified by:
closein interfaceAutoCloseable
-
drawLine
Draws a straight line segment specified withdoublecoordinate values (convenience method).- Parameters:
x1- x-coordinate of start pointy1- y-coordinate of start pointx2- x-coordinate of end pointy2- y-coordinate of end point- See Also:
-
drawOval
Draws an ellipse specified withdoublecoordinate values (convenience method).- Parameters:
x- x-coordinate of the upper-left corner of the framing rectangley- y-coordinate of the upper-left corner of the framing rectanglew- widthh- height- See Also:
-
drawRectangle
Draws a rectangle specified withdoublecoordinate values (convenience method).- Parameters:
x- x-coordinate of the upper-left cornery- y-coordinate of the upper-left cornerw- widthh- height- See Also:
-
drawPolygon
Draws a closed polygon specified by a sequence ofPoint2Dobjects 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
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
Sets this graphics context's current color to the specified (gray) color, with RGB = (gray, gray, gray).- Parameters:
gray- the gray value
-
setStroke
Sets the stroke to be used for all subsequent graphics operations.- Parameters:
stroke- aBasicStrokeinstance- See Also:
-
setLineWidth
Sets the line width of the current stroke. All other stroke properties remain unchanged.- Parameters:
width- the line width- See Also:
-
setEndCapButt
Sets the end cap style of the current stroke to "BUTT". All other stroke properties remain unchanged.- See Also:
-
setEndCapRound
Sets the end cap style of the current stroke to "ROUND". All other stroke properties remain unchanged.- See Also:
-
setEndCapSquare
Sets the end cap style of the current stroke to "SQUARE". All other stroke properties remain unchanged.- See Also:
-
setLineJoinBevel
Sets the line segment join style of the current stroke to "BEVEL". All other stroke properties remain unchanged.- See Also:
-
setLineJoinMiter
Sets the line segment join style of the current stroke to "MITER". All other stroke properties remain unchanged.- See Also:
-
setLineJoinRound
Sets the line segment join style of the current stroke to "ROUND". All other stroke properties remain unchanged.- See Also:
-