Class Pen
java.lang.Object
org.openpatch.scratch.extensions.pen.Pen
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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addedToStage
(Stage stage) 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 valuevoid
changeSize
(double size) Change the size of the penvoid
changeTransparency
(double step) Change the transparency by a stepvoid
down()
Set the pen down.void
draw()
Draw the line which the pen has drawn.void
eraseAll()
Erases all.double
getColor()
Retrieves the current color of the pen in HSB (Hue, Saturation, Brightness) format.double
getSize()
Returns the size of the penvoid
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
removedFromStage
(Stage stage) This method is called when the pen is removed from the stage.void
setColor
(double h) Set color via hue valuevoid
setColor
(double r, double g, double b) Set color via rgb valuesvoid
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 penvoid
setTransparency
(double transparency) Set the transparencyvoid
stamp()
Stamps the current sprite onto the canvas.void
up()
Move the pen up.
-
Constructor Details
-
Pen
public Pen()Constructs a new Pen object. -
Pen
Constructs a new Pen object associated with the given Sprite.- Parameters:
s
- the Sprite object to associate with this Pen
-
Pen
Copies a Pen object.- Parameters:
p
- Pen object to copy
-
-
Method Details
-
addedToStage
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
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 coordinatey
- y coordinate
-
setPosition
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.
-