public class Painter extends Object
Painter
class provides static methods to draw basic shapes such as lines, rectangles, circles,
characters and images.
Every method uses the color, the translation and the clip which are set in the given GraphicsContext
. The
background color and the ellipsis width are only used by a few methods.
Modifier and Type | Method and Description |
---|---|
static void |
drawChar(GraphicsContext gc,
char character,
Font font,
int x,
int y)
Draws a character.
|
static void |
drawCircle(GraphicsContext gc,
int x,
int y,
int diameter)
Draws a circle.
|
static void |
drawCircleArc(GraphicsContext gc,
int x,
int y,
int diameter,
float startAngle,
float arcAngle)
Draws a circle arc.
|
static void |
drawDisplayRegion(GraphicsContext gc,
int xSource,
int ySource,
int width,
int height,
int xDestination,
int yDestination)
Draws a region of the display.
|
static void |
drawDisplayRegion(GraphicsContext gc,
int xSource,
int ySource,
int width,
int height,
int xDestination,
int yDestination,
int alpha)
Draws a region of the display.
|
static void |
drawEllipse(GraphicsContext gc,
int x,
int y,
int width,
int height)
Draws an ellipse.
|
static void |
drawEllipseArc(GraphicsContext gc,
int x,
int y,
int width,
int height,
float startAngle,
float arcAngle)
Draws an ellipse arc.
|
static void |
drawHorizontalLine(GraphicsContext gc,
int x,
int y,
int length)
Draws an horizontal line between two points.
|
static void |
drawImage(GraphicsContext gc,
Image image,
int x,
int y)
Draws the image at the given anchor top-left point.
|
static void |
drawImage(GraphicsContext gc,
Image image,
int x,
int y,
int alpha)
Draws the image at the given anchor top-left point.
|
static void |
drawImageRegion(GraphicsContext gc,
Image image,
int xSource,
int ySource,
int width,
int height,
int xDestination,
int yDestination)
Draws a region of the image.
|
static void |
drawImageRegion(GraphicsContext gc,
Image image,
int xSource,
int ySource,
int width,
int height,
int xDestination,
int yDestination,
int alpha)
Draws a region of an image.
|
static void |
drawLine(GraphicsContext gc,
int startX,
int startY,
int endX,
int endY)
Draws a line between two points.
|
static void |
drawRectangle(GraphicsContext gc,
int x,
int y,
int width,
int height)
Draws an orthogonal rectangle.
|
static void |
drawRenderableString(GraphicsContext gc,
RenderableString string,
int x,
int y)
Draws the string using the font and given graphics context color.
|
static void |
drawRoundedRectangle(GraphicsContext gc,
int x,
int y,
int width,
int height,
int cornerEllipseWidth,
int cornerEllipseHeight)
Draws an orthogonal rectangle with rounded corners.
|
static void |
drawString(GraphicsContext gc,
String string,
Font font,
int x,
int y)
Draws a string.
|
static void |
drawSubstring(GraphicsContext gc,
String string,
int offset,
int length,
Font font,
int x,
int y)
Draws a subset of a string.
|
static void |
drawVerticalLine(GraphicsContext gc,
int x,
int y,
int length)
Draws a vertical line between two points.
|
static void |
fillCircle(GraphicsContext gc,
int x,
int y,
int diameter)
Fills a circle.
|
static void |
fillCircleArc(GraphicsContext gc,
int x,
int y,
int diameter,
float startAngle,
float arcAngle)
Fills a circle arc.
|
static void |
fillEllipse(GraphicsContext gc,
int x,
int y,
int width,
int height)
Fills an ellipse.
|
static void |
fillEllipseArc(GraphicsContext gc,
int x,
int y,
int width,
int height,
float startAngle,
float arcAngle)
Fills an ellipse arc.
|
static void |
fillRectangle(GraphicsContext gc,
int x,
int y,
int width,
int height)
Fills an orthogonal rectangle.
|
static void |
fillRoundedRectangle(GraphicsContext gc,
int x,
int y,
int width,
int height,
int cornerEllipseWidth,
int cornerEllipseHeight)
Fills an orthogonal rectangle with rounded corners.
|
static void |
writePixel(GraphicsContext gc,
int x,
int y)
Draws the pixel at the given coordinates.
|
public static void drawChar(GraphicsContext gc, char character, Font font, int x, int y)
This method uses the background color and the ellipsis width which are set in the given GraphicsContext
.
The top-left anchor point of the character is defined by the (x,y)
coordinates. The
font baseline
may be used to set the anchor at the position of the character
baseline.
gc
- the graphics context to use.character
- the character.font
- the font to use.x
- the x coordinate of the string.y
- the y coordinate of the string.public static void drawCircle(GraphicsContext gc, int x, int y, int diameter)
The center of the circle is defined as the center of the square whose origin is at (x,y)
(upper-left
corner) and whose dimension is (diameter,diameter)
.
If the given diameter is negative or zero, nothing is drawn.
gc
- the graphics context to use.x
- the x coordinate of the bounding rectangle of the circle.y
- the y coordinate of the bounding rectangle of the circle.diameter
- the diameter of the circle.public static void drawCircleArc(GraphicsContext gc, int x, int y, int diameter, float startAngle, float arcAngle)
The center of the circle is defined as the center of the square whose origin is at (x,y)
(upper-left
corner) and whose dimension is (diameter,diameter)
.
The circle arc is drawn from startAngle
to startAngle+arcAngle
degrees. The value of
startAngle
is interpreted such that 0 degrees is along the positive x axis.
A positive angle indicates a counter-clockwise rotation while a negative angle indicates a clockwise rotation.
If the diameter
is negative or zero, nothing is drawn.
gc
- the graphics context to use.x
- the x coordinate of the bounding rectangle of the circle.y
- the y coordinate of the bounding rectangle of the circle.diameter
- the diameter of the circle.startAngle
- the start angle of the arc.arcAngle
- the angular distance of the arc.public static void drawDisplayRegion(GraphicsContext gc, int xSource, int ySource, int width, int height, int xDestination, int yDestination)
Equivalent to calling drawDisplayRegion(GraphicsContext, int, int, int, int, int, int, int)
with
GraphicsContext.OPAQUE
as alpha.
gc
- the graphics context to use.xSource
- the x coordinate of the region to copy.ySource
- the y coordinate of the region to copy.width
- the width of the region to copy.height
- the height of the region to copy.xDestination
- the x coordinate to draw at.yDestination
- the y coordinate to draw at.public static void drawDisplayRegion(GraphicsContext gc, int xSource, int ySource, int width, int height, int xDestination, int yDestination, int alpha)
The four points of the source rectangle have the following coordinates: (xSource,ySource)
,
(xSource+width-1,ySource)
, (xSource,ySource+height-1)
and
(xSource+width-1,ySource+height-1)
. However, this rectangle is limited to the boundaries of the
display.
In addition with drawDisplayRegion(GraphicsContext, int, int, int, int, int, int)
, this method allows to
specify the global opacity value to apply during the image rendering. This value is clamped between
GraphicsContext.TRANSPARENT
and GraphicsContext.OPAQUE
.
gc
- the graphics context to use.xSource
- the x coordinate of the region to copy.ySource
- the y coordinate of the region to copy.width
- the width of the region to copy.height
- the height of the region to copy.xDestination
- the x coordinate to draw at.yDestination
- the y coordinate to draw at.alpha
- the alpha to apply to the region.public static void drawEllipse(GraphicsContext gc, int x, int y, int width, int height)
(x,y)
(upper-left corner) and whose dimension is (width,height)
.
If either width
or height
is negative or zero, nothing is drawn.
gc
- the graphics context to use.x
- the x coordinate of the bounding rectangle of the ellipse.y
- the y coordinate of the bounding rectangle of the ellipse.width
- the width of the ellipse.height
- the height of the ellipse.public static void drawEllipseArc(GraphicsContext gc, int x, int y, int width, int height, float startAngle, float arcAngle)
(x,y)
(upper-left corner) and whose dimension is (width,height)
.
The ellipse arc is drawn from startAngle
to startAngle+arcAngle
degrees. The value of
startAngle
is interpreted such that 0 degrees is along the positive x axis.
A positive angle indicates a counter-clockwise rotation while a negative angle indicates a clockwise rotation.
The angles are given relative to the rectangle. For instance an angle of 45 degrees is always defined by the line from the center of the rectangle to the upper right corner of the rectangle. Thus for a non squared rectangle angles are skewed along either height or width.
If either width
or height
is negative or zero, nothing is drawn.
gc
- the graphics context to use.x
- the x coordinate of the bounding rectangle of the ellipse.y
- the y coordinate of the bounding rectangle of the ellipse.width
- the width of the ellipse.height
- the height of the ellipse.startAngle
- the start angle of the arc.arcAngle
- the angular distance of the arc.public static void drawHorizontalLine(GraphicsContext gc, int x, int y, int length)
The line is drawn from the point with the (x,y)
coordinates to the point with the
(x+length-1,y)
coordinates. The drawn line is composed of length
pixels. If
length
is negative or zero, nothing is drawn.
gc
- the graphics context to use.x
- the x coordinate of the first point of the line.y
- the y coordinate of the two points line.length
- the length of the line.public static void drawImage(GraphicsContext gc, Image image, int x, int y)
Equivalent to calling drawImage(GraphicsContext, Image, int, int, int)
with
GraphicsContext.OPAQUE
as alpha.
gc
- the graphics context to use.image
- the image to render.x
- the x coordinate of the anchor top-left point.y
- the y coordinate of the anchor top-left point.public static void drawImage(GraphicsContext gc, Image image, int x, int y, int alpha)
In addition with drawImage(GraphicsContext, Image, int, int)
, this method allows to specify the global
opacity value to apply during the image rendering. This value is clamped between
GraphicsContext.TRANSPARENT
and GraphicsContext.OPAQUE
.
gc
- the graphics context to use.image
- the image to render.x
- the x coordinate of the anchor top-left point.y
- the y coordinate of the anchor top-left point.alpha
- the global opacity rendering value, between 0 (transparent) and 255 (opaque).public static void drawImageRegion(GraphicsContext gc, Image image, int xSource, int ySource, int width, int height, int xDestination, int yDestination)
Equivalent to calling drawImageRegion(GraphicsContext, Image, int, int, int, int, int, int, int)
with
GraphicsContext.OPAQUE
as alpha.
gc
- the graphics context to use.image
- the image to render.xSource
- the x coordinate of the upper-left corner of the region to copy.ySource
- the y coordinate of the upper-left corner of the region to copy.width
- the width of the region to copy.height
- the height of the region to copy.xDestination
- the x coordinate of the anchor top-left point in the destination.yDestination
- the y coordinate of the anchor top-left point in the destination.public static void drawImageRegion(GraphicsContext gc, Image image, int xSource, int ySource, int width, int height, int xDestination, int yDestination, int alpha)
The region of the image to draw is given relative to the image (origin at the upper-left corner) as a rectangle.
If the specified source region exceeds the image bounds, the copied region is limited to the image boundary. If the copied region goes out of the bounds of the graphics context area, pixels out of the range will not be drawn.
In addition with drawImageRegion(GraphicsContext, Image, int, int, int, int, int, int)
, this method
allows to specify the global opacity value to apply during the image rendering. This value is clamped between
GraphicsContext.TRANSPARENT
and GraphicsContext.OPAQUE
.
gc
- the graphics context to use.image
- the image to render.xSource
- the x coordinate of the upper-left corner of the region to copy.ySource
- the y coordinate of the upper-left corner of the region to copy.width
- the width of the region to copy.height
- the height of the region to copy.xDestination
- the x coordinate of the anchor top-left point in the destination.yDestination
- the y coordinate of the anchor top-left point in the destination.alpha
- the alpha to apply to the region.public static void drawLine(GraphicsContext gc, int startX, int startY, int endX, int endY)
The line is drawn from the point with the (startX,startY)
coordinates to the point with the
(endX,endY)
coordinates.
gc
- the graphics context to use.startX
- the x coordinate of the first point of the line.startY
- the y coordinate of the first point of the line.endX
- the x coordinate of the second point of the line.endY
- the y coordinate of the second point of the line.public static void drawRectangle(GraphicsContext gc, int x, int y, int width, int height)
The four points of the rectangle have the following coordinates: (x,y)
, (x+width-1,y)
,
(x,y+height-1)
and (x+width-1,y+height-1)
.
If either width
or height
is negative or zero, nothing is drawn.
gc
- the graphics context to use.x
- the x coordinate of the rectangle.y
- the y coordinate of the rectangle.width
- the width of the rectangle.height
- the height of the rectangle.public static void drawRenderableString(GraphicsContext gc, RenderableString string, int x, int y)
drawString(GraphicsContext, String, Font, int, int)
gc
- the graphics context to use.string
- the renderable string to render.x
- the x coordinate of the anchor point.y
- the y coordinate of the anchor point.drawString(GraphicsContext, String, Font, int, int)
public static void drawRoundedRectangle(GraphicsContext gc, int x, int y, int width, int height, int cornerEllipseWidth, int cornerEllipseHeight)
The four points of the rectangle have the following coordinates: (x,y)
, x+width-1,y
,
(x,y+height-1)
and (x+width-1,y+height-1)
.
If either width
or height
is negative or zero, nothing is drawn.
gc
- the graphics context to use.x
- the x coordinate of the rectangle.y
- the y coordinate of the rectangle.width
- the width of the rectangle.height
- the height of the rectangle.cornerEllipseWidth
- the horizontal diameter of the corner arcs.cornerEllipseHeight
- the vertical diameter of the corner arcs.public static void drawString(GraphicsContext gc, String string, Font font, int x, int y)
This method uses the background color and the ellipsis width which are set in the given GraphicsContext
.
The top-left anchor point of the string is defined by the (x,y)
coordinates. The
font baseline
may be used to set the anchor at the position of the string
baseline.
gc
- the graphics context to use.string
- the string.font
- the font to use.x
- the x coordinate of the string.y
- the y coordinate of the string.public static void drawSubstring(GraphicsContext gc, String string, int offset, int length, Font font, int x, int y)
This method uses the background color and the ellipsis width which are set in the given GraphicsContext
.
The top-left anchor point of the string is defined by the (x,y)
coordinates. The
font baseline
may be used to set the anchor at the position of the string
baseline.
gc
- the graphics context to use.string
- the string.offset
- the index of the first character of the string.length
- the number of characters of the string.font
- the font to use.x
- the x coordinate of the string.y
- the y coordinate of the string.StringIndexOutOfBoundsException
- if the given offset and length do not specify a valid range within the given string.public static void drawVerticalLine(GraphicsContext gc, int x, int y, int length)
The line is drawn from the point with the (x,y)
coordinates to the point with the
(x,y+length-1)
coordinates. The drawn line is composed of length
pixels. If
length
is negative or zero, nothing is drawn.
gc
- the graphics context to use.x
- the x coordinate of the first point of the line.y
- the y coordinate of the two points of the line.length
- the length of the line.public static void fillCircle(GraphicsContext gc, int x, int y, int diameter)
The center of the circle is defined as the center of the square whose origin is at (x,y)
(upper-left
corner) and whose dimension is (diameter,diameter)
.
If the given diameter is negative or zero, nothing is drawn.
gc
- the graphics context to use.x
- the x coordinate of the bounding rectangle of the circle.y
- the y coordinate of the bounding rectangle of the circle.diameter
- the diameter of the circle.public static void fillCircleArc(GraphicsContext gc, int x, int y, int diameter, float startAngle, float arcAngle)
The center of the circle is defined as the center of the square whose origin is at (x,y)
(upper-left
corner) and whose dimension is (diameter,diameter)
.
The circle arc is drawn from startAngle
to startAngle+arcAngle
degrees. The value of
startAngle
is interpreted such that 0 degrees is along the positive x axis.
A positive angle indicates a counter-clockwise rotation while a negative angle indicates a clockwise rotation.
If the given diameter is negative or zero, nothing is drawn.
gc
- the graphics context to use.x
- the x coordinate of the bounding rectangle of the circle.y
- the y coordinate of the bounding rectangle of the circle.diameter
- the diameter of the circle.startAngle
- the start angle of the arc.arcAngle
- the angular distance of the arc.public static void fillEllipse(GraphicsContext gc, int x, int y, int width, int height)
(x,y)
(upper-left corner) and whose dimension is (width,height)
.
If either width
or height
is negative or zero, nothing is drawn.
gc
- the graphics context to use.x
- the x coordinate of the bounding rectangle of the ellipse.y
- the y coordinate of the bounding rectangle of the ellipse.width
- the width of the ellipse.height
- the height of the ellipse.public static void fillEllipseArc(GraphicsContext gc, int x, int y, int width, int height, float startAngle, float arcAngle)
(x,y)
(upper-left corner) and whose dimension is (width,height)
.
The ellipse arc is drawn from startAngle
to startAngle+arcAngle
degrees. The value of
startAngle
is interpreted such that 0 degrees is along the positive x axis.
A positive angle indicates a counter-clockwise rotation while a negative angle indicates a clockwise rotation.
The angles are given relative to the rectangle. For instance an angle of 45 degrees is always defined by the line from the center of the rectangle to the upper right corner of the rectangle. Thus for a non squared rectangle angles are skewed along either height or width.
If either width
or height
is negative or zero, nothing is drawn.
gc
- the graphics context to use.x
- the x coordinate of the bounding rectangle of the ellipse.y
- the y coordinate of the bounding rectangle of the ellipse.width
- the width of the ellipse.height
- the height of the ellipse.startAngle
- the start angle of the arc.arcAngle
- the angular distance of the arc.public static void fillRectangle(GraphicsContext gc, int x, int y, int width, int height)
The four points of the rectangle have the following coordinates: (x,y)
, x+width-1,y
,
(x,y+height-1)
and (x+width-1,y+height-1)
.
If either width
or height
is negative or zero, nothing is drawn.
gc
- the graphics context to use.x
- the x coordinate of the rectangle.y
- the y coordinate of the rectangle.width
- the width of the rectangle.height
- the height of the rectangle.public static void fillRoundedRectangle(GraphicsContext gc, int x, int y, int width, int height, int cornerEllipseWidth, int cornerEllipseHeight)
The four points of the rectangle have the following coordinates: (x,y)
, x+width-1,y
,
(x,y+height-1)
and (x+width-1,y+height-1)
.
If either width
or height
is negative or zero, nothing is drawn. If either
cornerEllipseWidth
or arcHeight
is negative or zero, a regular rectangle is drawn.
gc
- the graphics context to use.x
- the x coordinate of the rectangle.y
- the y coordinate of the rectangle.width
- the width of the rectangle.height
- the height of the rectangle.cornerEllipseWidth
- the horizontal diameter of the corner arcs.cornerEllipseHeight
- the vertical diameter of the corner arcs.public static void writePixel(GraphicsContext gc, int x, int y)
gc
- the graphics context to use.x
- the x coordinate of the pixel.y
- the y coordinate of the pixel.