java.lang.Object
org.openpatch.scratch.extensions.pen.Pen

public class Pen extends Object
The Pen class represents a drawing tool that can be used to draw paths on a stage. It supports various functionalities such as setting color, size, transparency, moving to different positions, and stamping the current sprite onto the canvas. The Pen can be associated with a Sprite and can draw on both the foreground and background.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Pen()
    Constructs a new Pen object.
    Pen(Pen p)
    Copies a Pen object.
    Constructs a new Pen object associated with the given Sprite.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    This method is called when the pen is added to the stage.
    void
    changeColor(double c)
    Change color via a hue value, which is added to the current hue value
    void
    changeSize(double size)
    Change the size of the pen
    void
    changeTransparency(double step)
    Change the transparency by a step
    void
    Set the pen down.
    void
    Draw the line which the pen has drawn.
    void
    Erases all.
    double
    Retrieves the current color of the pen in HSB (Hue, Saturation, Brightness) format.
    double
    Returns the size of the pen
    void
    Sets the pen to the background.
    void
    Moves the pen to the foreground.
    void
    Moves the object to the current position of the mouse pointer.
    void
    Moves the object to a random position within the boundaries of the stage.
    boolean
    Returns true if the pen is in the background.
    void
    This method is called when the pen is removed from the stage.
    void
    setColor(double h)
    Set color via hue value
    void
    setColor(double r, double g, double b)
    Set color via rgb values
    void
    setPosition(double x, double y)
    Set the position if the pen is down.
    void
    Sets the position of the pen using a Vector2 object.
    void
    setSize(double size)
    Set the size of the pen
    void
    setTransparency(double transparency)
    Set the transparency
    void
    Stamps the current sprite onto the canvas.
    void
    up()
    Move the pen up.

    Methods inherited from class java.lang.Object

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

    • Pen

      public Pen()
      Constructs a new Pen object.
    • Pen

      public Pen(Sprite s)
      Constructs a new Pen object associated with the given Sprite.
      Parameters:
      s - the Sprite object to associate with this Pen
    • Pen

      public Pen(Pen p)
      Copies a Pen object.
      Parameters:
      p - Pen object to copy
  • Method Details

    • addedToStage

      public void addedToStage(Stage stage)
      This method is called when the pen is added to the stage. It assigns the given stage to the pen's stage variable.
      Parameters:
      stage - The stage to which the pen is added.
    • removedFromStage

      public void removedFromStage(Stage stage)
      This method is called when the pen is removed from the stage. It sets the stage reference to null.
      Parameters:
      stage - The stage from which the pen is removed.
    • getColor

      public double getColor()
      Retrieves the current color of the pen in HSB (Hue, Saturation, Brightness) format.
      Returns:
      the color of the pen as a double representing the HSB value.
    • setColor

      public void setColor(double h)
      Set color via hue value
      Parameters:
      h - a hue value [0...255]
    • setColor

      public void setColor(double r, double g, double b)
      Set color via rgb values
      Parameters:
      r - a red value [0...255]
      g - a green value [0...255]
      b - a blue value [0...255]
    • changeColor

      public void changeColor(double c)
      Change color via a hue value, which is added to the current hue value
      Parameters:
      c - a hue value [0...255]
    • goToBackground

      public void goToBackground()
      Sets the pen to the background. This method changes the state of the pen to indicate that it is no longer in the foreground.
    • goToForeground

      public void goToForeground()
      Moves the pen to the foreground. This method sets the pen's state to indicate that it is in the foreground.
    • isInBackground

      public boolean isInBackground()
      Returns true if the pen is in the background.
      Returns:
      true if the pen is in the background
    • setSize

      public void setSize(double size)
      Set the size of the pen
      Parameters:
      size - size of the pen
    • getSize

      public double getSize()
      Returns the size of the pen
      Returns:
      the size of the pen
    • changeSize

      public void changeSize(double size)
      Change the size of the pen
      Parameters:
      size - size to change
    • setTransparency

      public void setTransparency(double transparency)
      Set the transparency
      Parameters:
      transparency - transparency of the pen
    • changeTransparency

      public void changeTransparency(double step)
      Change the transparency by a step
      Parameters:
      step - the step to change the transparency by
    • setPosition

      public void setPosition(double x, double y)
      Set the position if the pen is down.
      Parameters:
      x - x coordinate
      y - y coordinate
    • setPosition

      public void setPosition(Vector2 v)
      Sets the position of the pen using a Vector2 object.
      Parameters:
      v - the Vector2 object containing the new x and y coordinates
    • goToRandomPosition

      public void goToRandomPosition()
      Moves the object to a random position within the boundaries of the stage. The new position is determined by generating random coordinates within the width and height of the stage.
    • goToMousePointer

      public void goToMousePointer()
      Moves the object to the current position of the mouse pointer.
    • down

      public void down()
      Set the pen down.
    • up

      public void up()
      Move the pen up.
    • stamp

      public void stamp()
      Stamps the current sprite onto the canvas. If the sprite is not null, it will stamp either to the foreground or the background based on the value of the isForeground flag.
    • eraseAll

      public void eraseAll()
      Erases all.
    • draw

      public void draw()
      Draw the line which the pen has drawn.