Package codedraw
Class Image
java.lang.Object
codedraw.Image
- Direct Known Subclasses:
BorderlessWindow
,CodeDraw
,FullScreen
This class represents an image that can be used within the CodeDraw library.
Empty images can be created via the constructor
Image(int, int)
.
Optionally a background color can be specified Image(int, int, Color)
.
Images can be loaded from external source via static constructors such as
fromFile(String)
and fromUrl(String)
.
Image image = Image.fromFile("/directory/filename.png");
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
This method is called each time right after a shape is drawn.protected void
This method is called each time right before a shape is drawn.void
clear()
Colors the whole canvas in white.void
Colors the whole canvas in the color given as a parameter.void
copyTo
(Graphics target, Interpolation interpolation) Copies this image onto the graphics object.static Image
Creates a new image from the given image that only contains the specified section.void
drawArc
(double centerX, double centerY, double radius, double startRadians, double sweepRadians) Draws an arc with the center being the (centerX, centerY) coordinates.void
drawArc
(double centerX, double centerY, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians) Draws an arc with the center being the (centerX, centerY) coordinates.void
drawBezier
(double startX, double startY, double control1X, double control1Y, double control2X, double control2Y, double endX, double endY) Draws a cubic Bézier curve.void
drawCircle
(double centerX, double centerY, double radius) Draws the outline of a circle.void
drawCurve
(double startX, double startY, double controlX, double controlY, double endX, double endY) Draws a quadratic Bézier curve.void
drawEllipse
(double centerX, double centerY, double horizontalRadius, double verticalRadius) Draws the outline of an ellipse.void
Draws an image at the specified (x, y) coordinate.void
drawImage
(double x, double y, double width, double height, Image image, Interpolation interpolation) Draws an image at the specified (x, y) coordinate.void
Draws an image at the specified (x, y) coordinate.void
drawLine
(double startX, double startY, double endX, double endY) Draws a straight line between the start point and end point.boolean
drawOver()
Defines the way new pixels are drawn over already existing pixels.drawPathStartingAt
(double x, double y) Starts a path from the starting position specified in this method.void
drawPie
(double centerX, double centerY, double radius, double startRadians, double sweepRadians) Draws the outline of a pie with the center being the (centerX, centerY) coordinates.void
drawPie
(double centerX, double centerY, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians) Draws the outline of a pie with the center being the (centerX, centerY) coordinates.void
drawPoint
(double centerX, double centerY) Draws a point.void
drawPolygon
(double... vertices) Draws the outline of a polygon.void
drawRectangle
(double x, double y, double width, double height) Draws the outline of a rectangle.void
drawSquare
(double x, double y, double sideLength) Draws the outline of a square.void
Draws the text at the specified (x, y) coordinate.void
drawTriangle
(double x1, double y1, double x2, double y2, double x3, double y3) Draws the outline of a triangle.void
fillCircle
(double centerX, double centerY, double radius) Draws a filled circle.void
fillEllipse
(double centerX, double centerY, double horizontalRadius, double verticalRadius) Draws a filled ellipse.fillPathStartingAt
(double x, double y) Starts a path from the starting position specified in this method.void
fillPie
(double centerX, double centerY, double radius, double startRadians, double sweepRadians) Draws a filled pie with the center being the (centerX, centerY) coordinates.void
fillPie
(double centerX, double centerY, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians) Draws a filled pie with the center being the (centerX, centerY) coordinates.void
fillPolygon
(double... vertices) Draws a filled polygon.void
fillRectangle
(double x, double y, double width, double height) Draws a filled rectangle.void
fillSquare
(double x, double y, double sideLength) Draws a filled square.void
fillTriangle
(double x1, double y1, double x2, double y2, double x3, double y3) Draws a filled triangle.static Image
fromBase64String
(String base64) Converts a Base64 string to an image.static Image
fromDPIAwareSize
(int width, int height) Creates a CodeDraw image with an up-scaled resolution.static Image
Loads an image from the file system.static Image
fromResource
(String resourceName) Loads images from the resource folder.static Image
Loads an image file from the internet.getColor()
Defines the color that is used for drawing all shapes.Defines how the corners of drawn shapes should look.int
This height of this image.double
Defines the width or thickness of drawn shapes and lines.getPixel
(int x, int y) Returns the pixel color at the specified location.Defines the styling of drawn text.Gets the currently applied transformation of this image.int
getWidth()
This width of this image.boolean
Defines whether drawn text and shapes are anti-aliased.static Image
mirrorHorizontally
(Image image) Mirrors the image horizontally.static Image
mirrorVertically
(Image image) Mirrors the image vertically.static Image
rotateClockwise
(Image image) Rotates the image clockwise.static Image
rotateCounterClockwise
(Image image) Rotates the image counter-clockwise.static void
save
(Image image, String pathToImage, ImageFormat format) Saves the image to the specified location using the specified image format.static Image
Increases or decrease the size of the image given as a parameter.static Image
scale
(Image source, double scale, Interpolation interpolation) Increases or decrease the size of the image given as a parameter.void
setAntiAliased
(boolean isAntiAliased) Defines whether drawn text and shapes are anti-aliased.void
Defines the color that is used for drawing all shapes.void
Defines how the corners of drawn shapes should look.void
setDrawOver
(boolean drawOver) Defines the way new pixels are drawn over already existing pixels.void
setLineWidth
(double lineWidth) Defines the width or thickness of drawn shapes and lines.void
Draws a point which is the size of one pixel.void
setTextFormat
(TextFormat textFormat) Defines the styling of drawn text.void
setTransformation
(Matrix2D transformation) A transformation can be used to apply a linear transformations before shapes are drawn to the image.void
Resets the current transformation to the default value where no transformation is applied.Creates a copy of this image in the form of aBufferedImage
.Creates a copy of this image in the form of aBufferedImage
.
-
Constructor Details
-
Image
Creates a copy of the image supplied as a parameter. The configuration of the image will not be copied. All parameters of the result will be set to their default value. DPI aware scaling will be retained.- Parameters:
image
- to create a copy of.
-
Image
Creates an image from theImage
supplied as a parameter. The contents of the supplied image will be copied to create this new Image instance.- Parameters:
image
- Image that is to be converted to a CodeDraw Image.
-
Image
public Image(int width, int height) Creates a white image of the specified size.- Parameters:
width
- The width of the image.height
- The height of the image.
-
Image
Creates an image of the specified size with a custom background color.- Parameters:
width
- The width of the image.height
- The height of the image.backgroundColor
- the background color of the whole image. Can be transparent.
-
-
Method Details
-
fromFile
Loads an image from the file system. Supported image formats: .jpg or .jpeg (JPEG), .bmp (Bitmap), .gif (Graphics Interchange Format), .png (Portable Network Graphic) and .wbmp (Wireless Application Protocol Bitmap Format).Image image = Image.fromFile("/directory/filename.png");
ImageIO.read(File)
is used to read images from the file system.- Parameters:
pathToImage
- A string that points to an image file.- Returns:
- An image.
-
fromUrl
Loads an image file from the internet. This function might be very slow depending on speed of the network connection or the availability of the server.
Supported image formats: .jpg or .jpeg (JPEG), .bmp (Bitmap), .gif (Graphics Interchange Format), .png (Portable Network Graphic) and .wbmp (Wireless Application Protocol Bitmap Format).Image image = Image.fromUrl("https://exmaple.com/example-image.png");
ImageIO.read(URL)
is used to load images from the network.- Parameters:
url
- Link to the image file.- Returns:
- An image.
-
fromResource
Loads images from the resource folder.
Supported image formats: .jpg or .jpeg (JPEG), .bmp (Bitmap), .gif (Graphics Interchange Format), .png (Portable Network Graphic) and .wbmp (Wireless Application Protocol Bitmap Format).Image image = Image.fromResource("./my_image.png");
ImageIO.read(URL)
is used to read images from the resource folder.- Parameters:
resourceName
- Path to the resource from the root of the resource folder.- Returns:
- An image.
-
fromBase64String
Converts a Base64 string to an image. Supported image formats: .jpg or .jpeg (JPEG), .bmp (Bitmap), .gif (Graphics Interchange Format), .png (Portable Network Graphic) and .wbmp (Wireless Application Protocol Bitmap Format).ImageIO.read(InputStream)
andBase64.Decoder.decode(String)
are used to convert the image.- Parameters:
base64
- a Base64 string.- Returns:
- an image.
-
fromDPIAwareSize
Creates a CodeDraw image with an up-scaled resolution. The upscaling factor depends on the highest resolution of all monitors of the computer executing this program. The pixel grid coordinates will be the same as a regular image but round edges and fractional coordinates will be drawn more precisely.- Parameters:
width
- The width of the image.height
- The height of the image.- Returns:
- an up-scaled image.
-
save
Saves the image to the specified location using the specified image format. Supported formats are all values inImageFormat
. The formatsImageFormat.JPG
,ImageFormat.JPEG
andImageFormat.BMP
do not support transparency.ImageIO.write(RenderedImage, String, File)
andFile(String)
are used to read the image from the file system. Read their documentation for more details.- Parameters:
image
- any image.pathToImage
- The location where the image should be saved.format
- The format the image should be saved in. As a default, chooseImageFormat.PNG
and make sure that the file ends with ".png".
-
crop
Creates a new image from the given image that only contains the specified section.- Parameters:
source
- The image from which a new smaller image should be created from.x
- The start point of the cutout.y
- The start point of the cutout.width
- The width of the cutout.height
- The height of the cutout.- Returns:
- A new image cutout from the specified section.
-
scale
Increases or decrease the size of the image given as a parameter. For example a scale of 0.5 would half the image and a size of 2 would double its size. If the resulting image resolution is too small this function will throw an exception.- Parameters:
source
- The source image to create the larger or smaller image.scale
- The increase or decrease in image size.- Returns:
- the scaled image.
- Throws:
IllegalArgumentException
- if the resulting image width or height is zero.
-
scale
Increases or decrease the size of the image given as a parameter. For example a scale of 0.5 would half the image and a size of 2 would double its size. If the resulting image resolution is too small this function will throw an exception.- Parameters:
source
- The source image to create the larger or smaller image.scale
- The increase or decrease in image size.interpolation
- What technique is used to upscale the image. SeeInterpolation
.- Returns:
- the scaled image.
- Throws:
IllegalArgumentException
- if the resulting image width or height is zero.
-
rotateClockwise
Rotates the image clockwise.- Parameters:
image
- The image to rotate.- Returns:
- A copy of the image that is rotated 90° clockwise.
-
rotateCounterClockwise
Rotates the image counter-clockwise.- Parameters:
image
- The image to rotate.- Returns:
- A copy of the image that is rotated 90° counter-clockwise.
-
mirrorHorizontally
Mirrors the image horizontally.- Parameters:
image
- The image to mirror.- Returns:
- A copy of the image that is mirrored along the horizontal axis.
-
mirrorVertically
Mirrors the image vertically.- Parameters:
image
- The image to mirror.- Returns:
- A copy of the image that is mirrored along the vertical axis.
-
getWidth
public int getWidth()This width of this image. This value cannot be changed once set via the constructor.- Returns:
- the width of the canvas in pixel.
-
getHeight
public int getHeight()This height of this image. This value cannot be changed once set via the constructor.- Returns:
- the height of the canvas in pixel.
-
getColor
Defines the color that is used for drawing all shapes.- Returns:
- the drawing color of this CodeDraw window.
-
setColor
Defines the color that is used for drawing all shapes. Use thePalette
for a large selection of predefined colors.- Parameters:
color
- Sets the drawing color of this CodeDraw window.
-
getLineWidth
public double getLineWidth()Defines the width or thickness of drawn shapes and lines. Must be greater than zero.- Returns:
- the lineWidth of this CodeDraw window.
-
setLineWidth
public void setLineWidth(double lineWidth) Defines the width or thickness of drawn shapes and lines. Must be greater than zero.- Parameters:
lineWidth
- Sets the lineWidth of this CodeDraw window.
-
getCorner
Defines how the corners of drawn shapes should look. SeeCorner
for details.- Returns:
- the corner style of this CodeDraw window.
-
setCorner
Defines how the corners of drawn shapes should look. SeeCorner
for details.- Parameters:
corner
- Sets the corner style of this CodeDraw window.
-
getTextFormat
Defines the styling of drawn text. See alsodrawText(double, double, String)
on how the styling is applied.- Returns:
- the text formatting options of this CodeDraw window.
-
setTextFormat
Defines the styling of drawn text. See alsodrawText(double, double, String)
on how the styling is applied.- Parameters:
textFormat
- Sets the text formatting options of this CodeDraw window.
-
isAntiAliased
public boolean isAntiAliased()Defines whether drawn text and shapes are anti-aliased. See Wikipedia Spatial Anti-aliasing for details.- Returns:
- Whether this CodeDraw window anti aliases.
-
setAntiAliased
public void setAntiAliased(boolean isAntiAliased) Defines whether drawn text and shapes are anti-aliased. See Wikipedia Spatial Anti-aliasing for details.- Parameters:
isAntiAliased
- Sets whether this CodeDraw window anti aliases.
-
getTransformation
Gets the currently applied transformation of this image. SeeMatrix2D
for details on linear transformations.- Returns:
- a
Matrix2D
.
-
setTransformation
A transformation can be used to apply a linear transformations before shapes are drawn to the image. For example text can be rotated by setting the transformation to a rotational matrix.
Seecd.setTransformation(Matrix2D.IDENTITY.rotateAt(300, 300, Math.PI / 4); cd.drawText(300, 300, "Hello World!");
Matrix2D
for details on linear transformations.- Parameters:
transformation
- a 2d matrix with a specific transformation.
-
setTransformationToIdentity
public void setTransformationToIdentity()Resets the current transformation to the default value where no transformation is applied. SeesetTransformation(Matrix2D)
andMatrix2D
for details on linear transformations. -
drawOver
public boolean drawOver()Defines the way new pixels are drawn over already existing pixels. When draw over is true transparent pixel will mix with the underlying pixels. When draw over is false transparent pixel will replace the existing pixels.- Returns:
- whether draw over is on or off.
-
setDrawOver
public void setDrawOver(boolean drawOver) Defines the way new pixels are drawn over already existing pixels. When draw over is true transparent pixel will mix with the underlying pixels. When draw over is false transparent pixel will replace the existing pixels. If you want to make a section of an image transparent set draw over to false.- Parameters:
drawOver
- whether shapes are drawn over or replace existing pixels.
-
getPixel
Returns the pixel color at the specified location. Ignores any transformation set bysetTransformation(Matrix2D)
.- Parameters:
x
- The distance in pixel from the left side of the canvas.y
- The distance in pixel from the top side of the canvas.
-
setPixel
Draws a point which is the size of one pixel. Ignores any transformation set bysetTransformation(Matrix2D)
. No exception is thrown if x or y are outside the bounds of this image.- Parameters:
x
- The distance in pixel from the left side of the canvas.y
- The distance in pixel from the top side of the canvas.color
- the color of the pixel.
-
drawText
Draws the text at the specified (x, y) coordinate. Formatting options can be set via the TextFormat object. SeegetTextFormat()
,setTextFormat(TextFormat)
and theTextFormat
class. Multiple lines can be drawn by including newline characters in the text parameter. If not specified otherwise in the TextFormat object the (x, y) coordinates will be in the top left corner of the text.- Parameters:
x
- The distance in pixel from the left side of the canvas.y
- The distance in pixel from the top side of the canvas.text
- The text or string to be drawn.
-
drawPoint
public void drawPoint(double centerX, double centerY) Draws a point. The size of the point can be changed through thesetLineWidth(double)
method.- Parameters:
centerX
- The distance in pixel from the left side of the canvas to the center of the point.centerY
- The distance in pixel from the top side of the canvas to the center of the point.
-
drawLine
public void drawLine(double startX, double startY, double endX, double endY) Draws a straight line between the start point and end point. The line width can be changed through thesetLineWidth(double)
method. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- Parameters:
startX
- The distance in pixel from the left side of the canvas to the start of the line.startY
- The distance in pixel from the top side of the canvas to the start of the line.endX
- The distance in pixel from the left side of the canvas to the end of the line.endY
- The distance in pixel from the top side of the canvas to the end of the line.
-
drawCurve
public void drawCurve(double startX, double startY, double controlX, double controlY, double endX, double endY) Draws a quadratic Bézier curve. See: Wikipedia Bezier Curve. The start and end of the curve will be precisely where startX/Y and endX/Y are specified. The controlX/Y parameter specifies in what way the curve will be bent. The line width can be changed withsetLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- Parameters:
startX
- The distance in pixel from the left side of the canvas to the start of the curve.startY
- The distance in pixel from the top side of the canvas to the start of the curve.controlX
- Defines the way the curve bends in the x direction.controlY
- Defines the way the curve bends in the y direction.endX
- The distance in pixel from the left side of the canvas to the end of the curve.endY
- The distance in pixel from the top side of the canvas to the end of the curve.
-
drawBezier
public void drawBezier(double startX, double startY, double control1X, double control1Y, double control2X, double control2Y, double endX, double endY) Draws a cubic Bézier curve. See Wikipedia Bezier Curve. The start and end of the curve will be precisely where startX/Y and endX/Y are specified. The control1X/Y and control2X/Y parameters specify in what way the curve will be bent. The line width can be changed withsetLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- Parameters:
startX
- The distance in pixel from the left side of the canvas to the start of the curve.startY
- The distance in pixel from the top side of the canvas to the start of the curve.control1X
- Defines the way the curve bends in the x direction.control1Y
- Defines the way the curve bends in the y direction.control2X
- Defines the way the curve bends in the x direction.control2Y
- Defines the way the curve bends in the y direction.endX
- The distance in pixel from the left side of the canvas to the end of the curve.endY
- The distance in pixel from the top side of the canvas to the end of the curve.
-
drawSquare
public void drawSquare(double x, double y, double sideLength) Draws the outline of a square. The line width can be changed withsetLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- Parameters:
x
- The distance in pixel from the left side of the canvas to the left size of the square.y
- The distance in pixel from the top side of the canvas to the top side of the square.sideLength
- The width and the height of the square in pixel.
-
fillSquare
public void fillSquare(double x, double y, double sideLength) Draws a filled square. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- Parameters:
x
- The distance in pixel from the left side of the canvas to the left size of the square.y
- The distance in pixel from the top side of the canvas to the top side of the square.sideLength
- The width and the height of the square in pixel.
-
drawRectangle
public void drawRectangle(double x, double y, double width, double height) Draws the outline of a rectangle. The line width can be changed withsetLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- Parameters:
x
- The distance in pixel from the left side of the canvas to the left size of the rectangle.y
- The distance in pixel from the top side of the canvas to the top side of the rectangle.height
- The height of the rectangle in pixel.width
- The width of the rectangle in pixel.
-
fillRectangle
public void fillRectangle(double x, double y, double width, double height) Draws a filled rectangle. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- Parameters:
x
- The distance in pixel from the left side of the canvas to the left size of the rectangle.y
- The distance in pixel from the top side of the canvas to the top side of the rectangle.height
- The height of the rectangle in pixel.width
- The width of the rectangle in pixel.
-
drawCircle
public void drawCircle(double centerX, double centerY, double radius) Draws the outline of a circle. The center of the circle will be at the specified (centerX, centerY) coordinate. The line width can be changed withsetLineWidth(double)
.- Parameters:
centerX
- The distance in pixel from the left side of the canvas to the center of the circle.centerY
- The distance in pixel from the top side of the canvas to the center of the circle.radius
- The radius of the circle in pixel.
-
fillCircle
public void fillCircle(double centerX, double centerY, double radius) Draws a filled circle. The center of the circle will be at the specified (centerX, centerY) coordinate.- Parameters:
centerX
- The distance in pixel from the left side of the canvas to the center of the circle.centerY
- The distance in pixel from the top side of the canvas to the center of the circle.radius
- The radius of the circle in pixel.
-
drawEllipse
public void drawEllipse(double centerX, double centerY, double horizontalRadius, double verticalRadius) Draws the outline of an ellipse. The center of the ellipse will be at the specified (centerX, centerY) coordinate. The line width can be changed withsetLineWidth(double)
.- Parameters:
centerX
- The distance in pixel from the left side of the canvas to the center of the ellipse.centerY
- The distance in pixel from the top side of the canvas to the center of the ellipse.horizontalRadius
- The horizontal radius of the ellipse in pixel. The width of the ellipse is 2 * horizontalRadius.verticalRadius
- The vertical radius of the ellipse in pixel. The height of the ellipse is 2 * verticalRadius.
-
fillEllipse
public void fillEllipse(double centerX, double centerY, double horizontalRadius, double verticalRadius) Draws a filled ellipse. The center of the ellipse will be at the specified (centerX, centerY) coordinate.- Parameters:
centerX
- The distance in pixel from the left side of the canvas to the center of the ellipse.centerY
- The distance in pixel from the top side of the canvas to the center of the ellipse.horizontalRadius
- The horizontal radius of the ellipse in pixel. The width of the ellipse is 2 * horizontalRadius.verticalRadius
- The vertical radius of the ellipse in pixel. The height of the ellipse is 2 * verticalRadius.
-
drawArc
public void drawArc(double centerX, double centerY, double radius, double startRadians, double sweepRadians) Draws an arc with the center being the (centerX, centerY) coordinates. The arc starts at the 3 o'clock position offset by the startRadians parameter. The total length of the arc is defined by the sweepRadians parameter. The line width can be changed withsetLineWidth(double)
.
You can useMath.toRadians(double)
to specify the angle in degrees.
The line width can be changed withcd.drawArc(200, 200, 50, Math.toRadians(90), Math.toRadians(180));
setLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- Parameters:
centerX
- The distance in pixel from the left side of the canvas to the center of the arc.centerY
- The distance in pixel from the top side of the canvas to the center of the arc.radius
- The radius of the arc in pixel.startRadians
- The starting angle in radians. A 0 radians angle would be interpreted as starting at 3 o'clock going clockwise.sweepRadians
- The length of the arc in radians from the start angle in a clockwise direction.
-
drawArc
public void drawArc(double centerX, double centerY, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians) Draws an arc with the center being the (centerX, centerY) coordinates. The width is 2 * horizontalRadius and the height is 2 * verticalRadius. The arc starts at the 3 o'clock position offset by the startRadians parameter. The total length of the arc is defined by the sweepRadians parameter. The line width can be changed withsetLineWidth(double)
.
You can useMath.toRadians(double)
to specify the angle in degrees.
The line width can be changed withcd.drawArc(200, 200, 50, 50, Math.toRadians(90), Math.toRadians(180));
setLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- Parameters:
centerX
- The distance in pixel from the left side of the canvas to the center of the arc.centerY
- The distance in pixel from the top side of the canvas to the center of the arc.horizontalRadius
- The horizontal radius of the arc in pixel. The width of the arc is 2 * horizontalRadius.verticalRadius
- The vertical radius of the arc in pixel. The height of the arc is 2 * verticalRadius.startRadians
- The starting angle in radians. A 0 radians angle would be interpreted as starting at 3 o'clock going clockwise.sweepRadians
- The length of the arc in radians from the start angle in a clockwise direction.
-
drawPie
public void drawPie(double centerX, double centerY, double radius, double startRadians, double sweepRadians) Draws the outline of a pie with the center being the (centerX, centerY) coordinates. The pie starts at the 3 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter. The line width can be changed withsetLineWidth(double)
.
You can useMath.toRadians(double)
to specify the angle in degrees.
The line width can be changed withcd.drawPie(200, 200, 50, Math.toRadians(90), Math.toRadians(180));
setLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- Parameters:
centerX
- The distance in pixel from the left side of the canvas to the center of the pie.centerY
- The distance in pixel from the top side of the canvas to the center of the pie.radius
- The radius of the pie in pixel.startRadians
- The starting angle in radians. A 0 radians angle would be interpreted as starting at 3 o'clock going clockwise.sweepRadians
- The length of the pie in radians from the start angle in a clockwise direction.
-
drawPie
public void drawPie(double centerX, double centerY, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians) Draws the outline of a pie with the center being the (centerX, centerY) coordinates. The width is 2 * horizontalRadius and the height is the 2 * verticalRadius. The pie starts at the 3 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter. The line width can be changed withsetLineWidth(double)
.
You can useMath.toRadians(double)
to specify the angle in degrees.
The line width can be changed withcd.drawPie(200, 200, 50, 50, Math.toRadians(90), Math.toRadians(180));
setLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- Parameters:
centerX
- The distance in pixel from the left side of the canvas to the center of the pie.centerY
- The distance in pixel from the top side of the canvas to the center of the pie.horizontalRadius
- The horizontal radius of the pie in pixel. The width of the pie is 2 * horizontalRadius.verticalRadius
- The vertical radius of the pie in pixel. The height of the pie is 2 * verticalRadius.startRadians
- The starting angle in radians. A 0 radians angle would be interpreted as starting at 3 o'clock going clockwise.sweepRadians
- The length of the pie in radians from the start angle in a clockwise direction.
-
fillPie
public void fillPie(double centerX, double centerY, double radius, double startRadians, double sweepRadians) Draws a filled pie with the center being the (centerX, centerY) coordinates. The pie starts at the 3 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter.
You can useMath.toRadians(double)
to specify the angle in degrees.cd.fillPie(200, 200, 50, Math.toRadians(90), Math.toRadians(180));
- Parameters:
centerX
- The distance in pixel from the left side of the canvas to the center of the pie.centerY
- The distance in pixel from the top side of the canvas to the center of the pie.radius
- The radius of the pie in pixel.startRadians
- The starting angle in radians. A 0 radians angle would be interpreted as starting at 3 o'clock going clockwise.sweepRadians
- The length of the pie in radians from the start angle in a clockwise direction.
-
fillPie
public void fillPie(double centerX, double centerY, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians) Draws a filled pie with the center being the (centerX, centerY) coordinates. The width is 2 * horizontalRadius and the height is the 2 * verticalRadius. The pie starts at the 3 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter.
You can useMath.toRadians(double)
to specify the angle in degrees.cd.fillPie(200, 200, 50, 50, Math.toRadians(90), Math.toRadians(180));
- Parameters:
centerX
- The distance in pixel from the left side of the canvas to the center of the pie.centerY
- The distance in pixel from the top side of the canvas to the center of the pie.horizontalRadius
- The horizontal radius of the pie in pixel. The width of the pie is 2 * horizontalRadius.verticalRadius
- The vertical radius of the pie in pixel. The height of the pie is 2 * verticalRadius.startRadians
- The starting angle in radians. A 0 radians angle would be interpreted as starting at 3 o'clock going clockwise.sweepRadians
- The length of the pie in radians from the start angle in a clockwise direction.
-
drawTriangle
public void drawTriangle(double x1, double y1, double x2, double y2, double x3, double y3) Draws the outline of a triangle. The line width can be changed withsetLineWidth(double)
.- Parameters:
x1
- The distance in pixel from the left side of the canvas to the first corner of the triangle.y1
- The distance in pixel from the top side of the canvas to the first corner of the triangle.x2
- The distance in pixel from the left side of the canvas to the second corner of the triangle.y2
- The distance in pixel from the top side of the canvas to the second corner of the triangle.x3
- The distance in pixel from the left side of the canvas to the third corner of the triangle.y3
- The distance in pixel from the top side of the canvas to the third corner of the triangle.
-
fillTriangle
public void fillTriangle(double x1, double y1, double x2, double y2, double x3, double y3) Draws a filled triangle.- Parameters:
x1
- The distance in pixel from the left side of the canvas to the first corner of the triangle.y1
- The distance in pixel from the top side of the canvas to the first corner of the triangle.x2
- The distance in pixel from the left side of the canvas to the second corner of the triangle.y2
- The distance in pixel from the top side of the canvas to the second corner of the triangle.x3
- The distance in pixel from the left side of the canvas to the third corner of the triangle.y3
- The distance in pixel from the top side of the canvas to the third corner of the triangle.
-
drawPolygon
public void drawPolygon(double... vertices) Draws the outline of a polygon. This function must be called with an even number of parameters. Each parameter pair (x, y) represents a vertex of the polygon. Must be called with at least two vertices as parameter. Each point passed to drawPolygon will be connected to the following vertices and the last vertex will be connected to the first vertex.
The line width can be changed withcd.drawPolygon( 200, 100, 100, 200, 300, 200 );
setLineWidth(double)
.- Parameters:
vertices
- An even number of doubles. Each pair represents one corner of the polygon.
-
fillPolygon
public void fillPolygon(double... vertices) Draws a filled polygon. This function must be called with an even number of parameters. Each parameter pair (x, y) represents a vertex of the polygon. Must be called with at least two vertices as parameter. Each point passed to drawPolygon will be connected to the following vertices and the last vertex will be connected to the first vertex. If you pass only two arguments to this method nothing will be drawn.cd.fillPolygon( 200, 100, 100, 200, 300, 200 );
- Parameters:
vertices
- An even number of doubles. Each pair represents one corner of the polygon.
-
fillPathStartingAt
Starts a path from the starting position specified in this method. Then method chaining is used to specify the section of the path. At the end complete has to be called to actually draw the shape. See the class description of thePath
class for an example.- Parameters:
x
- the starting point of the path relative to the top left corner of the canvas.y
- the starting point of the path relative to the top left corner of the canvas.- Returns:
- a path object with which more complicated shapes can be constructed.
-
drawPathStartingAt
Starts a path from the starting position specified in this method. Then method chaining is used to specify the section of the path. At the end complete has to be called to actually draw the shape. See the class description of thePath
class for an example.- Parameters:
x
- the starting point of the path relative to the top left corner of the canvas.y
- the starting point of the path relative to the top left corner of the canvas.- Returns:
- a path object with which more complicated shapes can be constructed.
-
drawImage
Draws an image at the specified (x, y) coordinate. The width and height of the image will be used to draw the image.- Parameters:
x
- The distance in pixel from the left side of the canvas to the left side of the image.y
- The distance in pixel from the top side of the canvas to the top side of the image.image
- Any image.
-
drawImage
Draws an image at the specified (x, y) coordinate. The image will be rescaled to fit within the width and height given as parameters.- Parameters:
x
- The distance in pixel from the left side of the canvas to the left side of the image.y
- The distance in pixel from the top side of the canvas to the top side of the image.width
- The width of the image on the canvas.height
- The height of the image on the canvas.image
- Any image.
-
drawImage
public void drawImage(double x, double y, double width, double height, Image image, Interpolation interpolation) Draws an image at the specified (x, y) coordinate. The image will be rescaled to fit within the width and height given as parameters.- Parameters:
x
- The distance in pixel from the left side of the canvas to the left side of the image.y
- The distance in pixel from the top side of the canvas to the top side of the image.width
- The width of the image on the canvas.height
- The height of the image on the canvas.image
- Any image.interpolation
- Defines the way the images is interpolated when scaled. SeeInterpolation
.
-
clear
public void clear()Colors the whole canvas in white. -
clear
Colors the whole canvas in the color given as a parameter.- Parameters:
color
- The color the canvas will be colored in.
-
copyTo
Copies this image onto the graphics object. Also applies an interpolation rendering hint if the graphics target is an instance of Graphics2D.- Parameters:
target
- a graphics object.interpolation
- Defines the way the images is interpolated when drawn onto the graphics object. SeeInterpolation
.
-
toBufferedImage
Creates a copy of this image in the form of aBufferedImage
.- Parameters:
type
- Defines the way the image is encoded in memory.- Returns:
- a BufferedImage.
-
toBufferedImage
Creates a copy of this image in the form of aBufferedImage
. TheBufferedImage
type INT_ARGB is chosen as a default.- Returns:
- a BufferedImage.
-
beforeDrawing
protected void beforeDrawing()This method is called each time right before a shape is drawn. You can override this method to implement custom behavior when drawing in the inheriting class. -
afterDrawing
protected void afterDrawing()This method is called each time right after a shape is drawn. You can override this method to implement custom behavior when drawing in the inheriting class.
-