Package jltk

Class Pen

  • Direct Known Subclasses:
    ColoredPen

    public class Pen
    extends Object
    A pen belongs to a window object. Because of this, you first have to create a window.
    A pen has a exact defined position on the window, which is defined by an x- and y-coordinate. The origin of the coordinate system is the upper left corner of the window. The x-axis points to the right, the y-axis points down. The default position is the upper left corner. The unit is pixels.
    A pen also has a direction. 0° means east. The pen can be turn clockwise.
    The pen can be lift up or lowered down. Is the pen down and moving across the window the pen draws a line between each location. The line depends on its mode (see below).
    If the pen is lift up, no line is drawn.

    • DrawMode: The pen draws a black line.
    • EraseMode: The pen draws a line in the background color. So the pen can "erase" its former drawing.

    You also can use a colored pen with some more options, see class ColoredPen for more details.

    Version:
    01.06.2022
    Author:
    ms
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int currentMode  
      protected static int DRAWINGMODE  
      protected static int ERASEMODE  
      protected javafx.scene.text.Font font  
      (package private) javafx.scene.canvas.GraphicsContext g  
      protected boolean isUp  
      protected javafx.scene.paint.Paint lineColor  
      protected double lineWidth  
      protected Window myWindow  
      protected double rotation  
      protected double x  
      protected double y  
    • Constructor Summary

      Constructors 
      Constructor Description
      Pen()
      Creates a new pen on the last created.
      Pen​(Window pWindow)
      Creates a new pen on the specified window.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void delay​(int pMilliseconds)
      Delays the execution by the specified amount of milliseconds.
      void down()
      Lowers the pen down ("puts it on the paper").
      void drawCircle​(double pRadius)
      The pen draws a circle with the specified radius.
      void drawRect​(double pWidth, double pHeight)
      The pen draws a rectangle with the specified width and height.
      void enableDrawMode()
      Puts the pen into drawing mode.
      void enableEraseMode()
      Puts the pen into erase mode.
      String getFont()
      Return the name of the font, the pen currently use.
      double getFontSize()
      Return the size of the font the pen currently use.
      double getRotation()
      Returns the current rotation of this pen.
      double getX()
      Returns the x-coordinate of the pen's current location.
      double getY()
      Returns the y-coordinate of the pen's current location.
      boolean isEraseModeEnabled()
      Return true, if the pen is in erase mode, return false if the pen is in draw mode
      void move​(double pDistance)
      Moves this pen by the specified distance.
      protected void setDefault()
      Sets the standard values for this pen.
      void setFont​(String pFontName)
      Set the font, the pen should use.
      void setFontSize​(double pSize)
      Set the font size to the specified value.
      void setFontStyle​(boolean pBold, boolean pItalic)
      Set the font style of the pen.
      void setPosition​(double pX, double pY)
      Moves this pen to the position (x1/y1).
      void setRotation​(double pAngle)
      Set the rotation of this pen.
      double textHeight​(String pText)
      Return the height of the giving text using the font settings of this pen
      double textWidth​(String pText)
      Return the width of the giving text using the font settings of this pen
      void turn​(double pAngle)
      Turn this pen by the specified amount (in degrees).
      void turnTowards​(double pX, double pY)
      Turn this pen to face towards the specified location.
      void up()
      Lifts the pen up.
      void write​(char pCharacter)
      The pen writes the specified character.
      void write​(double pNumber)
      The pen writes the specified double value.
      void write​(float pNumber)
      The pen writes the specified float value.
      void write​(int pNumber)
      The pen writes the specified integer.
      void write​(long pNumber)
      The pen writes the specified long integer.
      void write​(String pText)
      The pen writes the specified text.
    • Field Detail

      • x

        protected double x
      • y

        protected double y
      • isUp

        protected boolean isUp
      • rotation

        protected double rotation
      • myWindow

        protected Window myWindow
      • currentMode

        protected int currentMode
      • lineColor

        protected javafx.scene.paint.Paint lineColor
      • lineWidth

        protected double lineWidth
      • font

        protected javafx.scene.text.Font font
      • g

        javafx.scene.canvas.GraphicsContext g
    • Constructor Detail

      • Pen

        public Pen()
        Creates a new pen on the last created. Because of this, you first have to create a window and the a pen.
      • Pen

        public Pen​(Window pWindow)
        Creates a new pen on the specified window.
        Parameters:
        pWindow - The window for this pen.
    • Method Detail

      • delay

        public void delay​(int pMilliseconds)
        Delays the execution by the specified amount of milliseconds.
        Parameters:
        pMilliseconds - The duration of delay in milliseconds
      • textWidth

        public double textWidth​(String pText)
        Return the width of the giving text using the font settings of this pen
        Parameters:
        pText - The text of which to calculate the width
        Returns:
        the width of the text
      • textHeight

        public double textHeight​(String pText)
        Return the height of the giving text using the font settings of this pen
        Parameters:
        pText - The text of which to calculate the height
        Returns:
        the height of the text
      • setPosition

        public void setPosition​(double pX,
                                double pY)
        Moves this pen to the position (x1/y1). If the pen is down, a line is drawn.
        Parameters:
        pX - The x-coordinate
        pY - The y-coordinate
      • move

        public void move​(double pDistance)
        Moves this pen by the specified distance. If the pen is down, a line is drawn.
        Parameters:
        pDistance - The distance in pixel.
      • setRotation

        public void setRotation​(double pAngle)
        Set the rotation of this pen. Rotation is expressed as a degree value. Zero degrees is to the east (right-hand side of the window), and the angle increases clockwise.
        Parameters:
        pRotation - The angle in degrees
        See Also:
        turn(double)
      • turn

        public void turn​(double pAngle)
        Turn this pen by the specified amount (in degrees).
        Parameters:
        pAmount - The angle in degrees; positive values turn clockwise
      • turnTowards

        public void turnTowards​(double pX,
                                double pY)
        Turn this pen to face towards the specified location.
        Parameters:
        pX - The x-coordinate
        pY - The y-coordinate
      • drawRect

        public void drawRect​(double pWidth,
                             double pHeight)
        The pen draws a rectangle with the specified width and height. The position of the pen is the upper left corner. The rectangle is also drawn when then pen is up.
        Parameters:
        pWidth - The width of the rectangle
        pHeight - The height of the rectangle
      • drawCircle

        public void drawCircle​(double pRadius)
        The pen draws a circle with the specified radius. The position of the pen is the center of the circle. The circle is also drawn when then pen is up.
        Parameters:
        pRadius - The radius of the circle
      • write

        public void write​(String pText)
        The pen writes the specified text. The lower left corner of the text is the current pen position. After writing the pen position is the lower right corner of the text.
        Parameters:
        pText - The text
      • write

        public void write​(double pNumber)
        The pen writes the specified double value. The lower left corner of the double value is the current pen position. After writing the pen position is the lower right corner of the double value.
        Parameters:
        pText - The double value
      • write

        public void write​(int pNumber)
        The pen writes the specified integer. The lower left corner of the integer is the current pen position. After writing the pen position is the lower right corner of the integer.
        Parameters:
        pText - The integer
      • write

        public void write​(long pNumber)
        The pen writes the specified long integer. The lower left corner of the long integer is the current pen position. After writing the pen position is the lower right corner of the long integer.
        Parameters:
        pText - The long integer
      • write

        public void write​(float pNumber)
        The pen writes the specified float value. The lower left corner of the float value is the current pen position. After writing the pen position is the lower right corner of the float value.
        Parameters:
        pText - The float value
      • write

        public void write​(char pCharacter)
        The pen writes the specified character. The lower left corner of the character is the current pen position. After writing the pen position is the lower right corner of the character.
        Parameters:
        pText - The float value
      • up

        public void up()
        Lifts the pen up. Moving the pen now has no visible effect, no line will be drawn.
        See Also:
        move(double), #setPostion(double, double)
      • down

        public void down()
        Lowers the pen down ("puts it on the paper"). Now, the pen draws a line while moving.
        See Also:
        move(double), #setPostion(double, double)
      • enableDrawMode

        public void enableDrawMode()
        Puts the pen into drawing mode. In this mode, the pen draws a line while moving (if pen is down).
        See Also:
        down(), up(), enableEraseMode()
      • enableEraseMode

        public void enableEraseMode()
        Puts the pen into erase mode. In this mode, the pen erase everything while moving (if pen is down).
        See Also:
        down(), up(), #enableDrawing()
      • isEraseModeEnabled

        public boolean isEraseModeEnabled()
        Return true, if the pen is in erase mode, return false if the pen is in draw mode
        Returns:
        true if erase mode is enabled, else false
      • getX

        public double getX()
        Returns the x-coordinate of the pen's current location.
        Returns:
        Returns the x-coordinate of the pen's current location.
      • getY

        public double getY()
        Returns the y-coordinate of the pen's current location.
        Returns:
        Returns the y-coordinate of the pen's current location.
      • getRotation

        public double getRotation()
        Returns the current rotation of this pen. Rotation is expressed as a degree value, range (0..359). Zero degrees is towards the east, and the angle increases clockwise.
        Returns:
        The rotation in degrees.
      • setFont

        public void setFont​(String pFontName)
        Set the font, the pen should use. You can use every Font installed on your system. E.g. "Calibri", "Arial", "Times New Roman", "Deja Vu Sans". You can print all available fonts of your system, if you run jltk.util.Misc.printAvailableFontNames().
        Parameters:
        pFontName - Ein Parameter
        See Also:
        getFont(), Misc.printAvailableFontNames()
      • setFontSize

        public void setFontSize​(double pSize)
        Set the font size to the specified value.
        Parameters:
        pSize - The font size
      • getFontSize

        public double getFontSize()
        Return the size of the font the pen currently use.
        Returns:
        The font size
        See Also:
        setFontSize(double)
      • setFontStyle

        public void setFontStyle​(boolean pBold,
                                 boolean pItalic)
        Set the font style of the pen.
        Parameters:
        pBold - true if bold, else false
        pItalic - true if italic, else false
        See Also:
        write(String)
      • setDefault

        protected void setDefault()
        Sets the standard values for this pen. (for internal use)