Package codedraw

Class CodeDraw

java.lang.Object
codedraw.Image
codedraw.CodeDraw
All Implemented Interfaces:
AutoCloseable

public class CodeDraw extends Image implements AutoCloseable
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.

The source code can be found in the CodeDraw repository.
If you are unfamiliar with graphical output and want more details you can read the Introduction to CodeDraw.
For the JavaDoc visit CodeDrawJavaDoc.
Here is 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 cd.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
  • Constructor Details

    • CodeDraw

      public CodeDraw()
      Creates a canvas with size 600x600 pixels. The window 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 window surrounding the canvas will be slightly bigger. Once the size is set via this constructor it remains fixed.
      Parameters:
      canvasWidth - must be at least 1 pixel
      canvasHeight - must be at least 1 pixel
  • Method Details

    • show

      public static CodeDraw show(Image image)
      Shows the specified image in a CodeDraw window fitting the size of the image.
      Parameters:
      image - any image that should be displayed.
      Returns:
      the CodeDraw object that displays the image.
    • run

      public static void run(Animation animation)
      Runs the Animation interface using a CodeDraw window. This function returns when the CodeDraw window is closed by the user. The animation will appear with a size of 600 by 600 pixel running at 60 frames per second and 60 simulation per second.
      Parameters:
      animation - any class implementing the animation interface.
    • run

      public static void run(Animation animation, int width, int height)
      Runs the Animation interface using a CodeDraw window. This function returns when the CodeDraw window is closed by the user. The animation will appear with a size of 600 by 600 pixel running at 60 frames per second and 60 simulation per second.
      Parameters:
      animation - any class implementing the animation interface.
      width - the width of the CodeDraw window.
      height - the height of the CodeDraw window.
    • run

      public static void run(Animation animation, int width, int height, int framesPerSecond)
      Runs the Animation interface using a CodeDraw window. This function returns when the CodeDraw window is closed by the user. The animation will appear with a size of 600 by 600 pixel running at 60 frames per second and 60 simulation per second.
      Parameters:
      animation - any class implementing the animation interface.
      width - the width of the CodeDraw window.
      height - the height of the CodeDraw window.
      framesPerSecond - the rate at which the Animation.draw(Image) method should be called.
    • run

      public static void run(Animation animation, int width, int height, int framesPerSecond, int simulationsPerSecond)
      Runs the Animation interface using a CodeDraw window. This function returns when the CodeDraw window is closed by the user. The animation will appear with a size of 600 by 600 pixel running at 60 frames per second and 60 simulation per second.
      Parameters:
      animation - any class implementing the animation interface.
      width - the width of the CodeDraw window.
      height - the height of the CodeDraw window.
      framesPerSecond - the rate at which the Animation.draw(Image) method should be called.
      simulationsPerSecond - the rate at which the Animation.simulate() method should be called.
    • getEventScanner

      public EventScanner getEventScanner()
      Gets the EventScanner of this CodeDraw window. See the EventScanner for more details on how to use it.
      Returns:
      an EventScanner.
    • isInstantDraw

      public boolean isInstantDraw()
      When InstantDraw is enabled CodeDraw will immediately draw all shapes to the canvas without calling show(). When InstantDraw is disabled CodeDraw will only draw shapes to the window once show() is called. InstantDraw is disabled per default.
      Returns:
      whether InstantDraw is enabled.
    • setInstantDraw

      public void setInstantDraw(boolean isInstantDraw)
      When InstantDraw is enabled CodeDraw will immediately draw all shapes to the canvas without calling show(). When InstantDraw is disabled CodeDraw will only draw shapes to the window once show() is called. InstantDraw is disabled per default.
      Parameters:
      isInstantDraw - defines whether InstantDraw is enabled.
    • isAlwaysOnTop

      public boolean isAlwaysOnTop()
      Always on top defines whether this window is placed on top of all other windows.
      Returns:
      whether the CodeDraw window is always displayed on top of other windows.
    • setAlwaysOnTop

      public void setAlwaysOnTop(boolean isAlwaysOnTop)
      When set to true this CodeDraw window will always be displayed on top of other windows. When set to false this CodeDraw window will disappear behind other windows when CodeDraw loses focus.
      Parameters:
      isAlwaysOnTop - defines whether this CodeDraw window is displayed on top of other windows.
    • getCanvasPositionX

      public int getCanvasPositionX()
      Gets the distance in pixel from the top left corner of the default screen to the top left corner of the 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 default screen to the left of the CodeDraw canvas.
    • getCanvasPositionY

      public int getCanvasPositionY()
      Gets the distance in pixel from the top left corner of the default screen to the top left corner of the 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 default 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 default screen to the top left corner of the 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 default 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 default screen to the top left corner of the 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 default screen to the top of the CodeDraw canvas.
    • getWindowPositionX

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

      public int getWindowPositionY()
      Gets the distance in pixel from the top left corner of the default screen to the top left corner of the CodeDraw window. Changing the window position also changes the canvas position.
      Returns:
      The distance in pixel from the top side of the default 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 default screen to the top left corner of the CodeDraw window. Changing the window position also changes the canvas position.
      Parameters:
      x - The distance in pixel from the left side of the default 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 default screen to the top left corner of the CodeDraw window. Changing the window position also changes the canvas position.
      Parameters:
      y - The distance in pixel from the top side of the default screen to the top of the CodeDraw window.
    • getTitle

      public String getTitle()
      The title is the text displayed in the top left corner of the CodeDraw window. It is also the description displayed in many places on your operating system.
      Returns:
      the text of the title.
    • setTitle

      public void setTitle(String title)
      The title is the text displayed in the top left corner of the CodeDraw window. It is also the description displayed in many places on your operating system.
      Parameters:
      title - Sets the text of the title.
    • getCursorStyle

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

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

      public void show()
      Displays all the shapes and images that were drawn onto the canvas. Since showing the drawn elements in the CodeDraw window is slow, calling this method frequently will slow down your program.
    • show

      public void show(long waitMilliseconds)
      Displays all the shapes and images that were drawn onto the canvas and waits for the given amount of milliseconds. Since showing the drawn elements in the CodeDraw window is slow, calling this method frequently will slow down your program. This method might take longer to return from show if you specify only a small amount of milliseconds. 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
      Parameters:
      waitMilliseconds - Minimum time it takes this function to return.
    • isClosed

      public boolean isClosed()
      Checks whether this CodeDraw window is already closed. The window can close if the user closes the window or when the close() method is called.
      Returns:
      whether this CodeDraw window is closed.
    • close

      public void close()
      Closes the window and disposes all resources associated with this instance. Any methods associated with the graphical user interface can no longer be accessed afterwards.
      Specified by:
      close in interface AutoCloseable
    • close

      public void close(boolean terminateProcess)
      Closes the window and disposes all resources associated with this instance. Any methods associated with the graphical user interface can no longer be accessed afterwards.
      Parameters:
      terminateProcess - 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 String toString()
      Overrides:
      toString in class Object
    • afterDrawing

      protected void afterDrawing()
      Description copied from class: Image
      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.
      Overrides:
      afterDrawing in class Image