Class PixelDrawing
- java.lang.Object
-
- uk.ac.starlink.ttools.plot2.layer.PixelDrawing
-
- All Implemented Interfaces:
PixerFactory
public class PixelDrawing extends java.lang.Object implements PixerFactory
Provides drawing primitives on a pixel map. This is a bit like aGraphics
, but renders only to a one-bit-per-pixel bitmap. After drawings have been done, the object can be used as aPixerFactory
to get a list of the pixels which have been hit at least once by one of the drawing methods called during the life of the object. Pixels will not be repeated in this list.The drawing methods are intended to be as efficient as possible. Bounds checking is done, so it is generally not problematic (or inefficient) to attempt drawing operations with coordinates far outside the drawing's range.
- Since:
- 13 Sep 2021
- Author:
- Mark Taylor
-
-
Constructor Summary
Constructors Constructor Description PixelDrawing(int x0, int y0, int width, int height)
Constructs a drawing with bounds given explicitly.PixelDrawing(java.awt.Rectangle bounds)
Constructs a drawing with bounds given by a rectangle.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addPixel(int x, int y)
Adds a single pixel to the list of pixels which have been plotted if it is within this drawing's bounds.void
addPixelUnchecked(int x, int y)
Adds a single pixel to the list of pixels which have been plotted without bounds checking.boolean
contains(int x, int y)
Indicates whether a given position is within the bounds of this drawing.Pixer
createPixer()
Returns a Pixer that interrogates this drawing's bitmap.void
drawEllipse(int x0, int y0, int ax, int ay, int bx, int by)
Draws the outline of an ellipse with no restrictions on the alignment of its axes.void
drawLine(int x0, int y0, int x1, int y1)
Draws a straight line between two points.void
drawOval(int x, int y, int width, int height)
Draws the outline of an ellipse with horizontal/vertical axes.void
fill(java.awt.Shape shape)
Deprecated.may be slow for large shapes; use fillPolygon if possiblevoid
fillEllipse(int x0, int y0, int ax, int ay, int bx, int by)
Fills an ellipse with no restrictions on the alignment of its axes.void
fillOval(int x, int y, int width, int height)
Fills an ellipse with horizontal/vertical axes.void
fillPolygon(int[] xs, int[] ys, int np)
Draws a filled polygon on this drawing.void
fillRect(int x, int y, int width, int height)
Fills a rectangle.int
getMaxX()
Returns the highest X value from each created pixer.int
getMaxY()
Returns the highest Y value from each created pixer.int
getMinX()
Returns the lowest X value from each created pixer.int
getMinY()
Returns the lowest Y value from each created pixer.int
getPixelCount()
Returns the number of pixels over which each created pixer iterates.java.util.BitSet
getPixels()
Returns the pixel mask containing the data for this drawing.
-
-
-
Constructor Detail
-
PixelDrawing
public PixelDrawing(java.awt.Rectangle bounds)
Constructs a drawing with bounds given by a rectangle.- Parameters:
bounds
- rectangle giving the region in which pixels may be plotted
-
PixelDrawing
public PixelDrawing(int x0, int y0, int width, int height)
Constructs a drawing with bounds given explicitly.- Parameters:
x0
- lower bound of X coordinatey0
- upper bound of Y coordinatewidth
- extent in X directionheight
- extent in Y direction
-
-
Method Detail
-
addPixel
public void addPixel(int x, int y)
Adds a single pixel to the list of pixels which have been plotted if it is within this drawing's bounds. Calling it with coordinates which have already been plotted, or which are outside this drawing's bounds, has no effect.- Parameters:
x
- X coordinatey
- Y coordinate
-
addPixelUnchecked
public void addPixelUnchecked(int x, int y)
Adds a single pixel to the list of pixels which have been plotted without bounds checking. An assertion is made that the coordinates are outside of this drawing's bounds.- Parameters:
x
- X coordinate, must be within this drawing's X boundsy
- Y coordinate, must be within this drawing's Y bounds
-
drawLine
public void drawLine(int x0, int y0, int x1, int y1)
Draws a straight line between two points.- Parameters:
x0
- X coordinate of first pointy0
- Y coordinate of first pointx1
- X coordinate of second pointy1
- Y coordinate of second point- See Also:
Graphics.drawLine(int, int, int, int)
-
fillRect
public void fillRect(int x, int y, int width, int height)
Fills a rectangle.- Parameters:
x
- X coordinate of top left cornery
- Y coordinate of top left cornerwidth
- widthheight
- height- See Also:
Graphics.fillRect(int, int, int, int)
-
drawOval
public void drawOval(int x, int y, int width, int height)
Draws the outline of an ellipse with horizontal/vertical axes.- Parameters:
x
- X coordinate of top left corner of enclosing rectangley
- Y coordinate of top left corner of enclosing rectanglewidth
- width of enclosing rectangleheight
- height of enclosing rectangle- See Also:
Graphics.drawOval(int, int, int, int)
-
fillOval
public void fillOval(int x, int y, int width, int height)
Fills an ellipse with horizontal/vertical axes.- Parameters:
x
- X coordinate of top left corner of enclosing rectangley
- Y coordinate of top left corner of enclosing rectanglewidth
- width of enclosing rectangleheight
- height of enclosing rectangle- See Also:
Graphics.drawOval(int, int, int, int)
-
drawEllipse
public void drawEllipse(int x0, int y0, int ax, int ay, int bx, int by)
Draws the outline of an ellipse with no restrictions on the alignment of its axes.- Parameters:
x0
- X coordinate of ellipse centrey0
- Y coordinate of ellipse centreax
- X component of semi-major (or -minor) axisay
- Y component of semi-major (or -minor) axisbx
- X component of semi-minor (or -major) axisby
- Y component of semi-minor (Or -major) axis
-
fillEllipse
public void fillEllipse(int x0, int y0, int ax, int ay, int bx, int by)
Fills an ellipse with no restrictions on the alignment of its axes.- Parameters:
x0
- X coordinate of ellipse centrey0
- Y coordinate of ellipse centreax
- X component of semi-major (or -minor) axisay
- Y component of semi-major (or -minor) axisbx
- X component of semi-minor (or -major) axisby
- Y component of semi-minor (Or -major) axis
-
fillPolygon
public void fillPolygon(int[] xs, int[] ys, int np)
Draws a filled polygon on this drawing.- Parameters:
xs
- X coordinates of verticesys
- Y coordinates of verticesnp
- number of vertices
-
fill
@Deprecated public void fill(java.awt.Shape shape)
Deprecated.may be slow for large shapes; use fillPolygon if possibleFills an arbitrary shape.- Parameters:
shape
- shape to fill- See Also:
Graphics2D.fill(java.awt.Shape)
-
contains
public boolean contains(int x, int y)
Indicates whether a given position is within the bounds of this drawing.- Parameters:
x
- X coordinatey
- Y coordinate- Returns:
- true iff (x,y) is in bounds
-
getPixels
public java.util.BitSet getPixels()
Returns the pixel mask containing the data for this drawing. The coordinate mapping is not currently defined, but the return value of this method for two PixelDrawings with the same bounds will have the same bit-to-pixel correspondance; a set bit corresponds to a painted pixel. This value may be modified in place.- Returns:
- bit map giving pixel data
-
getMinX
public int getMinX()
Description copied from interface:PixerFactory
Returns the lowest X value from each created pixer.- Specified by:
getMinX
in interfacePixerFactory
- Returns:
- minimum X coordinate
-
getMaxX
public int getMaxX()
Description copied from interface:PixerFactory
Returns the highest X value from each created pixer.- Specified by:
getMaxX
in interfacePixerFactory
- Returns:
- maximum X coordinate
-
getMinY
public int getMinY()
Description copied from interface:PixerFactory
Returns the lowest Y value from each created pixer.- Specified by:
getMinY
in interfacePixerFactory
- Returns:
- minimum Y coordinate
-
getMaxY
public int getMaxY()
Description copied from interface:PixerFactory
Returns the highest Y value from each created pixer.- Specified by:
getMaxY
in interfacePixerFactory
- Returns:
- maximum Y coordinate
-
getPixelCount
public int getPixelCount()
Description copied from interface:PixerFactory
Returns the number of pixels over which each created pixer iterates.- Specified by:
getPixelCount
in interfacePixerFactory
- Returns:
- pixel count
-
createPixer
public Pixer createPixer()
Returns a Pixer that interrogates this drawing's bitmap. May not be maximally efficient if it will be called many times.- Specified by:
createPixer
in interfacePixerFactory
- Returns:
- pixer
-
-