Package codedraw

Class Image

java.lang.Object
codedraw.Image
Direct Known Subclasses:
BorderlessWindow, CodeDraw, FullScreen

public class Image extends Object
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

    Constructors
    Constructor
    Description
    Image(int width, int height)
    Creates a white image of the specified size.
    Image(int width, int height, Color backgroundColor)
    Creates an image of the specified size with a custom background color.
    Image(Image image)
    Creates a copy of the image supplied as a parameter.
    Image(Image image)
    Creates an image from the Image supplied as a parameter.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected 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
    Colors the whole canvas in white.
    void
    clear(Color color)
    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
    crop(Image source, int x, int y, int width, int height)
    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
    drawImage(double x, double y, double width, double height, Image image)
    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
    drawImage(double x, double y, Image image)
    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
    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
    drawText(double x, double y, String text)
    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
    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
    fromFile(String pathToImage)
    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.
    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
    This width of this image.
    boolean
    Defines whether drawn text and shapes are anti-aliased.
    static Image
    Mirrors the image horizontally.
    static Image
    Mirrors the image vertically.
    static Image
    Rotates the image clockwise.
    static 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
    scale(Image source, double scale)
    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
    setColor(Color color)
    Defines the color that is used for drawing all shapes.
    void
    setCorner(Corner corner)
    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
    setPixel(int x, int y, Color color)
    Draws a point which is the size of one pixel.
    void
    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 a BufferedImage.
    Creates a copy of this image in the form of a BufferedImage.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Image

      public Image(Image 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

      public Image(Image image)
      Creates an image from the Image 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

      public Image(int width, int height, Color backgroundColor)
      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

      public static Image fromFile(String pathToImage)
      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

      public static Image fromUrl(String url)
      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.
      
       Image image = Image.fromUrl("https://exmaple.com/example-image.png");
       
      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(URL) is used to load images from the network.
      Parameters:
      url - Link to the image file.
      Returns:
      An image.
    • fromResource

      public static Image fromResource(String resourceName)
      Loads images from the resource folder.
      
       Image image = Image.fromResource("./my_image.png");
       
      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(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

      public static Image fromBase64String(String base64)
      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) and Base64.Decoder.decode(String) are used to convert the image.
      Parameters:
      base64 - a Base64 string.
      Returns:
      an image.
    • fromDPIAwareSize

      public static Image fromDPIAwareSize(int width, int height)
      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

      public static void save(Image image, String pathToImage, ImageFormat format)
      Saves the image to the specified location using the specified image format. Supported formats are all values in ImageFormat. The formats ImageFormat.JPG, ImageFormat.JPEG and ImageFormat.BMP do not support transparency. ImageIO.write(RenderedImage, String, File) and File(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, choose ImageFormat.PNG and make sure that the file ends with ".png".
    • crop

      public static Image crop(Image source, int x, int y, int width, int height)
      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

      public static Image scale(Image source, double 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

      public static Image scale(Image source, double scale, Interpolation interpolation)
      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. See Interpolation.
      Returns:
      the scaled image.
      Throws:
      IllegalArgumentException - if the resulting image width or height is zero.
    • rotateClockwise

      public static Image rotateClockwise(Image image)
      Rotates the image clockwise.
      Parameters:
      image - The image to rotate.
      Returns:
      A copy of the image that is rotated 90° clockwise.
    • rotateCounterClockwise

      public static Image rotateCounterClockwise(Image image)
      Rotates the image counter-clockwise.
      Parameters:
      image - The image to rotate.
      Returns:
      A copy of the image that is rotated 90° counter-clockwise.
    • mirrorHorizontally

      public static Image mirrorHorizontally(Image image)
      Mirrors the image horizontally.
      Parameters:
      image - The image to mirror.
      Returns:
      A copy of the image that is mirrored along the horizontal axis.
    • mirrorVertically

      public static Image mirrorVertically(Image image)
      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

      public Color getColor()
      Defines the color that is used for drawing all shapes.
      Returns:
      the drawing color of this CodeDraw window.
    • setColor

      public void setColor(Color color)
      Defines the color that is used for drawing all shapes. Use the Palette 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

      public Corner getCorner()
      Defines how the corners of drawn shapes should look. See Corner for details.
      Returns:
      the corner style of this CodeDraw window.
    • setCorner

      public void setCorner(Corner corner)
      Defines how the corners of drawn shapes should look. See Corner for details.
      Parameters:
      corner - Sets the corner style of this CodeDraw window.
    • getTextFormat

      public TextFormat getTextFormat()
      Defines the styling of drawn text. See also drawText(double, double, String) on how the styling is applied.
      Returns:
      the text formatting options of this CodeDraw window.
    • setTextFormat

      public void setTextFormat(TextFormat textFormat)
      Defines the styling of drawn text. See also drawText(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

      public Matrix2D getTransformation()
      Gets the currently applied transformation of this image. See Matrix2D for details on linear transformations.
      Returns:
      a Matrix2D.
    • setTransformation

      public void setTransformation(Matrix2D transformation)
      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.
      
       cd.setTransformation(Matrix2D.IDENTITY.rotateAt(300, 300, Math.PI / 4);
       cd.drawText(300, 300, "Hello World!");
       
      See 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. See setTransformation(Matrix2D) and Matrix2D 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

      public Color getPixel(int x, int y)
      Returns the pixel color at the specified location. Ignores any transformation set by setTransformation(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

      public void setPixel(int x, int y, Color color)
      Draws a point which is the size of one pixel. Ignores any transformation set by setTransformation(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

      public void drawText(double x, double y, String text)
      Draws the text at the specified (x, y) coordinate. Formatting options can be set via the TextFormat object. See getTextFormat(), setTextFormat(TextFormat) and the TextFormat 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 the setLineWidth(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 the setLineWidth(double) method. The corners can be changed with setCorner(Corner). For details see the Corner 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 with setLineWidth(double). The corners can be changed with setCorner(Corner). For details see the Corner 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 with setLineWidth(double). The corners can be changed with setCorner(Corner). For details see the Corner 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 with setLineWidth(double). The corners can be changed with setCorner(Corner). For details see the Corner 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 with setCorner(Corner). For details see the Corner 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 with setLineWidth(double). The corners can be changed with setCorner(Corner). For details see the Corner 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 with setCorner(Corner). For details see the Corner 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 with setLineWidth(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 with setLineWidth(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 with setLineWidth(double).
      You can use Math.toRadians(double) to specify the angle in degrees.
      
       cd.drawArc(200, 200, 50, Math.toRadians(90), Math.toRadians(180));
       
      The line width can be changed with setLineWidth(double). The corners can be changed with setCorner(Corner). For details see the Corner 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 with setLineWidth(double).
      You can use Math.toRadians(double) to specify the angle in degrees.
      
       cd.drawArc(200, 200, 50, 50, Math.toRadians(90), Math.toRadians(180));
       
      The line width can be changed with setLineWidth(double). The corners can be changed with setCorner(Corner). For details see the Corner 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 with setLineWidth(double).
      You can use Math.toRadians(double) to specify the angle in degrees.
      
       cd.drawPie(200, 200, 50, Math.toRadians(90), Math.toRadians(180));
       
      The line width can be changed with setLineWidth(double). The corners can be changed with setCorner(Corner). For details see the Corner 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 with setLineWidth(double).
      You can use Math.toRadians(double) to specify the angle in degrees.
      
       cd.drawPie(200, 200, 50, 50, Math.toRadians(90), Math.toRadians(180));
       
      The line width can be changed with setLineWidth(double). The corners can be changed with setCorner(Corner). For details see the Corner 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 use Math.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 use Math.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 with setLineWidth(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.
      
       cd.drawPolygon(
           200, 100,
           100, 200,
           300, 200
       );
       
      The line width can be changed with 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

      public Path fillPathStartingAt(double x, double y)
      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 the Path 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

      public Path drawPathStartingAt(double x, double y)
      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 the Path 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

      public void drawImage(double x, double y, Image image)
      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

      public void drawImage(double x, double y, double width, double height, Image image)
      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. See Interpolation.
    • clear

      public void clear()
      Colors the whole canvas in white.
    • clear

      public void clear(Color color)
      Colors the whole canvas in the color given as a parameter.
      Parameters:
      color - The color the canvas will be colored in.
    • copyTo

      public void copyTo(Graphics target, Interpolation interpolation)
      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. See Interpolation.
    • toBufferedImage

      public BufferedImage toBufferedImage(BufferedImageType type)
      Creates a copy of this image in the form of a BufferedImage.
      Parameters:
      type - Defines the way the image is encoded in memory.
      Returns:
      a BufferedImage.
    • toBufferedImage

      public BufferedImage toBufferedImage()
      Creates a copy of this image in the form of a BufferedImage. The BufferedImage 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.