- 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 TypeMethodDescriptionvoid
close()
void
drawLine
(double x1, double y1, double x2, double y2) Draws a straight line segment specified withdouble
coordinate values (convenience method).void
drawOval
(double x, double y, double w, double h) Draws an ellipse specified withdouble
coordinate values (convenience method).void
drawPolygon
(Point2D... points) Draws a closed polygon specified by a sequence ofPoint2D
objects with arbitrary coordinate values (convenience method).void
drawRectangle
(double x, double y, double w, double h) Draws a rectangle specified withdouble
coordinate values (convenience method).Returns the underlyingGraphics2D
object, which can be used to perform arbitrary graphics operations.void
setAntialiasing
(boolean onoff) Turn anti-aliasing on/off for thisImageGraphics
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
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
setStroke
(BasicStroke stroke) Sets the stroke to be used for all subsequent graphics operations.void
update()
Forces the image to be updated by copying the (modified)BufferedImage
back to the input image.
-
Constructor Details
-
ImageGraphics
Constructor. The supplied image must be of typeByteProcessor
,ShortProcessor
orColorProcessor
. AnIllegalArgumentException
is thrown for images of typeFloatProcessor
.- Parameters:
ip
- image to draw on
-
ImageGraphics
Constructor. The supplied image must be of typeByteProcessor
,ShortProcessor
orColorProcessor
. AnIllegalArgumentException
is 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 thisImageGraphics
instance (turned on by default). The new setting will only affect subsequent graphics operations.- Parameters:
onoff
- set true to turn on
-
getGraphics2D
Returns the underlyingGraphics2D
object, which can be used to perform arbitrary graphics operations.- Returns:
- the
Graphics2D
object
-
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 thetry() ...
clause, as described in theImageGraphics
class documentation above. -
close
- Specified by:
close
in interfaceAutoCloseable
-
drawLine
Draws a straight line segment specified withdouble
coordinate 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 withdouble
coordinate 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 withdouble
coordinate 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 ofPoint2D
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
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
- aBasicStroke
instance- 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:
-