Package nl.saxion.app
Class SaxionApp
java.lang.Object
nl.saxion.app.SaxionApp
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:
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
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
add
(CanvasElement element) Add a canvas element, and make sure the canvas redraws.static void
clear()
Clears the current drawing and console messages.static Color
createColor
(int red, int green, int blue) Create a new color using the RGB scalestatic 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
Draw an image file (png or jpg) on the canvas.static void
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
drawPolygon
(Polygon polygon) 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
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 pixelsstatic Pen
getPen()
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
getWidth()
Get the width of the canvas in pixelsstatic void
pause()
Pause the drawing at this point.static void
Plays a sound one time.static void
Plays a sound.static void
Prints the text in the default color and does not include a line break.static void
Prints the text in the color received and does not include a line break.static void
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
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
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
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
quit()
Stops the application completely.static Character
readChar()
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
readInt()
Allows the user to type an integer (whole) number, concluded with an enter.static int
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
readString
(Color color) 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
Saves the image as a PNG file.static void
setBackgroundColor
(Color color) Change the background color on top of which your content is shown.static void
setBorderColor
(Color color) 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
Set the fill color of the objects that you are going to draw.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).static void
setTextDrawingColor
(Color color) 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
Open a SaxionApp window with default size and run your code in it.static void
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
stopLoop()
static void
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 offstatic void
Turn border onstatic void
Turn fill offstatic void
Turn fill on
-
Field Details
-
SAXION_GREEN
The official Saxion green color. -
SAXION_PINK
The official Saxion pink color. -
DEFAULT_BACKGROUND_COLOR
The default background color
-
-
Method Details
-
start
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
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 pixelsheight
- Window height in pixels
-
startGameLoop
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 pixelsheight
- Window height in pixelsms
- 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 pixelsheight
- The new height in pixels
-
showMessage
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 messagecolor
- The background color to draw the message on. The foreground color is always white.
-
add
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
Waits for a character to be typed and returns which character it was.- Returns:
- A Character. E.g.: 'a', 'Z' or '\n' (enter).
-
readChar
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
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
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
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
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 rectangley
- Top coordinate of rectanglewidth
- Width of the rectangleheight
- 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 boxy
- Top coordinate of bounding boxwidth
- Width of the ovalheight
- 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 circlecenterY
- Center Y-coordinate of circleradius
- Radius of the circle
-
drawText
Draw a piece of text on the canvas. The border color is used for the text.- Parameters:
text
- Text to drawx
- X-coordinatey
- Y-coordinatefontSize
- Font size in pixels
-
drawBorderedText
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 drawx
- X-coordinatey
- Y-coordinatefontSize
- 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-coordinatey
- Y-coordinate
-
drawPoint
public static void drawPoint(int x, int y, int size) Draw a single point on the canvas.- Parameters:
x
- X-coordinatey
- Y-coordinatesize
- 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 xy1
- Start yx2
- End xy2
- End y
-
drawImage
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
- Filenamex
- Left-coordinatey
- Top-coordinatewidth
- Width of the image on the canvasheight
- Height of the image on the canvas
-
drawImage
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
- Filenamex
- Left-coordinatey
- Top-coordinate
-
drawPolygon
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 degreesx
- X pointy
- 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 coordinateshearY
- 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
Set the color of the border.- Parameters:
color
- Color object
-
setTextDrawingColor
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
Set the fill color of the objects that you are going to draw.- Parameters:
color
- Color object
-
setBackgroundColor
Change the background color on top of which your content is shown.- Parameters:
color
- Color object
-
getPen
Get an instance of the Pen. The pen gives you the ability to draw lines on the canvas.- Returns:
- Pen object
-
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
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
Creates a random color.- Returns:
- Color object
-
createColor
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
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
Prints the text in the default color and does not include a line break.- Parameters:
text
- - The text to print.
-
printLine
Prints the text in the default color and includes a line break.- Parameters:
text
- - The text to print.
-
print
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
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
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
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
Plays a sound. (needs to be in .wav format).- Parameters:
filename
- Filename of the file you want to playloop
- True, if the file needs to be looped automatically
-
playSound
Plays a sound one time. File needs to be in .wav format.- Parameters:
filename
- Filename of the file you want to play
-
stopSound
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.
-