Package Green

Class Green

java.lang.Object
Green.Green

public final class Green extends Object
The main class of the library. This handles inputs, provides a few utility functions, and provides a World-independent way to call handleAct() and handleDraw().

A single instance of this class should be created in PApplet.setup() by supplying 'this' as the parameter.
Author:
Zacchary Dempsey-Plante
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static int
    Bilinear resizing algorithm.
    static int
    Nearest-neighbour resizing algorithm.
    static int
    Tile the image to fit (from top left), rather than adjusting it in any way.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Green​(PApplet theParent)
    Starts the backbone of the library.
  • Method Summary

    Modifier and Type
    Method
    Description
    static float
    cot​(float angle)
    Calculates the cotangent of angle in radians.
    static float
    csc​(float angle)
    Calculates the cosecant of angle in radians.
    float
    Retrieves the deltaTime - the time since the last frame.
    static int
    getDigits​(int value)
    Calculates the number of digits in a given integer.
    static Green
    Retrieves the current instance of the library that is running.
    static boolean
    getLinesIntersect​(float a1x, float a1y, float a2x, float a2y, float b1x, float b1y, float b2x, float b2y)
    Determines whether two line segments ([(a1x, a1y) - (a2x, a2y)] and [(b1x, b1y) - (b2x, b2y)]) intersect.
    int
    Retrieves the amount that the mouse scrolled in this frame.
    float
    Calculates the mouse's speed from last frame to this frame.
    Retrieves the parent instance of Processing.
    static float
    getPointsAngle​(float x1, float y1, float x2, float y2)
    Calculates the angle between two points ((x1, y1) and (x2, y2)).
    static float
    getPointsDist​(float x1, float y1, float x2, float y2)
    Calculates the distance between two points ((x1, y1) and (x2, y2)).
    static World
    Retrieves the currently-loaded World.
    void
    Calls the Actor.act(float) method on every Actor in the currently-loaded World.
    void
    Calls the Actor.draw() method on every Actor, drawing all objects in the currently-loaded World to screen.
    void
    Handles all the frame-by-frame input.
    void
    handleKeyDown​(char key, int keyCode)
    Handles the event of a key being pressed.
    void
    handleKeyUp​(char key, int keyCode)
    Handles the event of a key being released.
    void
    handleMouseDown​(int mouseButton)
    Handles the event of a mouse button being pressed.
    void
    handleMousePosition​(int mouseX, int mouseY)
    Deprecated.
    void
    handleMousePosition​(int previousMouseX, int previousMouseY, int mouseX, int mouseY)
    Handles the mouse position input.
    void
    handleMouseUp​(int mouseButton)
    Handles the event of a mouse button being released.
    void
    handleMouseWheel​(int mouseScroll)
    Handles when the mouse is scrolled.
    boolean
    isKeyDown​(char key)
    Retrieves whether a specific key is currently down.
    boolean
    isKeyDown​(int keyCode)
    Retrieves whether a specific key is currently down.
    boolean
    Retrieves whether a specific key is currently down.
    boolean
    isKeyDownThisFrame​(char key)
    Retrieves whether a specific key was pressed this frame.
    boolean
    isKeyDownThisFrame​(int keyCode)
    Retrieves whether a specific key was pressed this frame.
    boolean
    Retrieves whether a specific key was pressed this frame.
    boolean
    isKeyUpThisFrame​(char key)
    Retrieves whether a specific key was released this frame.
    boolean
    isKeyUpThisFrame​(int keyCode)
    Retrieves whether a specific key was released this frame.
    boolean
    Retrieves whether a specific key was released this frame.
    boolean
    isMouseButtonDown​(int mouseButton)
    Retrieves whether a specific mouse button is currently down.
    boolean
    isMouseButtonDownThisFrame​(int mouseButton)
    Retrieves whether a specific mouse button was pressed this frame.
    boolean
    isMouseButtonUpThisFrame​(int mouseButton)
    Retrieves whether a specific mouse button was released this frame.
    boolean
    Retrieves whether the mouse has moved this frame.
    boolean
    Retrieves whether the mouse is currently scrolling.
    void
    loadWorld​(World world)
    Loads a World to make it the one currently in use.
    static boolean
    pointInRectangle​(float pXx, float pXy, float pYx, float pYy, float pZx, float pZy, float pWx, float pWy, float pPx, float pPy)
    Checks whether a point (pPx, pPy) is within the bounds of a rectangle ((pXx, pXy), (pYx, pYy), (pZx, pZy), (pWx, pWy)) where the points are provided in clockwise order.
    resizeNN​(PImage src, int w, int h)
    A nearest-neighbour resizing implementation.
    static float
    sec​(float angle)
    Calculates the secant of angle in radians.
    tileImage​(PImage src, int w, int h)
    Tiles a source image to fill a new image with different dimensions.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • Green

      public Green(PApplet theParent)
      Starts the backbone of the library.
      Parameters:
      theParent - The PApplet running the library.
      Throws:
      SingleInstanceException - Thrown when this constructor is called more than once.
  • Method Details

    • getInstance

      public static Green getInstance()
      Retrieves the current instance of the library that is running.
      Returns:
      The current instance of the library that is running.
    • getWorld

      public static World getWorld()
      Retrieves the currently-loaded World.
      Returns:
      The currently-loaded World.
    • getParent

      public PApplet getParent()
      Retrieves the parent instance of Processing.
      Returns:
      The parent instance of Processing.
    • getDeltaTime

      public float getDeltaTime()
      Retrieves the deltaTime - the time since the last frame.
      Returns:
      The time, in seconds, since the last frame.
    • getPointsDist

      public static float getPointsDist(float x1, float y1, float x2, float y2)
      Calculates the distance between two points ((x1, y1) and (x2, y2)).
      Parameters:
      x1 - The X-axis position of point 1.
      y1 - The Y-axis position of point 1.
      x2 - The X-axis position of point 2.
      y2 - The Y-axis position of point 2.
      Returns:
      The distance between the two points.
    • getPointsAngle

      public static float getPointsAngle(float x1, float y1, float x2, float y2)
      Calculates the angle between two points ((x1, y1) and (x2, y2)).
      Parameters:
      x1 - The X-axis position of point 1.
      y1 - The Y-axis position of point 1.
      x2 - The X-axis position of point 2.
      y2 - The Y-axis position of point 2.
      Returns:
      The angle between the two points.
    • getLinesIntersect

      public static boolean getLinesIntersect(float a1x, float a1y, float a2x, float a2y, float b1x, float b1y, float b2x, float b2y)
      Determines whether two line segments ([(a1x, a1y) - (a2x, a2y)] and [(b1x, b1y) - (b2x, b2y)]) intersect.
      Parameters:
      a1x - The X-axis position of point 1 of line segment A.
      a1y - The Y-axis position of point 1 of line segment A.
      a2x - The X-axis position of point 2 of line segment A.
      a2y - The Y-axis position of point 2 of line segment A.
      b1x - The X-axis position of point 1 of line segment B.
      b1y - The Y-axis position of point 1 of line segment B.
      b2x - The X-axis position of point 2 of line segment B.
      b2y - The Y-axis position of point 2 of line segment B.
      Returns:
      Whether the two line segments intersect.
    • csc

      public static float csc(float angle)
      Calculates the cosecant of angle in radians.
      Parameters:
      angle - The angle in radians to calculate the cosecant of.
      Returns:
      The cosecant of angle.
    • sec

      public static float sec(float angle)
      Calculates the secant of angle in radians.
      Parameters:
      angle - The angle in radians to calculate the secant of.
      Returns:
      The secant of angle.
    • cot

      public static float cot(float angle)
      Calculates the cotangent of angle in radians.
      Parameters:
      angle - The angle in radians to calculate the cotangent of.
      Returns:
      The cotangent of angle.
    • getDigits

      public static int getDigits(int value)
      Calculates the number of digits in a given integer. If value is 0, 1 is returned. If value is negative, the negative sign is ignored.
      Parameters:
      value - The integer to measure the number of digits of.
      Returns:
      The number of digits in value.
    • resizeNN

      public PImage resizeNN(PImage src, int w, int h)
      A nearest-neighbour resizing implementation. This is great for resizing pixel art while keeping it looking crisp and sharp.
      Parameters:
      src - The original image to resize.
      w - The width to resize to.
      h - The height to resize to.
      Returns:
      The resized image.
      See Also:
      https://gist.github.com/gncgnc/fc645b8db00ec43d43fecef37d58df73, https://gist.github.com/GoToLoop/2e12acf577506fd53267e1d186624d7c
    • tileImage

      public PImage tileImage(PImage src, int w, int h)
      Tiles a source image to fill a new image with different dimensions. Primarily used for background images.
      Parameters:
      src - The original image to tile.
      w - The width to tile to.
      h - The height to tile to.
      Returns:
      The new image, composed of src tiled to fit.
    • pointInRectangle

      public static boolean pointInRectangle(float pXx, float pXy, float pYx, float pYy, float pZx, float pZy, float pWx, float pWy, float pPx, float pPy)
      Checks whether a point (pPx, pPy) is within the bounds of a rectangle ((pXx, pXy), (pYx, pYy), (pZx, pZy), (pWx, pWy)) where the points are provided in clockwise order.
      Parameters:
      pXx - The X-axis position of the X point.
      pXy - The Y-axis position of the X point.
      pYx - The X-axis position of the Y point.
      pYy - The Y-axis position of the Y point.
      pZx - The X-axis position of the Z point.
      pZy - The Y-axis position of the Z point.
      pWx - The X-axis position of the W point.
      pWy - The Y-axis position of the W point.
      pPx - The X-axis position of the point to check.
      pPy - The Y-axis position of the point to check.
      Returns:
      Whether the point to check is within the bounds of the provided rectangle.
      See Also:
      https://gamedev.stackexchange.com/a/110233
    • loadWorld

      public void loadWorld(World world)
      Loads a World to make it the one currently in use. If there is a world already loaded, the new one will only be loaded at the end of the current frame.
      Parameters:
      world - The World to load.
    • handleDraw

      public void handleDraw() throws NoWorldException
      Calls the Actor.draw() method on every Actor, drawing all objects in the currently-loaded World to screen.
      Throws:
      NoWorldException - Thrown when the method is called and there is no World loaded.
    • handleAct

      public void handleAct() throws NoWorldException
      Calls the Actor.act(float) method on every Actor in the currently-loaded World.
      Throws:
      NoWorldException - Thrown when the method is called and there is no World loaded.
    • handleInput

      public void handleInput()
      Handles all the frame-by-frame input.
    • handleMouseDown

      public void handleMouseDown(int mouseButton)
      Handles the event of a mouse button being pressed. Put this in the PApplet.mousePressed() event.
      Parameters:
      mouseButton - The mouse button that was pressed - either PConstants.LEFT, PConstants.CENTER, or PConstants.RIGHT. Please pass in PApplet.mouseButton in your code.
    • handleMouseUp

      public void handleMouseUp(int mouseButton)
      Handles the event of a mouse button being released. Put this in the PApplet.mouseReleased() event.
      Parameters:
      mouseButton - The mouse button that was released - either PConstants.LEFT, PConstants.CENTER, or PConstants.RIGHT. Please pass in PApplet.mouseButton in your code.
    • handleMousePosition

      public void handleMousePosition(int previousMouseX, int previousMouseY, int mouseX, int mouseY)
      Handles the mouse position input. Just put this in your PApplet.draw() method.
      Parameters:
      previousMouseX - The mouse's previous X-axis position, in pixels. Please pass in PApplet.pmouseX in your code.
      previousMouseY - The mouse's previous Y-axis position, in pixels. Please pass in PApplet.pmouseY in your code.
      mouseX - The mouse's X-axis position, in pixels. Please pass in PApplet.mouseX in your code.
      mouseY - The mouse's Y-axis position, in pixels. Please pass in PApplet.mouseY in your code.
    • handleMousePosition

      @Deprecated public void handleMousePosition(int mouseX, int mouseY)
      Deprecated.
      Handles the mouse position input. Just put this in your PApplet.draw() method.
      Parameters:
      mouseX - The mouse's X-axis position, in pixels. Please pass in PApplet.mouseX in your code.
      mouseY - The mouse's Y-axis position, in pixels. Please pass in PApplet.mouseY in your code.
    • handleMouseWheel

      public void handleMouseWheel(int mouseScroll)
      Handles when the mouse is scrolled. Put this in the PApplet.mouseWheel(processing.event.MouseEvent) event.
      Parameters:
      mouseScroll - The amount that the mouse has scrolled this frame. Please pass in MouseEvent.getCount().
    • isMouseMoving

      public boolean isMouseMoving()
      Retrieves whether the mouse has moved this frame.
      Returns:
      Whether the mouse has moved this frame.
    • getMouseSpeed

      public float getMouseSpeed()
      Calculates the mouse's speed from last frame to this frame. This may not be very reliable, since it is calculated from frame-to-frame.
      Returns:
      The mouse's speed from last frame to this frame, in pixels/second.
    • isMouseButtonDown

      public boolean isMouseButtonDown(int mouseButton)
      Retrieves whether a specific mouse button is currently down.
      Parameters:
      mouseButton - The mouse button to check for - either PConstants.LEFT, PConstants.CENTER, or PConstants.RIGHT.
      Returns:
      Whether mouseButton is down.
    • isMouseButtonDownThisFrame

      public boolean isMouseButtonDownThisFrame(int mouseButton)
      Retrieves whether a specific mouse button was pressed this frame.
      Parameters:
      mouseButton - The mouse button to check for - either PConstants.LEFT, PConstants.CENTER, or PConstants.RIGHT.
      Returns:
      Whether mouseButton was pressed in this frame.
    • isMouseButtonUpThisFrame

      public boolean isMouseButtonUpThisFrame(int mouseButton)
      Retrieves whether a specific mouse button was released this frame.
      Parameters:
      mouseButton - The mouse button to check for - either PConstants.LEFT, PConstants.CENTER, or PConstants.RIGHT.
      Returns:
      Whether mouseButton was released in this frame.
    • getMouseScroll

      public int getMouseScroll()
      Retrieves the amount that the mouse scrolled in this frame.
      Returns:
      The amount that the mouse scrolled in this frame.
    • isMouseScrolling

      public boolean isMouseScrolling()
      Retrieves whether the mouse is currently scrolling.
      Returns:
      Whether the mouse is currently scrolling.
    • handleKeyDown

      public void handleKeyDown(char key, int keyCode)
      Handles the event of a key being pressed. Put this in the PApplet.keyPressed() event.
      Parameters:
      key - The char representation of the key that was pressed. Please pass in PApplet.key in your code.
      keyCode - The key code of the key that was pressed. Please pass in PApplet.keyCode in your code.
      See Also:
      Java KeyEvent reference
    • handleKeyUp

      public void handleKeyUp(char key, int keyCode)
      Handles the event of a key being released. Put this in the PApplet.keyReleased() event.
      Parameters:
      key - The char representation of the key that was released. Please pass in PApplet.key in your code.
      keyCode - The key code of the key that was released. Please pass in PApplet.keyCode in your code.
      See Also:
      Java KeyEvent reference
    • isKeyDown

      public boolean isKeyDown(char key)
      Retrieves whether a specific key is currently down.
      Parameters:
      key - The char representation of the key to check for.
      Returns:
      Whether key is currently down.
    • isKeyDown

      public boolean isKeyDown(int keyCode)
      Retrieves whether a specific key is currently down.
      Parameters:
      keyCode - The key code of the key to check for.
      Returns:
      Whether key is currently down.
      See Also:
      Java KeyEvent reference
    • isKeyDown

      public boolean isKeyDown(InputKey key)
      Retrieves whether a specific key is currently down.
      Parameters:
      key - The key to check for.
      Returns:
      Whether key is currently down.
    • isKeyDownThisFrame

      public boolean isKeyDownThisFrame(char key)
      Retrieves whether a specific key was pressed this frame.
      Parameters:
      key - The char representation of the key to check for.
      Returns:
      Whether key was pressed in this frame.
    • isKeyDownThisFrame

      public boolean isKeyDownThisFrame(int keyCode)
      Retrieves whether a specific key was pressed this frame.
      Parameters:
      keyCode - The key code of the key to check for.
      Returns:
      Whether key was pressed in this frame.
      See Also:
      Java KeyEvent reference
    • isKeyDownThisFrame

      public boolean isKeyDownThisFrame(InputKey key)
      Retrieves whether a specific key was pressed this frame.
      Parameters:
      key - The key to check for.
      Returns:
      Whether key was pressed in this frame.
    • isKeyUpThisFrame

      public boolean isKeyUpThisFrame(char key)
      Retrieves whether a specific key was released this frame.
      Parameters:
      key - The char representation of the key to check for.
      Returns:
      Whether key was released in this frame.
    • isKeyUpThisFrame

      public boolean isKeyUpThisFrame(int keyCode)
      Retrieves whether a specific key was released this frame.
      Parameters:
      keyCode - The key code of the key to check for.
      Returns:
      Whether key was released in this frame.
      See Also:
      Java KeyEvent reference
    • isKeyUpThisFrame

      public boolean isKeyUpThisFrame(InputKey key)
      Retrieves whether a specific key was released this frame.
      Parameters:
      key - The key to check for.
      Returns:
      Whether key was released in this frame.