Package codedraw

Class CodeDraw

java.lang.Object
codedraw.CodeDraw

public class CodeDraw
extends java.lang.Object
CodeDraw is an easy-to-use drawing library where you use code to create pictures and animations. It is made for beginners that understand little about programming and makes it very simple to draw and animate various shapes and images to a canvas.

You can view the CodeDraw repository here.
To get a brief overview and a bunch of examples visit the README of the CodeDraw repository.
Read the introduction to CodeDraw for a beginners guide to CodeDraw.
For the JavaDoc visit CodeDrawJavaDoc.
An example to get you started:

 import codedraw.*;

 public class MyProgram {
     public static void main(String[] args) {
         // Creates a new CodeDraw window with the size of 600x600 pixel
         CodeDraw cd = new CodeDraw();

         // Sets the drawing color to red
         cd.setColor(Palette.RED);
         // Draws the outline of a rectangle
         cd.drawRectangle(100, 100, 200, 100);
         // Draws a filled Square
         cd.fillSquare(180, 150, 80);

         // Changes the color to light blue
         cd.setColor(Palette.LIGHT_BLUE);
         cd.fillCircle(300, 200, 50);

         // Finally, the method show must be called
         // to display the drawn shapes in the CodeDraw window.
         cd.show();
     }
 }
 
Fun Fact: You can copy the currently displayed canvas to your clipboard by pressing Ctrl + C
Author:
Niklas Krassnig, Nikolaus Kasyan
  • Constructor Summary

    Constructors
    Constructor Description
    CodeDraw()
    Creates a canvas with size 600x600 pixels.
    CodeDraw​(int canvasWidth, int canvasHeight)
    Creates a canvas with the specified size.
  • Method Summary

    Modifier and Type Method Description
    void clear()
    Colors the whole canvas in white.
    void clear​(java.awt.Color color)
    Colors the whole canvas in the color given as a parameter.
    void dispose()
    Closes the frame and disposes all created resources associated with this CodeDraw instance.
    void dispose​(boolean exit)
    Closes the frame and disposes all created resources associated with this CodeDraw instance.
    void drawArc​(double x, double y, double radius, double startRadians, double sweepRadians)
    Draws the outline of an arc with the center being the (x, y) coordinates.
    void drawArc​(double x, double y, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians)
    Draws the outline of an arc with the center being the (x, y) coordinates.
    void drawBezier​(double startX, double startY, double control1X, double control1Y, double control2X, double control2Y, double endX, double endY)
    Draws a cubic bezier curve.
    void drawCircle​(double x, double y, 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 bezier curve.
    void drawEllipse​(double x, double y, double horizontalRadius, double verticalRadius)
    Draws the outline of an ellipse.
    void drawImage​(double x, double y, double width, double height, java.awt.Image image)
    Draws an image at the specified (x, y) coordinate.
    void drawImage​(double x, double y, double width, double height, java.io.File file)
    Draws an image at the specified (x, y) coordinate.
    void drawImage​(double x, double y, double width, double height, java.lang.String fileName)
    Draws an image at the specified (x, y) coordinate.
    void drawImage​(double x, double y, java.awt.Image image)
    Draws an image at the specified (x, y) coordinate.
    void drawImage​(double x, double y, java.io.File file)
    Draws an image at the specified (x, y) coordinate.
    void drawImage​(double x, double y, java.lang.String fileName)
    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.
    void drawPie​(double x, double y, double radius, double startRadians, double sweepRadians)
    Draws the outline of a pie with the center being the (x, y) coordinates.
    void drawPie​(double x, double y, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians)
    Draws the outline of a pie with the center being the (x, y) coordinates.
    void drawPixel​(double x, double y)
    Draws a point which is exactly 1x1 pixel in size.
    void drawPoint​(double x, double y)
    Draws a point which changes size depending on the getLineWidth()
    void drawPolygon​(double... points)
    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, java.lang.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 x, double y, double radius)
    Draws a filled circle.
    void fillEllipse​(double x, double y, double horizontalRadius, double verticalRadius)
    Draws a filled ellipse.
    void fillPie​(double x, double y, double radius, double startRadians, double sweepRadians)
    Draws a filled pie with the center being the (x, y) coordinates.
    void fillPie​(double x, double y, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians)
    Draws a filled pie with the center being the (x, y) coordinates.
    void fillPolygon​(double... points)
    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.
    int getCanvasPositionX()
    Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas.
    int getCanvasPositionY()
    Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas.
    java.awt.Color getColor()
    Defines the color that is used for drawing all shapes.
    Corner getCorner()
    Defines how the corners of drawn shapes should look.
    CursorStyle getCursorStyle()
    Defines the style of the cursor while hovering of the CodeDraw canvas.
    int getHeight()
    This value cannot be changed once set via the constructor.
    double getLineWidth()
    Defines the width or thickness of drawn shapes and lines.
    TextFormat getTextFormat()
    Defines the styling of drawn text.
    java.lang.String getTitle()
    The title is the text displayed in the top left corner of the CodeDraw window.
    int getWidth()
    This value cannot be changed once set via the constructor.
    int getWindowPositionX()
    Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window.
    int getWindowPositionY()
    Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window.
    boolean isAntiAliased()
    Defines whether draw text, drawn shapes and filled shapes are anti-aliased.
    void isAntiAliased​(boolean isAntiAliased)
    Defines whether drawn text, drawn shapes and filled shapes are anti-aliased.
    Subscription onKeyDown​(EventHandler<CodeDraw,​java.awt.event.KeyEvent> handler)
    Trigger exactly once when a key is pressed down.
    Subscription onKeyPress​(EventHandler<CodeDraw,​java.awt.event.KeyEvent> handler)
    onKeyPress will continuously trigger while a key is being held down.
    Subscription onKeyUp​(EventHandler<CodeDraw,​java.awt.event.KeyEvent> handler)
    Trigger when a key is released.
    Subscription onMouseClick​(EventHandler<CodeDraw,​java.awt.event.MouseEvent> handler)
    Triggers once when a mouse button is pressed down and quickly released again.
    Subscription onMouseDown​(EventHandler<CodeDraw,​java.awt.event.MouseEvent> handler)
    Triggers exactly once when a mouse button is pressed down.
    Subscription onMouseEnter​(EventHandler<CodeDraw,​java.awt.event.MouseEvent> handler)
    Triggers when the mouse enters the canvas.
    Subscription onMouseLeave​(EventHandler<CodeDraw,​java.awt.event.MouseEvent> handler)
    Triggers when the mouse leaves the canvas.
    Subscription onMouseMove​(EventHandler<CodeDraw,​java.awt.event.MouseEvent> handler)
    Triggers continuously while the mouse is being moved.
    Subscription onMouseUp​(EventHandler<CodeDraw,​java.awt.event.MouseEvent> handler)
    Triggers when a mouse button is released.
    Subscription onMouseWheel​(EventHandler<CodeDraw,​java.awt.event.MouseWheelEvent> handler)
    Triggers each time the mouse wheel is turned.
    Subscription onWindowMove​(EventHandler<CodeDraw,​java.awt.event.ComponentEvent> handler)
    Triggers every time the CodeDraw window is moved.
    java.awt.image.BufferedImage saveCanvas()
    Creates a copy of the current buffer (not the displayed image) and returns it as a buffered image.
    void setCanvasPositionX​(int x)
    Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas.
    void setCanvasPositionY​(int y)
    Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas.
    void setColor​(java.awt.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 setCursorStyle​(CursorStyle cursorStyle)
    Defines the style of the cursor while hovering of the CodeDraw canvas.
    void setLineWidth​(double lineWidth)
    Defines the width or thickness of drawn shapes and lines.
    void setTextFormat​(TextFormat textFormat)
    Defines the styling of drawn text.
    void setTitle​(java.lang.String title)
    The title is the text displayed in the top left corner of the CodeDraw window.
    void setWindowPositionX​(int x)
    Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window.
    void setWindowPositionY​(int y)
    Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window.
    void show()
    Displays the drawn shapes and images on the canvas.
    void show​(int waitMilliseconds)
    Displays the drawn shapes and images on the canvas and then waits for the given amount of milliseconds.
    java.lang.String toString()  

    Methods inherited from class java.lang.Object

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

    • CodeDraw

      public CodeDraw()
      Creates a canvas with size 600x600 pixels. The frame surrounding the canvas will be slightly bigger. The size remains fixed after calling this constructor.
    • CodeDraw

      public CodeDraw​(int canvasWidth, int canvasHeight)
      Creates a canvas with the specified size. The frame surrounding the canvas will be slightly bigger. Once the size is set via this constructor it remains fixed.
      Parameters:
      canvasWidth - must be at least 150 pixel
      canvasHeight - must be at least 1 pixel
  • Method Details

    • getWindowPositionX

      public int getWindowPositionX()
      Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window. Changing the window position also changes the canvas position.
      Returns:
      The distance in pixel from the left side of the main screen to the left of the CodeDraw window.
    • getWindowPositionY

      public int getWindowPositionY()
      Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window. Changing the window position also changes the canvas position.
      Returns:
      The distance in pixel from the top side of the main screen to the top of the CodeDraw window.
    • setWindowPositionX

      public void setWindowPositionX​(int x)
      Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window. Changing the window position also changes the canvas position.
      Parameters:
      x - The distance in pixel from the left side of the main screen to the left of the CodeDraw window.
    • setWindowPositionY

      public void setWindowPositionY​(int y)
      Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window. Changing the window position also changes the canvas position.
      Parameters:
      y - The distance in pixel from the top side of the main screen to the top of the CodeDraw window.
    • getCanvasPositionX

      public int getCanvasPositionX()
      Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas. The top left corner of the canvas is the origin point for all drawn objects. Changing the canvas position also changes the window position.
      Returns:
      The distance in pixel from the left side of the main screen to the left of the CodeDraw canvas.
    • getCanvasPositionY

      public int getCanvasPositionY()
      Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas. The top left corner of the canvas is the origin point for all drawn objects. Changing the canvas position also changes the window position.
      Returns:
      The distance in pixel from the top side of the main screen to the top of the CodeDraw canvas.
    • setCanvasPositionX

      public void setCanvasPositionX​(int x)
      Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas. The top left corner of the canvas is the origin point for all drawn objects. Changing the canvas position also changes the window position.
      Parameters:
      x - The distance in pixel from the left side of the main screen to the left of the CodeDraw canvas.
    • setCanvasPositionY

      public void setCanvasPositionY​(int y)
      Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas. The top left corner of the canvas is the origin point for all drawn objects. Changing the canvas position also changes the window position.
      Parameters:
      y - The distance in pixel from the top side of the main screen to the top of the CodeDraw canvas.
    • getWidth

      public int getWidth()
      This value cannot be changed once set via the constructor.
      Returns:
      Returns the width of the canvas.
    • getHeight

      public int getHeight()
      This value cannot be changed once set via the constructor.
      Returns:
      Returns the height of the canvas.
    • getLineWidth

      public double getLineWidth()
      Defines the width or thickness of drawn shapes and lines.
      Returns:
      Returns the lineWidth of this CodeDraw window.
    • setLineWidth

      public void setLineWidth​(double lineWidth)
      Defines the width or thickness of drawn shapes and lines.
      Parameters:
      lineWidth - Sets the lineWidth of this CodeDraw window.
    • getTextFormat

      public TextFormat getTextFormat()
      Defines the styling of drawn text. See also drawText(double, double, String)
      Returns:
      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)
      Parameters:
      textFormat - Sets the text formatting options of this CodeDraw window.
    • getCursorStyle

      public CursorStyle getCursorStyle()
      Defines the style of the cursor while hovering of the CodeDraw canvas. See also CursorStyle.
      Returns:
      Returns the cursor style of this CodeDraw canvas.
    • setCursorStyle

      public void setCursorStyle​(CursorStyle cursorStyle)
      Defines the style of the cursor while hovering of the CodeDraw canvas. See also CursorStyle.
      Parameters:
      cursorStyle - Sets the cursor style of this CodeDraw canvas.
    • isAntiAliased

      public boolean isAntiAliased()
      Defines whether draw text, drawn shapes and filled shapes are anti-aliased. See Wikipedia Spatial Anti-aliasing
      Returns:
      Whether this CodeDraw window anti aliases.
    • isAntiAliased

      public void isAntiAliased​(boolean isAntiAliased)
      Defines whether drawn text, drawn shapes and filled shapes are anti-aliased. See Wikipedia Spatial Anti-aliasing
      Parameters:
      isAntiAliased - Sets whether this CodeDraw window anti aliases.
    • getCorner

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

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

      public java.lang.String getTitle()
      The title is the text displayed in the top left corner of the CodeDraw window.
      Returns:
      Returns the text of the title.
    • setTitle

      public void setTitle​(java.lang.String title)
      The title is the text displayed in the top left corner of the CodeDraw window.
      Parameters:
      title - Sets the text of the title.
    • getColor

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

      public void setColor​(java.awt.Color color)
      Defines the color that is used for drawing all shapes.
      Parameters:
      color - Sets the drawing color of this CodeDraw window.
    • onMouseClick

      public Subscription onMouseClick​(EventHandler<CodeDraw,​java.awt.event.MouseEvent> handler)
      Triggers once when a mouse button is pressed down and quickly released again.
      Parameters:
      handler - A lambda or function reference.
    • onMouseMove

      public Subscription onMouseMove​(EventHandler<CodeDraw,​java.awt.event.MouseEvent> handler)
      Triggers continuously while the mouse is being moved.
      Parameters:
      handler - A lambda or function reference.
    • onMouseDown

      public Subscription onMouseDown​(EventHandler<CodeDraw,​java.awt.event.MouseEvent> handler)
      Triggers exactly once when a mouse button is pressed down.
      Parameters:
      handler - A lambda or function reference.
    • onMouseUp

      public Subscription onMouseUp​(EventHandler<CodeDraw,​java.awt.event.MouseEvent> handler)
      Triggers when a mouse button is released.
      Parameters:
      handler - A lambda or function reference.
    • onMouseEnter

      public Subscription onMouseEnter​(EventHandler<CodeDraw,​java.awt.event.MouseEvent> handler)
      Triggers when the mouse enters the canvas.
      Parameters:
      handler - A lambda or function reference.
    • onMouseLeave

      public Subscription onMouseLeave​(EventHandler<CodeDraw,​java.awt.event.MouseEvent> handler)
      Triggers when the mouse leaves the canvas.
      Parameters:
      handler - A lambda or function reference.
    • onMouseWheel

      public Subscription onMouseWheel​(EventHandler<CodeDraw,​java.awt.event.MouseWheelEvent> handler)
      Triggers each time the mouse wheel is turned.
      Parameters:
      handler - A lambda or function reference.
    • onKeyDown

      public Subscription onKeyDown​(EventHandler<CodeDraw,​java.awt.event.KeyEvent> handler)
      Trigger exactly once when a key is pressed down.
      Parameters:
      handler - A lambda or function reference.
    • onKeyUp

      public Subscription onKeyUp​(EventHandler<CodeDraw,​java.awt.event.KeyEvent> handler)
      Trigger when a key is released.
      Parameters:
      handler - A lambda or function reference.
    • onKeyPress

      public Subscription onKeyPress​(EventHandler<CodeDraw,​java.awt.event.KeyEvent> handler)
      onKeyPress will continuously trigger while a key is being held down.
      Parameters:
      handler - A lambda or function reference.
    • onWindowMove

      public Subscription onWindowMove​(EventHandler<CodeDraw,​java.awt.event.ComponentEvent> handler)
      Triggers every time the CodeDraw window is moved.
      Parameters:
      handler - A lambda or function reference.
    • drawText

      public void drawText​(double x, double y, java.lang.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. 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.
    • drawPixel

      public void drawPixel​(double x, double y)
      Draws a point which is exactly 1x1 pixel in size.
      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.
    • drawPoint

      public void drawPoint​(double x, double y)
      Draws a point which changes size depending on the getLineWidth()
      Parameters:
      x - The distance in pixel from the left side of the canvas to the center of the point.
      y - 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.
      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 bezier 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.
      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 bezier 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 parameter specify in what way the curve will be bent.
      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.
      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.
      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.
      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.
      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 x, double y, double radius)
      Draws the outline of a circle. The center of the circle will be at the specified (x, y) coordinate.
      Parameters:
      x - The distance in pixel from the left side of the canvas to the center of the circle.
      y - 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 x, double y, double radius)
      Draws a filled circle. The center of the circle will be at the specified (x, y) coordinate.
      Parameters:
      x - The distance in pixel from the left side of the canvas to the center of the circle.
      y - 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 x, double y, double horizontalRadius, double verticalRadius)
      Draws the outline of an ellipse. The center of the ellipse will be at the specified (x, y) coordinate.
      Parameters:
      x - The distance in pixel from the left side of the canvas to the center of the ellipse.
      y - 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 x, double y, double horizontalRadius, double verticalRadius)
      Draws a filled ellipse. The center of the ellipse will be at the specified (x, y) coordinate.
      Parameters:
      x - The distance in pixel from the left side of the canvas to the center of the ellipse.
      y - 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 x, double y, double radius, double startRadians, double sweepRadians)
      Draws the outline of an arc with the center being the (x, y) coordinates. The arc starts at the 12 o'clock position offset by the startRadians parameter. The total length of the arc is defined by the sweepRadians parameter.
      Parameters:
      x - The distance in pixel from the left side of the canvas to the center of the arc.
      y - 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 12 o'clock going clock-wise.
      sweepRadians - The length of the arc in radians from the start angle in a clockwise direction.
    • drawArc

      public void drawArc​(double x, double y, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians)
      Draws the outline of an arc with the center being the (x, y) coordinates. The width is 2 * horizontalRadius and the height is 2 * verticalRadius. The arc starts at the 12 o'clock position offset by the startRadians parameter. The total length of the arc is defined by the sweepRadians parameter.
      Parameters:
      x - The distance in pixel from the left side of the canvas to the center of the arc.
      y - 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 12 o'clock going clock-wise.
      sweepRadians - The length of the arc in radians from the start angle in a clockwise direction.
    • drawPie

      public void drawPie​(double x, double y, double radius, double startRadians, double sweepRadians)
      Draws the outline of a pie with the center being the (x, y) coordinates. The pie starts at the 12 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter.
      Parameters:
      x - The distance in pixel from the left side of the canvas to the center of the pie.
      y - 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 12 o'clock going clock-wise.
      sweepRadians - The length of the pie in radians from the start angle in a clockwise direction.
    • drawPie

      public void drawPie​(double x, double y, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians)
      Draws the outline of a pie with the center being the (x, y) coordinates. The width is 2 * horizontalRadius and the height is the 2 * verticalRadius. The pie starts at the 12 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter.
      Parameters:
      x - The distance in pixel from the left side of the canvas to the center of the pie.
      y - 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 12 o'clock going clock-wise.
      sweepRadians - The length of the pie in radians from the start angle in a clockwise direction.
    • fillPie

      public void fillPie​(double x, double y, double radius, double startRadians, double sweepRadians)
      Draws a filled pie with the center being the (x, y) coordinates. The pie starts at the 12 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter.
      Parameters:
      x - The distance in pixel from the left side of the canvas to the center of the pie.
      y - 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 12 o'clock going clock-wise.
      sweepRadians - The length of the pie in radians from the start angle in a clockwise direction.
    • fillPie

      public void fillPie​(double x, double y, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians)
      Draws a filled pie with the center being the (x, y) coordinates. The width is 2 * horizontalRadius and the height is the 2 * verticalRadius. The pie starts at the 12 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter.
      Parameters:
      x - The distance in pixel from the left side of the canvas to the center of the pie.
      y - 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 12 o'clock going clock-wise.
      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.
      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... points)
      Draws the outline of a polygon. This function must be called with an even number of parameters. Each parameter pair (x, y) represents a corner of the polygon. Must be called with at least two points as parameter. Each point passed to drawPolygon will be connected to the following points and the last point will be connected to the first point.
      
       cd.drawPolygon(
           200, 100,
           100, 200,
           300, 200
       );
       
      Parameters:
      points - An even number of doubles. Each pair represents one corner of the polygon.
    • fillPolygon

      public void fillPolygon​(double... points)
      Draws a filled polygon. This function must be called with an even number of parameters. Each parameter pair (x, y) represents a corner of the polygon. Must be called with at least two points as parameter. Each point passed to drawPolygon will be connected to the following points and the last point will be connected to the first point.
      
       cd.fillPolygon(
           200, 100,
           100, 200,
           300, 200
       );
       
      Parameters:
      points - An even number of doubles. Each pair represents one corner of the polygon.
    • drawImage

      public void drawImage​(double x, double y, java.awt.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.
      
       CodeDraw cd = new CodeDraw();
      
       BufferedImage img;
       try {
           img = ImageIO.read(new File("C:\\pathToDirectory\\filename.png"));
       } catch (IOException e) {
           throw new UncheckedIOException(e);
       }
      
       cd.drawImage(100, 100, img);
       cd.show();
       
      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 - The image that will be drawn on the canvas.
    • drawImage

      public void drawImage​(double x, double y, java.io.File file)
      Draws an image at the specified (x, y) coordinate. The width and height of the image will be used to draw the image.
      
       CodeDraw cd = new CodeDraw();
      
       cd.drawImage(100, 100, new File("C:\\pathToDirectory\\filename.png"));
       cd.show();
       

      Supported image formats are: .jpg or .jpeg (JPEG), .bmp (Bitmap), .gif (Graphics Interchange Format), .png (Portable Network Graphic) and .wbmp (Wireless Application Protocol Bitmap Format).
      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.
      file - A file that points to an image file. See ImageIO.read(File)
    • drawImage

      public void drawImage​(double x, double y, java.lang.String fileName)
      Draws an image at the specified (x, y) coordinate. The width and height of the image will be used to draw the image.
      
       CodeDraw cd = new CodeDraw();
      
       cd.drawImage(100, 100, "C:\\pathToDirectory\\filename.png");
       cd.show();
       

      Supported image formats are: .jpg or .jpeg (JPEG), .bmp (Bitmap), .gif (Graphics Interchange Format), .png (Portable Network Graphic) and .wbmp (Wireless Application Protocol Bitmap Format).
      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.
      fileName - A fileName that points to an image file. See ImageIO.read(File)
    • drawImage

      public void drawImage​(double x, double y, double width, double height, java.awt.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.
      
       CodeDraw cd = new CodeDraw();
      
       BufferedImage img;
       try {
           img = ImageIO.read(new File("C:\\pathToDirectory\\filename.png"));
       } catch (IOException e) {
           throw new UncheckedIOException(e);
       }
      
       cd.drawImage(100, 100, 200, 200, img);
       cd.show();
       
      The size of the example image will be 200x200 pixel.
      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 - The image that will be drawn on the canvas.
    • drawImage

      public void drawImage​(double x, double y, double width, double height, java.io.File file)
      Draws an image at the specified (x, y) coordinate. The image will be rescaled to fit within the width and height given as parameters.
      
       CodeDraw cd = new CodeDraw();
      
       cd.drawImage(100, 100, 200, 200, new File("C:\\pathToDirectory\\filename.png"));
       cd.show();
       
      The size of the example image will be 200x200 pixel.
      Supported image formats are: .jpg or .jpeg (JPEG), .bmp (Bitmap), .gif (Graphics Interchange Format), .png (Portable Network Graphic) and .wbmp (Wireless Application Protocol Bitmap Format).
      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.
      file - A file that points to an image file. See ImageIO.read(File)
    • drawImage

      public void drawImage​(double x, double y, double width, double height, java.lang.String fileName)
      Draws an image at the specified (x, y) coordinate. The image will be rescaled to fit within the width and height given as parameters.
      
       CodeDraw cd = new CodeDraw();
      
       cd.drawImage(100, 100, 200, 200, "C:\\pathToDirectory\\filename.png");
       cd.show();
       
      The size of the example image will be 200x200 pixel.
      Supported image formats are: .jpg or .jpeg (JPEG), .bmp (Bitmap), .gif (Graphics Interchange Format), .png (Portable Network Graphic) and .wbmp (Wireless Application Protocol Bitmap Format).
      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.
      fileName - A fileName that points to an image file. See ImageIO.read(File)
    • saveCanvas

      public java.awt.image.BufferedImage saveCanvas()
      Creates a copy of the current buffer (not the displayed image) and returns it as a buffered image. A BufferedImage can be saved as a file with the following code:
      
       CodeDraw cd = new CodeDraw();
      
       // some drawing occurs here
      
       try {
            ImageIO.write(
                cd.saveCanvas(),
                "png",
                new File("C:\\pathToDirectory\\filename.png")
            );
       } catch (IOException e) {
            throw new UncheckedIOException(e);
       }
       
      Fun Fact: You can copy the currently displayed canvas to your clipboard by pressing Ctrl + C
      .
      Returns:
      The current buffer as an image object.
    • clear

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

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

      public void show()
      Displays the drawn shapes and images on the canvas. Calling show while executing an event is generally not recommended because rendering is slow and this will slow down you program. Instead, you should call show from the main thread.
    • show

      public void show​(int waitMilliseconds)
      Displays the drawn shapes and images on the canvas and then waits for the given amount of milliseconds. The copying of the buffer to the screen also takes a bit of time and that will be the minimum time it takes for this method to return.
      The amount of milliseconds this method must be called with to display a certain amount of frames per second:
      30 fps ~ 33ms
      60 fps ~ 16ms
      120 fps ~ 8ms
      Calling show or sleeping while executing an event will block all other events from executing. It is generally not recommended. Instead, you should call show from the main thread.
      Parameters:
      waitMilliseconds - Minimum time it takes this function to return.
    • dispose

      public void dispose()
      Closes the frame and disposes all created resources associated with this CodeDraw instance. Terminates the process when all CodeDraw instances are closed. To prevent termination pass false to this function. See dispose(boolean).
    • dispose

      public void dispose​(boolean exit)
      Closes the frame and disposes all created resources associated with this CodeDraw instance.
      Parameters:
      exit - When true terminates the process when all CodeDraw instances are closed. When false lets the process continue even though all CodeDraw instances have been closed.
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object