Package nl.saxion.app

Class SaxionApp

java.lang.Object
nl.saxion.app.SaxionApp

public class SaxionApp extends Object
The SaxionApp is designed to aid in introducing students to programming at Saxion.
The SaxionApp contains static methods to show a window containing text and graphics.
It contains the following features:
  • Drawing basic shapes: point, line, rectangle, oval and circle
  • Drawing text: normal text and text with a border
  • Drawing images: import an image and draw it on the canvas (this is very slow!!! Be patient :-) )
  • Pen and FilledPen for drawing lines and polygons
  • Pausing and clearing the board
  • Exporting to png images
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Color
    The default background color
    static final Color
    The official Saxion green color.
    static final Color
    The official Saxion pink color.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    add(CanvasElement element)
    Add a canvas element, and make sure the canvas redraws.
    static void
    Clears the current drawing and console messages.
    static Color
    createColor(int red, int green, int blue)
    Create a new color using the RGB scale
    static void
    drawBorderedText(String text, int x, int y, int fontSize)
    Draw a piece of text on the canvas with a border.
    static void
    drawCircle(int centerX, int centerY, int radius)
    Draw a circle on the canvas, from the center point of the circle.
    static void
    drawImage(String filename, int x, int y)
    Draw an image file (png or jpg) on the canvas.
    static void
    drawImage(String filename, int x, int y, int width, int height)
    Draw an image file (png or jpg) on the canvas.
    static void
    drawLine(int x1, int y1, int x2, int y2)
    Draw a line.
    static void
    drawOval(int x, int y, int width, int height)
    Draw an oval on the screen.
    static void
    drawPoint(int x, int y)
    Draw a single point on the canvas, using 4 as width and height.
    static void
    drawPoint(int x, int y, int size)
    Draw a single point on the canvas.
    static void
    Experimental method, used for the filled pen to add a shape to the SaxionApp.
    static void
    drawRectangle(int x, int y, int width, int height)
    Draw a rectangle on the screen.
    static void
    drawText(String text, int x, int y, int fontSize)
    Draw a piece of text on the canvas.
    static FilledPen
    Get an instance of the FilledPen.
    static int
    Get the height of the canvas in pixels
    static Pen
    Get an instance of the Pen.
    static Color
    Creates a random color.
    static int
    getRandomValueBetween(int lowerBound, int upperBound)
    Generates a random integer value between lowerBound (inclusive) and upperBound (NOT inclusive).
    static int
    Get the width of the canvas in pixels
    static void
    Pause the drawing at this point.
    static void
    playSound(String filename)
    Plays a sound one time.
    static void
    playSound(String filename, boolean loop)
    Plays a sound.
    static void
    print(String text)
    Prints the text in the default color and does not include a line break.
    static void
    print(String text, Color color)
    Prints the text in the color received and does not include a line break.
    static void
    print(String text, Color color, boolean newLine)
    Print text to the application console.
    static <T> void
    print(T value)
    Overloaded method to allow printing for various data types not just String, the string value of the value will be printed, with default color and with no line break.
    static <T> void
    print(T value, Color color)
    Overloaded method to allow printing for various data types not just String, the string value of the value will be printed, with color received as a parameter and with no line break.
    static void
    Adds a line break to the current position.
    static void
    Prints the text in the default color and includes a line break.
    static void
    printLine(String text, Color color)
    Prints the text in the color received and includes a line break.
    static <T> void
    printLine(T value)
    Overloaded method to allow printing for various data types not just String, the string value of the value will be printed, with default color and with a line break.
    static <T> void
    printLine(T value, Color color)
    Overloaded method to allow printing for various data types not just String, the string value of the value will be printed, with color received as a parameter and with a line break.
    static void
    Stops the application completely.
    static Character
    Waits for a character to be typed and returns which character it was.
    static Character
    readChar(double timeoutSeconds)
    Waits for up to `timeoutSeconds` for a character to be typed and returns which character it was.
    static double
    Allows the user to enter a double value (like 3,1415), concluded with an enter.
    static double
    readDouble(String alternativeErrorMessage)
    Allows the user to enter a double value (like 3,1415), concluded with an enter.
    static int
    Allows the user to type an integer (whole) number, concluded with an enter.
    static int
    readInt(String alternativeErrorMessage)
    Allows the user to type an integer (whole) number, concluded with an enter.
    static String
    Allow the user to input a line of text, concluded with an enter.
    static String
    Allow the user to input a line of text, concluded with an enter.
    static void
    Experimental: Reset all Canvas Transformations Uses AffineTransformations in Java.
    static void
    resize(int width, int height)
    Change the size of the canvas while the application is running.
    static void
    saveImage(String filename)
    Saves the image as a PNG file.
    static void
    Change the background color on top of which your content is shown.
    static void
    Set the color of the border.
    static void
    setBorderSize(int size)
    Set the border size of the next object that you are going to draw.
    static void
    setFill(Color color)
    Set the fill color of the objects that you are going to draw.
    static void
    When in game loop mode, change the duration of the gameloop (how long it takes between executing the loop method).
    static void
    Set the color of the text.
    static void
    showMessage(String text, Color color)
    Shows an overlay bar on the bottom of the screen containing a message.
    static void
    sleep(double seconds)
    Pause program for the given number of seconds.
    static void
    start(Runnable runnable)
    Open a SaxionApp window with default size and run your code in it.
    static void
    start(Runnable runnable, int width, int height)
    Open a SaxionApp window with the given size and run your code in it.
    static void
    startGameLoop(GameLoop gameLoop, int width, int height, int ms)
    Open the SaxionApp in gameloop mode.
    static void
     
    static void
    Stop all sounds that are currently playing.
    static void
     
    static void
    stopSound(String filename)
    Stop a certain sound that is currently playing.
    static void
    transformRotate(double degrees)
    Experimental: rotate around the center point of the canvas before drawing a shape.
    static void
    transformRotate(double degrees, int x, int y)
    Experimental: rotate around the specified point on the canvas before drawing a shape.
    static void
    transformScale(double scaleFactorX, double scaleFactorY)
    Experimental: scale the canvas before drawing a shape.
    static void
    transformShear(double shearX, double shearY)
    Experimental: shear transformation on the the canvas before drawing a shape.
    static void
    transformTranslate(double translateX, double translateY)
    Experimental: translate the canvas before drawing a shape.
    static void
    Turns the border off
    static void
    Turn border on
    static void
    Turn fill off
    static void
    Turn fill on

    Methods inherited from class java.lang.Object

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

    • SAXION_GREEN

      public static final Color SAXION_GREEN
      The official Saxion green color.
    • SAXION_PINK

      public static final Color SAXION_PINK
      The official Saxion pink color.
    • DEFAULT_BACKGROUND_COLOR

      public static final Color DEFAULT_BACKGROUND_COLOR
      The default background color
  • Method Details

    • start

      public static void start(Runnable runnable)
      Open a SaxionApp window with default size and run your code in it.
      Parameters:
      runnable - A Runnable object, having a run() method containing your code.
    • start

      public static void start(Runnable runnable, int width, int height)
      Open a SaxionApp window with the given size and run your code in it.
      Parameters:
      runnable - A Runnable object, having a run() method containing your code.
      width - Window width in pixels
      height - Window height in pixels
    • startGameLoop

      public static void startGameLoop(GameLoop gameLoop, int width, int height, int ms)
      Open the SaxionApp in gameloop mode. The loop function will be executed every `ms` ms. This mode is used for creating simple games. It lets you interact with the application via keyboard and mouse listeners.
      Parameters:
      gameLoop - Object that implements the GameLoop interface.
      width - Window width in pixels
      height - Window height in pixels
      ms - How long does the application pause before the gameloop is called again (in milliseconds)
    • setGameLoopTimeMs

      public static void setGameLoopTimeMs(int ms)
      When in game loop mode, change the duration of the gameloop (how long it takes between executing the loop method). Please note, this value is in milliseconds
      Parameters:
      ms - Milliseconds
    • resize

      public static void resize(int width, int height)
      Change the size of the canvas while the application is running. This creates a new window and destroy the old one. The canvas elements are kept.
      Parameters:
      width - The new width in pixels
      height - The new height in pixels
    • showMessage

      public static void showMessage(String text, Color color)
      Shows an overlay bar on the bottom of the screen containing a message. (Like an Android Toast or Snackbar). It waits for a key press before removing the overlay and returning.
      Parameters:
      text - The message
      color - The background color to draw the message on. The foreground color is always white.
    • add

      public static void add(CanvasElement element)
      Add a canvas element, and make sure the canvas redraws.
      Parameters:
      element - The element to add to the Canvas.
    • clear

      public static void clear()
      Clears the current drawing and console messages.
    • sleep

      public static void sleep(double seconds)
      Pause program for the given number of seconds.
      Parameters:
      seconds - The amount of seconds to sleep.
    • readChar

      public static Character readChar()
      Waits for a character to be typed and returns which character it was.
      Returns:
      A Character. E.g.: 'a', 'Z' or '\n' (enter).
    • readChar

      public static Character readChar(double timeoutSeconds)
      Waits for up to `timeoutSeconds` for a character to be typed and returns which character it was.
      Parameters:
      timeoutSeconds - The time in seconds to wait for a key to be pressed.
      Returns:
      Either a Character or `null` in case of a timeout.
    • readString

      public static String readString(Color color)
      Allow the user to input a line of text, concluded with an enter.
      Parameters:
      color - The color used to show user input or `null` if it is to be hidden.
      Returns:
      The typed string, excluding the enter character.
    • readString

      public static String readString()
      Allow the user to input a line of text, concluded with an enter.
      Returns:
      The typed string, excluding the enter character.
    • readInt

      public static int readInt()
      Allows the user to type an integer (whole) number, concluded with an enter. In case the user types something else, an error message is shown and the user is asked again.
      Returns:
      The integer provided by the user.
    • readInt

      public static int readInt(String alternativeErrorMessage)
      Allows the user to type an integer (whole) number, concluded with an enter. In case the user types something else, the alternative error message will be shown.
      Parameters:
      alternativeErrorMessage - The error message that is displayed when the input is incorrect.
      Returns:
      The integer provided by the user.
    • readDouble

      public static double readDouble()
      Allows the user to enter a double value (like 3,1415), concluded with an enter. In case the user types something else, an error message is shown and the user is asked again. Note that the decision whether or not you need to use a comma or a decimal point depends on your regional settings.
      Returns:
      The double provided by the user.
    • readDouble

      public static double readDouble(String alternativeErrorMessage)
      Allows the user to enter a double value (like 3,1415), concluded with an enter. In case the user types something else, an alternative error message is shown and the user is asked again. Note that the decision whether or not you need to use a comma or a decimal point depends on your regional settings.
      Parameters:
      alternativeErrorMessage - The error message that is displayed when the input is incorrect.
      Returns:
      The double provided by the user.
    • pause

      public static void pause()
      Pause the drawing at this point.
    • stopLoop

      public static void stopLoop()
    • startLoop

      public static void startLoop()
    • drawRectangle

      public static void drawRectangle(int x, int y, int width, int height)
      Draw a rectangle on the screen.
      Parameters:
      x - Left coordinate of rectangle
      y - Top coordinate of rectangle
      width - Width of the rectangle
      height - Height of the rectangle
    • drawOval

      public static void drawOval(int x, int y, int width, int height)
      Draw an oval on the screen. The x and y coordinate are the top-left corner of the bounding box of the oval.
      Parameters:
      x - Left coordinate of bounding box
      y - Top coordinate of bounding box
      width - Width of the oval
      height - Height of the oval
    • drawCircle

      public static void drawCircle(int centerX, int centerY, int radius)
      Draw a circle on the canvas, from the center point of the circle.
      Parameters:
      centerX - Center X-coordinate of circle
      centerY - Center Y-coordinate of circle
      radius - Radius of the circle
    • drawText

      public static void drawText(String text, int x, int y, int fontSize)
      Draw a piece of text on the canvas. The border color is used for the text.
      Parameters:
      text - Text to draw
      x - X-coordinate
      y - Y-coordinate
      fontSize - Font size in pixels
    • drawBorderedText

      public static void drawBorderedText(String text, int x, int y, int fontSize)
      Draw a piece of text on the canvas with a border. The border color is used for the border, the fill color is used for the contents within the borders.
      Parameters:
      text - Text to draw
      x - X-coordinate
      y - Y-coordinate
      fontSize - Font size in pixels
    • drawPoint

      public static void drawPoint(int x, int y)
      Draw a single point on the canvas, using 4 as width and height.
      Parameters:
      x - X-coordinate
      y - Y-coordinate
    • drawPoint

      public static void drawPoint(int x, int y, int size)
      Draw a single point on the canvas.
      Parameters:
      x - X-coordinate
      y - Y-coordinate
      size - Width and height of the point
    • drawLine

      public static void drawLine(int x1, int y1, int x2, int y2)
      Draw a line.
      Parameters:
      x1 - Start x
      y1 - Start y
      x2 - End x
      y2 - End y
    • drawImage

      public static void drawImage(String filename, int x, int y, int width, int height)
      Draw an image file (png or jpg) on the canvas. CAUTION: this method is very slow! Might take a while before you see the loaded image.
      Parameters:
      filename - Filename
      x - Left-coordinate
      y - Top-coordinate
      width - Width of the image on the canvas
      height - Height of the image on the canvas
    • drawImage

      public static void drawImage(String filename, int x, int y)
      Draw an image file (png or jpg) on the canvas. CAUTION: this method is very slow! Might take a while before you see the loaded image.
      Parameters:
      filename - Filename
      x - Left-coordinate
      y - Top-coordinate
    • drawPolygon

      public static void drawPolygon(Polygon polygon)
      Experimental method, used for the filled pen to add a shape to the SaxionApp. You can create a Polygon object and add it to the drawing here as well. Uses the current fill color and current line color for the polygon.
      Parameters:
      polygon - Polygon object
    • transformRotate

      public static void transformRotate(double degrees)
      Experimental: rotate around the center point of the canvas before drawing a shape. Uses AffineTransformations in Java.
      Parameters:
      degrees - Amount of degrees
    • transformRotate

      public static void transformRotate(double degrees, int x, int y)
      Experimental: rotate around the specified point on the canvas before drawing a shape.
      Parameters:
      degrees - Amount of degrees
      x - X point
      y - Y point
    • transformScale

      public static void transformScale(double scaleFactorX, double scaleFactorY)
      Experimental: scale the canvas before drawing a shape. Uses AffineTransformations in Java.
      Parameters:
      scaleFactorX - Scale factor for x-axis (1.0 = no scale)
      scaleFactorY - Scale factor for y-axis (1.0 = no scale)
    • transformTranslate

      public static void transformTranslate(double translateX, double translateY)
      Experimental: translate the canvas before drawing a shape. Uses AffineTransformations in Java.
      Parameters:
      translateX - Translation in pixels on x-axis.
      translateY - Translation in pixels on y-axis.
    • transformShear

      public static void transformShear(double shearX, double shearY)
      Experimental: shear transformation on the the canvas before drawing a shape. Uses AffineTransformations in Java.
      Parameters:
      shearX - The multiplier by which coordinates are shifted in the direction of the positive X axis as a factor of their Y coordinate
      shearY - The multiplier by which coordinates are shifted in the direction of the positive Y axis as a factor of their X coordinate
    • resetTransformations

      public static void resetTransformations()
      Experimental: Reset all Canvas Transformations Uses AffineTransformations in Java.
    • setBorderSize

      public static void setBorderSize(int size)
      Set the border size of the next object that you are going to draw. Make sure that 0 <= borderSize <= 500
      Parameters:
      size - Border size in pixels
    • setBorderColor

      public static void setBorderColor(Color color)
      Set the color of the border.
      Parameters:
      color - Color object
    • setTextDrawingColor

      public static void setTextDrawingColor(Color color)
      Set the color of the text. Same method as `setBorderColor(Color color)`.
      Parameters:
      color - Color object
    • turnFillOff

      public static void turnFillOff()
      Turn fill off
    • turnFillOn

      public static void turnFillOn()
      Turn fill on
    • turnBorderOff

      public static void turnBorderOff()
      Turns the border off
    • turnBorderOn

      public static void turnBorderOn()
      Turn border on
    • setFill

      public static void setFill(Color color)
      Set the fill color of the objects that you are going to draw.
      Parameters:
      color - Color object
    • setBackgroundColor

      public static void setBackgroundColor(Color color)
      Change the background color on top of which your content is shown.
      Parameters:
      color - Color object
    • getPen

      public static Pen getPen()
      Get an instance of the Pen. The pen gives you the ability to draw lines on the canvas.
      Returns:
      Pen object
    • getFilledPen

      public static FilledPen getFilledPen()
      Get an instance of the FilledPen. This object gives you the opportunity to draw shapes that have a border and a fill.
      Returns:
      FilledPen object
    • saveImage

      public static void saveImage(String filename)
      Saves the image as a PNG file.
      Parameters:
      filename - Filename (make sure it ends with .png).
    • getWidth

      public static int getWidth()
      Get the width of the canvas in pixels
      Returns:
      Number of pixels
    • getHeight

      public static int getHeight()
      Get the height of the canvas in pixels
      Returns:
      Number of pixels
    • getRandomColor

      public static Color getRandomColor()
      Creates a random color.
      Returns:
      Color object
    • createColor

      public static Color createColor(int red, int green, int blue)
      Create a new color using the RGB scale
      Parameters:
      red - Value between 0 and 255.
      green - Value between 0 and 255.
      blue - Value between 0 and 255.
      Returns:
      Color object
    • print

      public static void print(String text, Color color, boolean newLine)
      Print text to the application console.
      Parameters:
      text - The text to print. There are overloads of this method that allow other types, which will be converted to string.
      color - Optional: the color to use.
      newLine - Optional: boolean specifying if the output should move to the next line after this text. As a convenience, the printLine() method exists for this as well.
    • print

      public static void print(String text)
      Prints the text in the default color and does not include a line break.
      Parameters:
      text - - The text to print.
    • printLine

      public static void printLine(String text)
      Prints the text in the default color and includes a line break.
      Parameters:
      text - - The text to print.
    • print

      public static void print(String text, Color color)
      Prints the text in the color received and does not include a line break.
      Parameters:
      text - - The text to print.
      color - - Color the text will be printed
    • printLine

      public static void printLine(String text, Color color)
      Prints the text in the color received and includes a line break.
      Parameters:
      text - - The text to print.
      color - - Color the text will be printed
    • print

      public static <T> void print(T value)
      Overloaded method to allow printing for various data types not just String, the string value of the value will be printed, with default color and with no line break.
      Type Parameters:
      T - - Data type of the value parameter
      Parameters:
      value - - A generic value that can be received that will be converted into a String to be printed.
    • printLine

      public static <T> void printLine(T value)
      Overloaded method to allow printing for various data types not just String, the string value of the value will be printed, with default color and with a line break.
      Type Parameters:
      T - - Data type of the value parameter
      Parameters:
      value - - A generic value that can be received that will be converted into a String to be printed.
    • print

      public static <T> void print(T value, Color color)
      Overloaded method to allow printing for various data types not just String, the string value of the value will be printed, with color received as a parameter and with no line break.
      Type Parameters:
      T - - Data type of the value parameter
      Parameters:
      value - - A generic value that can be received that will be converted into a String to be printed.
      color - - Color the text will be printed
    • printLine

      public static <T> void printLine(T value, Color color)
      Overloaded method to allow printing for various data types not just String, the string value of the value will be printed, with color received as a parameter and with a line break.
      Type Parameters:
      T - - Data type of the value parameter
      Parameters:
      value - - A generic value that can be received that will be converted into a String to be printed.
      color - - Color the text will be printed
    • printLine

      public static void printLine()
      Adds a line break to the current position. (Prints an empty string and adds a line break)
    • getRandomValueBetween

      public static int getRandomValueBetween(int lowerBound, int upperBound)
      Generates a random integer value between lowerBound (inclusive) and upperBound (NOT inclusive). For example: getRandomValueBetween(0, 10) can produce any value between 0 and 9.
      Parameters:
      lowerBound - The specified lower bound for this random number generator. Values returned by this method will never be less than this value.
      upperBound - The specified upper bound for this random number generator. Values returned by this method will always be less than this value.
      Returns:
      A value that has the following properties lowerBound <= value < upperBound.
    • playSound

      public static void playSound(String filename, boolean loop)
      Plays a sound. (needs to be in .wav format).
      Parameters:
      filename - Filename of the file you want to play
      loop - True, if the file needs to be looped automatically
    • playSound

      public static void playSound(String filename)
      Plays a sound one time. File needs to be in .wav format.
      Parameters:
      filename - Filename of the file you want to play
    • stopSound

      public static void stopSound(String filename)
      Stop a certain sound that is currently playing.
      Parameters:
      filename - Filename of the file you want to stop
    • stopAllSounds

      public static void stopAllSounds()
      Stop all sounds that are currently playing.
    • quit

      public static void quit()
      Stops the application completely.