Class Console
- java.lang.Object
-
- javafx.scene.Node
-
- javafx.scene.canvas.Canvas
-
- hsafx.Console
-
- All Implemented Interfaces:
ActionListener
,EventListener
,javafx.css.Styleable
,javafx.event.EventTarget
public final class Console extends javafx.scene.canvas.Canvas implements ActionListener
This is a re-implementation of the old hsa console by Holt Software Associates in order to use Java FX, and to present an API consistent with the GraphicsContext and Scanner classes.UPDATE HISTORY: Some update history details have been lost. Notably the addition of mouse routines (Josh Gray?), anti-aliasing (Josh Gray?), and keyboard listener routines (Sam Scott, date unknown)
Update September 2014: Fixed bug in getRow() and getColumn(); they now report the current cursor position without requiring a print() first.
Update August 2012: Changed synchronization to synchronize on the associated Console object. Now application writers can also synchronize on the Console object to kill the last remaining cases of screen flicker.
Update April 30, 2010: Re-done from scratch in Swing with much code imported from the old hsa console. The main goals were to reduce screen flicker during animations and eliminate a couple of small bugs in the input routines.
- Version:
- 6.0 May 24, 2018
- Author:
- Sam Scott (conversion to Swing, conversion to FX), Michael Harwood (minor text printing bug fix), Josh Gray (getRow()/getColumn() bug fix), Tom West (original hsa code)
-
-
Constructor Summary
Constructors Constructor Description Console(double width, double height, int fontSize, String title, ConsoleView container)
Creates a new console.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
actionPerformed(ActionEvent e)
void
autoRefreshOff()
Turns off auto-refresh.void
autoRefreshOn()
Turns on auto-refresh (refreshes the screen 60 times per second)void
clear()
Clears the console to the background colorvoid
clearRect(double x, double y, double width, double height)
Clears a rectangle to the background colorvoid
close()
Terminates the program.void
drawImage(javafx.scene.image.Image img, double x, double y)
Draws an image on the console.void
drawImage(javafx.scene.image.Image img, double x, double y, double width, double height)
Draws an image on the console, stretched to a given height and width.void
fillArc(double x, double y, double width, double height, double startAngle, double arcAngle)
Draws an filled in portion of an oval, inscribed inside the rectangle defined by the arguments provided.void
fillMapleLeaf(double x, double y, double width, double height)
Draws a filled maple leaf on the console from (x, y) to (x + width, y + width).void
fillOval(double x, double y, double width, double height)
Draws a filled oval, inscribed inside the rectangle defined by the arguments provided.void
fillPolygon(double[] xs, double[] ys, int num)
Draws a filled polygon.void
fillRect(double x, double y, double width, double height)
Draws a filled rectanglevoid
fillRoundRect(double x, double y, double width, double height, double xRadius, double yRadius)
Draws a filled rectangle with rounded corners.void
fillStar(double x, double y, double width, double height)
Draws a filled star on the console from (x, y) to (x + width, y + width).void
fillText(String str, double x, double y)
Draws text on the console.char
getChar()
Pauses to read a character from the GraphicsConsole without showing the cursor.char
getChar(boolean cursor)
Pauses to read the next character entered on the keyboard.int
getColumn()
Retrieve the current column for print/println.char
getKeyChar()
Returns the char for the key currently held down.String
getKeyCode()
Returns the code for the key currently held down.char
getLastKeyChar()
Returns the char for the last key that was pressed.String
getLastKeyCode()
Returns the code for the last key pressed.boolean
getMouseButton(int buttonNum)
Returns true if the specified button is pressed, false otherwise.int
getMouseClick()
Returns non-zero if the mouse has been clicked since the last time the click was queried, zero if the mouse was not clicked.double
getMouseDX()
Returns the distance dragged in the x-axis (positive or negative)double
getMouseDY()
Returns the distance dragged in the y-axis (positive or negative)double
getMouseX()
Returns the X coordinate of the mouse pointer position within the drawing area.double
getMouseY()
Returns the Y coordinate of the mouse pointer position within the drawing area.int
getNumColumns()
Get the total number of columns on the console for print/println.int
getNumRows()
Get the total number of rows on the console for print/println.int
getRow()
Retrieve the current row for print/println.boolean
isKeyDown(char key)
Call this to find out whether or not a particular character is being held down.boolean
isKeyDown(String key)
Call this to find out whether or not a particular key is being held down.boolean
isMouseDragged()
Returns true of mouse is being dragged (button down and mouse moving triggers a drag event)void
mouseMoved(javafx.scene.input.MouseEvent e)
String
next()
Reads in input from the keyboard buffer until it hits a whitespace, which indicates the end of a token.boolean
nextBoolean()
Reads a boolean from the GraphicsConsole.byte
nextByte()
Reads an 8-bit integer (a "byte") from the GraphicsConsole.char
nextChar()
Reads a single character from the Console.double
nextDouble()
Reads a double precision floating point number (a "double") from the GraphicsConsole.float
nextFloat()
Reads a floating point number (a "float") from the GraphicsConsole.int
nextInt()
Reads a 32-bit integer (an "int") from the GraphicsConsole.String
nextLine()
Reads a full line of text from the GraphicsConsole.long
nextLong()
Reads a 64-bit integer (a "long") from the GraphicsConsole.short
nextShort()
Reads a 16-bit integer (a "short") from the GraphicsConsole.void
print(boolean value)
Writes the text representation of a boolean to the GraphicsConsole.void
print(boolean value, int fieldSize)
Writes the text representation of a boolean to the GraphicsConsole with a specified field size.void
print(byte number)
Writes the text representation of an 8-bit integer (a "byte") to the GraphicsConsole.void
print(byte number, int fieldSize)
Writes the text representation of an 8-bit integer (a "byte") to the GraphicsConsole with a specified field size.void
print(char ch)
Writes a character to the GraphicsConsole.void
print(char ch, int fieldSize)
Writes a character to the GraphicsConsole with a specified field size.void
print(double number)
Writes a double precision floating point number (a "double") to the GraphicsConsole.void
print(double number, int fieldSize)
Writes a double precision floating point number (a "double") to the GraphicsConsole with a specified field size.void
print(double number, int fieldSize, int decimalPlaces)
Writes a double precision floating point number (a "double") to the GraphicsConsole with a specified field size and a specified number of decimal places.void
print(float number)
Writes a floating point number (a "float") to the GraphicsConsole.void
print(float number, int fieldSize)
Writes a floating point number (a "float") to the GraphicsConsole with a specified field size.void
print(float number, int fieldSize, int decimalPlaces)
Writes a floating point number (a "double") to the GraphicsConsole with a specified field size and a specified number of decimal places.void
print(int number)
Writes the text representation of an 32-bit integer (an "int") to the GraphicsConsole.void
print(int number, int fieldSize)
Writes the text representation of an 32-bit integer (an "int") to the GraphicsConsole with a specified field size.void
print(long number)
Writes the text representation of an 64-bit integer (a "long") to the GraphicsConsole.void
print(long number, int fieldSize)
Writes the text representation of an 64-bit integer (a "long") to the GraphicsConsole with a specified field size.void
print(short number)
Writes the text representation of an 16-bit integer (a "short") to the GraphicsConsole.void
print(short number, int fieldSize)
Writes the text representation of an 16-bit integer (a "short") to the GraphicsConsole with a specified field size.void
print(Object o)
void
print(String text)
Write a string to the Console.void
print(String text, int fieldSize)
Writes a string to the GraphicsConsole with a specified field size.void
println()
Writes a newline to the GraphicsConsole.void
println(boolean value)
Writes the text representation of a boolean to the GraphicsConsole followed by a newline.void
println(boolean value, int fieldSize)
Writes the text representation of a boolean to the GraphicsConsole with a specified field size followed by a newline.void
println(byte number)
Writes the text representation of an 8-bit integer (a "byte") to the GraphicsConsole followed by a newline.void
println(byte number, int fieldSize)
Writes the text representation of an 8-bit integer (a "byte") to the GraphicsConsole with a specified field size followed by a newline.void
println(char ch)
Writes a character to the GraphicsConsole followed by a newline.void
println(char ch, int fieldSize)
Writes a character to the GraphicsConsole with a specified field size.void
println(double number)
Writes a double precision floating point number (a "double") to the GraphicsConsole followed by a newline.void
println(double number, int fieldSize)
Writes a double precision floating point number (a "double") to the GraphicsConsole with a specified field size followed by a newline.void
println(double number, int fieldSize, int decimalPlaces)
Writes a double precision floating point number (a "double") to the GraphicsConsole with a specified field size and a specified number of decimal places followed by a newline.void
println(float number)
Writes a floating point number (a "float") to the GraphicsConsole followed by a newline.void
println(float number, int fieldSize)
Writes a floating point number (a "float") to the GraphicsConsole with a specified field size followed by a newline.void
println(float number, int fieldSize, int decimalPlaces)
Writes a floating point number (a "double") to the GraphicsConsole with a specified field size and a specified number of decimal places followed by a newline.void
println(int number)
Writes the text representation of an 32-bit integer (an "int") to the GraphicsConsole followed by a newline.void
println(int number, int fieldSize)
Writes the text representation of an 32-bit integer (an "int") to the GraphicsConsole with a specified field size followed by a newline.void
println(long number)
Writes the text representation of an 64-bit integer (a "long") to the GraphicsConsole followed by a newline.void
println(long number, int fieldSize)
Writes the text representation of an 64-bit integer (a "long") to the GraphicsConsole with a specified field size followed by a newline.void
println(short number)
Writes the text representation of an 16-bit integer (a "short") to the GraphicsConsole followed by a newline.void
println(short number, int fieldSize)
Writes the text representation of an 16-bit integer (a "short") to the GraphicsConsole with a specified field size followed by a newline.void
println(Object o)
void
println(String text)
Writes a string to the GraphicsConsole followed by a newline.void
println(String text, int fieldSize)
Writes a string to the GraphicsConsole with a specified field size followed by a newline.void
quit()
Quits the program.void
refresh()
Refreshes the screen.double
reportSize(String s, javafx.scene.text.Font myFont)
void
setBackground(int r, int g, int b)
void
setBackground(int r, int g, int b, double a)
void
setBackground(String color)
Sets the color to use for the stroke commandsvoid
setCursor(int row, int col)
Set the row and column for print/println.void
setFill(int r, int g, int b)
void
setFill(int r, int g, int b, double a)
void
setFill(String color)
Sets the color to use for the fill commands as well as print/println.void
setFont(String fname, double fsize)
Sets the font to use for fillText and strokeText.void
setLineWidth(int width)
Sets the line width for stroke commandsvoid
setStroke(int r, int g, int b)
void
setStroke(int r, int g, int b, double a)
void
setStroke(String color)
Sets the color to use for the stroke commandsvoid
showDialog(String message, String title)
Shows a popup dialog using Swing JOptionPane.String
showInputDialog(String message, String title)
This uses Swing JOptionPane to get text from the user.void
sleep(long milliSeconds)
A simplified sleep function handles the try/catch or "throws InterruptedException" that Thread.sleep() produces.void
strokeArc(double x, double y, double width, double height, double startAngle, double arcAngle)
Draws an outline of a portion of an oval, inscribed inside the rectangle defined by the arguments provided.void
strokeLine(double x1, double y1, double x2, double y2)
Draws a line.void
strokeMapleLeaf(double x, double y, double width, double height)
Draws a maple leaf outline on the console from (x, y) to (x + width, y + width).void
strokeOval(double x, double y, double width, double height)
Draws an outline of an oval, inscribed inside the rectangle defined by the arguments provided.void
strokePolygon(double[] xs, double[] ys, int num)
Draws a polygon outline.void
strokeRect(double x, double y, double width, double height)
Draws a rectangle outlinevoid
strokeRoundRect(double x, double y, double width, double height, double xRadius, double yRadius)
Draws the outline of a rectangle with rounded corners.void
strokeStar(double x, double y, double width, double height)
Draws a star outline on the console from (x, y) to (x + width, y + width).void
strokeText(String str, double x, double y)
Draws outlined text on the console.-
Methods inherited from class javafx.scene.canvas.Canvas
getGraphicsContext2D, getHeight, getWidth, heightProperty, setHeight, setWidth, widthProperty
-
Methods inherited from class javafx.scene.Node
accessibleHelpProperty, accessibleRoleDescriptionProperty, accessibleRoleProperty, accessibleTextProperty, addEventFilter, addEventHandler, applyCss, autosize, blendModeProperty, boundsInLocalProperty, boundsInParentProperty, buildEventDispatchChain, cacheHintProperty, cacheProperty, clipProperty, computeAreaInScreen, contains, contains, cursorProperty, depthTestProperty, disabledProperty, disableProperty, effectiveNodeOrientationProperty, effectProperty, eventDispatcherProperty, executeAccessibleAction, fireEvent, focusedProperty, focusTraversableProperty, getAccessibleHelp, getAccessibleRole, getAccessibleRoleDescription, getAccessibleText, getBaselineOffset, getBlendMode, getBoundsInLocal, getBoundsInParent, getCacheHint, getClassCssMetaData, getClip, getContentBias, getCssMetaData, getCursor, getDepthTest, getEffect, getEffectiveNodeOrientation, getEventDispatcher, getId, getInitialCursor, getInitialFocusTraversable, getInputMethodRequests, getLayoutBounds, getLayoutX, getLayoutY, getLocalToParentTransform, getLocalToSceneTransform, getNodeOrientation, getOnContextMenuRequested, getOnDragDetected, getOnDragDone, getOnDragDropped, getOnDragEntered, getOnDragExited, getOnDragOver, getOnInputMethodTextChanged, getOnKeyPressed, getOnKeyReleased, getOnKeyTyped, getOnMouseClicked, getOnMouseDragEntered, getOnMouseDragExited, getOnMouseDragged, getOnMouseDragOver, getOnMouseDragReleased, getOnMouseEntered, getOnMouseExited, getOnMouseMoved, getOnMousePressed, getOnMouseReleased, getOnRotate, getOnRotationFinished, getOnRotationStarted, getOnScroll, getOnScrollFinished, getOnScrollStarted, getOnSwipeDown, getOnSwipeLeft, getOnSwipeRight, getOnSwipeUp, getOnTouchMoved, getOnTouchPressed, getOnTouchReleased, getOnTouchStationary, getOnZoom, getOnZoomFinished, getOnZoomStarted, getOpacity, getParent, getProperties, getPseudoClassStates, getRotate, getRotationAxis, getScaleX, getScaleY, getScaleZ, getScene, getStyle, getStyleableParent, getStyleClass, getTransforms, getTranslateX, getTranslateY, getTranslateZ, getTypeSelector, getUserData, getViewOrder, hasProperties, hoverProperty, idProperty, inputMethodRequestsProperty, intersects, intersects, isCache, isDisable, isDisabled, isFocused, isFocusTraversable, isHover, isManaged, isMouseTransparent, isPickOnBounds, isPressed, isResizable, isVisible, layoutBoundsProperty, layoutXProperty, layoutYProperty, localToParent, localToParent, localToParent, localToParent, localToParent, localToParentTransformProperty, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToSceneTransformProperty, localToScreen, localToScreen, localToScreen, localToScreen, localToScreen, lookup, lookupAll, managedProperty, maxHeight, maxWidth, minHeight, minWidth, mouseTransparentProperty, nodeOrientationProperty, notifyAccessibleAttributeChanged, onContextMenuRequestedProperty, onDragDetectedProperty, onDragDoneProperty, onDragDroppedProperty, onDragEnteredProperty, onDragExitedProperty, onDragOverProperty, onInputMethodTextChangedProperty, onKeyPressedProperty, onKeyReleasedProperty, onKeyTypedProperty, onMouseClickedProperty, onMouseDragEnteredProperty, onMouseDragExitedProperty, onMouseDraggedProperty, onMouseDragOverProperty, onMouseDragReleasedProperty, onMouseEnteredProperty, onMouseExitedProperty, onMouseMovedProperty, onMousePressedProperty, onMouseReleasedProperty, onRotateProperty, onRotationFinishedProperty, onRotationStartedProperty, onScrollFinishedProperty, onScrollProperty, onScrollStartedProperty, onSwipeDownProperty, onSwipeLeftProperty, onSwipeRightProperty, onSwipeUpProperty, onTouchMovedProperty, onTouchPressedProperty, onTouchReleasedProperty, onTouchStationaryProperty, onZoomFinishedProperty, onZoomProperty, onZoomStartedProperty, opacityProperty, parentProperty, parentToLocal, parentToLocal, parentToLocal, parentToLocal, parentToLocal, pickOnBoundsProperty, prefHeight, prefWidth, pressedProperty, pseudoClassStateChanged, queryAccessibleAttribute, relocate, removeEventFilter, removeEventHandler, requestFocus, resize, resizeRelocate, rotateProperty, rotationAxisProperty, scaleXProperty, scaleYProperty, scaleZProperty, sceneProperty, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, screenToLocal, screenToLocal, screenToLocal, setAccessibleHelp, setAccessibleRole, setAccessibleRoleDescription, setAccessibleText, setBlendMode, setCache, setCacheHint, setClip, setCursor, setDepthTest, setDisable, setDisabled, setEffect, setEventDispatcher, setEventHandler, setFocused, setFocusTraversable, setHover, setId, setInputMethodRequests, setLayoutX, setLayoutY, setManaged, setMouseTransparent, setNodeOrientation, setOnContextMenuRequested, setOnDragDetected, setOnDragDone, setOnDragDropped, setOnDragEntered, setOnDragExited, setOnDragOver, setOnInputMethodTextChanged, setOnKeyPressed, setOnKeyReleased, setOnKeyTyped, setOnMouseClicked, setOnMouseDragEntered, setOnMouseDragExited, setOnMouseDragged, setOnMouseDragOver, setOnMouseDragReleased, setOnMouseEntered, setOnMouseExited, setOnMouseMoved, setOnMousePressed, setOnMouseReleased, setOnRotate, setOnRotationFinished, setOnRotationStarted, setOnScroll, setOnScrollFinished, setOnScrollStarted, setOnSwipeDown, setOnSwipeLeft, setOnSwipeRight, setOnSwipeUp, setOnTouchMoved, setOnTouchPressed, setOnTouchReleased, setOnTouchStationary, setOnZoom, setOnZoomFinished, setOnZoomStarted, setOpacity, setPickOnBounds, setPressed, setRotate, setRotationAxis, setScaleX, setScaleY, setScaleZ, setStyle, setTranslateX, setTranslateY, setTranslateZ, setUserData, setViewOrder, setVisible, snapshot, snapshot, startDragAndDrop, startFullDrag, styleProperty, toBack, toFront, toString, translateXProperty, translateYProperty, translateZProperty, usesMirroring, viewOrderProperty, visibleProperty
-
-
-
-
Constructor Detail
-
Console
public Console(double width, double height, int fontSize, String title, ConsoleView container)
Creates a new console.- Parameters:
width
- The width in pixelsheight
- The height in pixelsfontSize
- The font size for print/printlntitle
- The title of the windowcontainer
- The ConsoleView object container
-
-
Method Detail
-
reportSize
public double reportSize(String s, javafx.scene.text.Font myFont)
-
close
public void close()
Terminates the program.
-
clear
public void clear()
Clears the console to the background color
-
clearRect
public void clearRect(double x, double y, double width, double height)
Clears a rectangle to the background color- Parameters:
x
- left coordinate in pixelsy
- top coordinate in pixelswidth
- width of rectangleheight
- height of rectangle
-
setFill
public void setFill(String color)
Sets the color to use for the fill commands as well as print/println.- Parameters:
color
- Any legal CSS color string (color name, hex color, rgb, rgba, etc.)(color
-
setFill
public void setFill(int r, int g, int b)
-
setFill
public void setFill(int r, int g, int b, double a)
-
setStroke
public void setStroke(String color)
Sets the color to use for the stroke commands- Parameters:
color
- Any legal CSS color string (color name, hex color, rgb, rgba, etc.)(color
-
setStroke
public void setStroke(int r, int g, int b)
-
setStroke
public void setStroke(int r, int g, int b, double a)
-
setBackground
public void setBackground(String color)
Sets the color to use for the stroke commands- Parameters:
color
- Any legal CSS color string (color name, hex color, rgb, rgba, etc.)(color
-
setBackground
public void setBackground(int r, int g, int b)
-
setBackground
public void setBackground(int r, int g, int b, double a)
-
setLineWidth
public void setLineWidth(int width)
Sets the line width for stroke commands- Parameters:
width
- A line width in pixels
-
fillRect
public void fillRect(double x, double y, double width, double height)
Draws a filled rectangle- Parameters:
x
- left coordinate in pixelsy
- top coordinate in pixelswidth
- width of rectangleheight
- height of rectangle
-
strokeRect
public void strokeRect(double x, double y, double width, double height)
Draws a rectangle outline- Parameters:
x
- left coordinate in pixelsy
- top coordinate in pixelswidth
- width of rectangleheight
- height of rectangle
-
fillOval
public void fillOval(double x, double y, double width, double height)
Draws a filled oval, inscribed inside the rectangle defined by the arguments provided.- Parameters:
x
- left coordinate in pixelsy
- top coordinate in pixelswidth
- width of rectangleheight
- height of rectangle
-
strokeOval
public void strokeOval(double x, double y, double width, double height)
Draws an outline of an oval, inscribed inside the rectangle defined by the arguments provided.- Parameters:
x
- left coordinate in pixelsy
- top coordinate in pixelswidth
- width of rectangleheight
- height of rectangle
-
strokeLine
public void strokeLine(double x1, double y1, double x2, double y2)
Draws a line.- Parameters:
x1
- X coordinate of first endpointy1
- Y coordinate of first endpointx2
- X coordinate of second endpointy2
- Y coordinate of second endpoint
-
strokePolygon
public void strokePolygon(double[] xs, double[] ys, int num)
Draws a polygon outline.- Parameters:
xs
- An array of x coordinates for each vertexys
- An array of y coordinates for each vertexnum
- The number of vertices in the polygon
-
fillPolygon
public void fillPolygon(double[] xs, double[] ys, int num)
Draws a filled polygon.- Parameters:
xs
- An array of x coordinates for each vertexys
- An array of y coordinates for each vertexnum
- The number of vertices in the polygon
-
strokeArc
public void strokeArc(double x, double y, double width, double height, double startAngle, double arcAngle)
Draws an outline of a portion of an oval, inscribed inside the rectangle defined by the arguments provided.- Parameters:
x
- left coordinate in pixelsy
- top coordinate in pixelswidth
- width of rectangleheight
- height of rectanglestartAngle
- angle in degrees to start drawing it (0 = east, 90 = north, 180 = west, 270 = south, etc.)arcAngle
- number of degrees of ark to draw (counter-clockwise)
-
fillArc
public void fillArc(double x, double y, double width, double height, double startAngle, double arcAngle)
Draws an filled in portion of an oval, inscribed inside the rectangle defined by the arguments provided.- Parameters:
x
- left coordinate in pixelsy
- top coordinate in pixelswidth
- width of rectangleheight
- height of rectanglestartAngle
- angle in degrees to start drawing it (0 = east, 90 = north, 180 = west, 270 = south, etc.)arcAngle
- number of degrees of ark to draw (counter-clockwise)
-
strokeRoundRect
public void strokeRoundRect(double x, double y, double width, double height, double xRadius, double yRadius)
Draws the outline of a rectangle with rounded corners.- Parameters:
x
- left coordinate in pixelsy
- top coordinate in pixelswidth
- width of rectangleheight
- height of rectanglexRadius
- The width of a rounded corneryRadius
- The height of a rounded corner
-
fillRoundRect
public void fillRoundRect(double x, double y, double width, double height, double xRadius, double yRadius)
Draws a filled rectangle with rounded corners.- Parameters:
x
- left coordinate in pixelsy
- top coordinate in pixelswidth
- width of rectangleheight
- height of rectanglexRadius
- The width of a rounded corneryRadius
- The height of a rounded corner
-
fillText
public void fillText(String str, double x, double y)
Draws text on the console.- Parameters:
str
- The text to drawx
- The x location of the left edge of the texty
- The y location of the bottom of the text
-
strokeText
public void strokeText(String str, double x, double y)
Draws outlined text on the console.- Parameters:
str
- The text to drawx
- The x location of the left edge of the texty
- The y location of the bottom of the text
-
setFont
public void setFont(String fname, double fsize)
Sets the font to use for fillText and strokeText.- Parameters:
fname
- A legal font namefsize
- The size of the font in pixels
-
drawImage
public void drawImage(javafx.scene.image.Image img, double x, double y)
Draws an image on the console.- Parameters:
img
- The Image object to drawx
- The location of the left sidey
- The location of the top of the image
-
drawImage
public void drawImage(javafx.scene.image.Image img, double x, double y, double width, double height)
Draws an image on the console, stretched to a given height and width.- Parameters:
img
- The Image object to drawx
- The location of the left sidey
- The location of the top of the imagewidth
- The width of the image in pixelsheight
- The height of the image in pixels
-
strokeStar
public void strokeStar(double x, double y, double width, double height)
Draws a star outline on the console from (x, y) to (x + width, y + width). Adapted from hsa.- Parameters:
x
- The x coordinate of the top left corner of the rectangle that the star is inscribed in.y
- The y coordinate of the top left corner of the rectangle that the star is inscribed in.width
- The width of the rectangle that the star is inscribed in.height
- The height of the rectangle that the star is inscribed in.
-
fillStar
public void fillStar(double x, double y, double width, double height)
Draws a filled star on the console from (x, y) to (x + width, y + width). Adapted from hsa.- Parameters:
x
- The x coordinate of the top left corner of the rectangle that the star is inscribed in.y
- The y coordinate of the top left corner of the rectangle that the star is inscribed in.width
- The width of the rectangle that the star is inscribed in.height
- The height of the rectangle that the star is inscribed in.
-
strokeMapleLeaf
public void strokeMapleLeaf(double x, double y, double width, double height)
Draws a maple leaf outline on the console from (x, y) to (x + width, y + width). Adapted from hsa.- Parameters:
x
- The x coordinate of the top left corner of the rectangle that the maple leaf is inscribed in.y
- The y coordinate of the top left corner of the rectangle that the maple leaf is inscribed in.width
- The width of the rectangle that the maple leaf is inscribed in.height
- The height of the rectangle that the maple leaf is inscribed in.
-
fillMapleLeaf
public void fillMapleLeaf(double x, double y, double width, double height)
Draws a filled maple leaf on the console from (x, y) to (x + width, y + width). Adapted from hsa.- Parameters:
x
- int The x coordinate of the top left corner of the rectangle that the maple leaf is inscribed in.y
- int The y coordinate of the top left corner of the rectangle that the maple leaf is inscribed in.width
- int The width of the rectangle that the maple leaf is inscribed in.height
- int The height of the rectangle that the maple leaf is inscribed in.
-
setCursor
public void setCursor(int row, int col)
Set the row and column for print/println.- Parameters:
row
- The row number (0 = top)col
- The column number (0 = left)
-
getColumn
public int getColumn()
Retrieve the current column for print/println.- Returns:
- The current column
-
getRow
public int getRow()
Retrieve the current row for print/println.- Returns:
- The current row
-
getNumColumns
public int getNumColumns()
Get the total number of columns on the console for print/println.- Returns:
- The number of columns available
-
getNumRows
public int getNumRows()
Get the total number of rows on the console for print/println.- Returns:
- The number of rows available
-
print
public void print(String text)
Write a string to the Console. Adapted from hsa.- Parameters:
text
- The string to be written to the Console
-
println
public void println()
Writes a newline to the GraphicsConsole. Adapted from hsa.
-
println
public void println(byte number)
Writes the text representation of an 8-bit integer (a "byte") to the GraphicsConsole followed by a newline. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.
-
println
public void println(byte number, int fieldSize)
Writes the text representation of an 8-bit integer (a "byte") to the GraphicsConsole with a specified field size followed by a newline. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.
-
println
public void println(char ch)
Writes a character to the GraphicsConsole followed by a newline. Adapted from hsa.- Parameters:
ch
- The character to be written to the GraphicsConsole.
-
println
public void println(char ch, int fieldSize)
Writes a character to the GraphicsConsole with a specified field size. Adapted from hsa.- Parameters:
ch
- The character to be written to the GraphicsConsole.fieldSize
- The field width that the character is to be written in.
-
println
public void println(double number)
Writes a double precision floating point number (a "double") to the GraphicsConsole followed by a newline. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.
-
println
public void println(double number, int fieldSize)
Writes a double precision floating point number (a "double") to the GraphicsConsole with a specified field size followed by a newline. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.
-
println
public void println(double number, int fieldSize, int decimalPlaces)
Writes a double precision floating point number (a "double") to the GraphicsConsole with a specified field size and a specified number of decimal places followed by a newline. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.decimalPlaces
- The number of decimal places of the number to be displayed.
-
println
public void println(float number)
Writes a floating point number (a "float") to the GraphicsConsole followed by a newline. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.
-
println
public void println(float number, int fieldSize)
Writes a floating point number (a "float") to the GraphicsConsole with a specified field size followed by a newline. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.
-
println
public void println(float number, int fieldSize, int decimalPlaces)
Writes a floating point number (a "double") to the GraphicsConsole with a specified field size and a specified number of decimal places followed by a newline. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.decimalPlaces
- The number of decimal places of the number to be displayed.
-
println
public void println(int number)
Writes the text representation of an 32-bit integer (an "int") to the GraphicsConsole followed by a newline. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.
-
println
public void println(int number, int fieldSize)
Writes the text representation of an 32-bit integer (an "int") to the GraphicsConsole with a specified field size followed by a newline. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.
-
println
public void println(long number)
Writes the text representation of an 64-bit integer (a "long") to the GraphicsConsole followed by a newline. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.
-
print
public void print(byte number)
Writes the text representation of an 8-bit integer (a "byte") to the GraphicsConsole. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.
-
print
public void print(byte number, int fieldSize)
Writes the text representation of an 8-bit integer (a "byte") to the GraphicsConsole with a specified field size. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.
-
print
public void print(char ch)
Writes a character to the GraphicsConsole. Adapted from hsa.- Parameters:
ch
- The character to be written to the GraphicsConsole.
-
print
public void print(char ch, int fieldSize)
Writes a character to the GraphicsConsole with a specified field size. Adapted from hsa.- Parameters:
ch
- The character to be written to the GraphicsConsole.fieldSize
- The field width that the character is to be written in.
-
print
public void print(double number)
Writes a double precision floating point number (a "double") to the GraphicsConsole. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.
-
print
public void print(double number, int fieldSize)
Writes a double precision floating point number (a "double") to the GraphicsConsole with a specified field size. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.
-
print
public void print(double number, int fieldSize, int decimalPlaces)
Writes a double precision floating point number (a "double") to the GraphicsConsole with a specified field size and a specified number of decimal places. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.decimalPlaces
- The number of decimal places of the number to be displayed.
-
print
public void print(float number)
Writes a floating point number (a "float") to the GraphicsConsole. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.
-
print
public void print(float number, int fieldSize)
Writes a floating point number (a "float") to the GraphicsConsole with a specified field size.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.
-
print
public void print(float number, int fieldSize, int decimalPlaces)
Writes a floating point number (a "double") to the GraphicsConsole with a specified field size and a specified number of decimal places. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.decimalPlaces
- The number of decimal places of the number to be displayed.
-
print
public void print(int number)
Writes the text representation of an 32-bit integer (an "int") to the GraphicsConsole. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.
-
print
public void print(int number, int fieldSize)
Writes the text representation of an 32-bit integer (an "int") to the GraphicsConsole with a specified field size. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.
-
print
public void print(long number)
Writes the text representation of an 64-bit integer (a "long") to the GraphicsConsole. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.
-
print
public void print(long number, int fieldSize)
Writes the text representation of an 64-bit integer (a "long") to the GraphicsConsole with a specified field size. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.
-
print
public void print(String text, int fieldSize)
Writes a string to the GraphicsConsole with a specified field size. Adapted from hsa.- Parameters:
text
- The string to be written to the GraphicsConsole.fieldSize
- The field width that the string is to be written in.
-
print
public void print(short number)
Writes the text representation of an 16-bit integer (a "short") to the GraphicsConsole. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.
-
print
public void print(short number, int fieldSize)
Writes the text representation of an 16-bit integer (a "short") to the GraphicsConsole with a specified field size. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.
-
print
public void print(boolean value)
Writes the text representation of a boolean to the GraphicsConsole. Adapted from hsa.- Parameters:
value
- The boolean to be written to the GraphicsConsole.
-
print
public void print(boolean value, int fieldSize)
Writes the text representation of a boolean to the GraphicsConsole with a specified field size. Adapted from hsa.- Parameters:
value
- The boolean to be written to the GraphicsConsole.fieldSize
- The field width that the boolean is to be written in.
-
println
public void println(long number, int fieldSize)
Writes the text representation of an 64-bit integer (a "long") to the GraphicsConsole with a specified field size followed by a newline. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.
-
println
public void println(String text)
Writes a string to the GraphicsConsole followed by a newline. Adapted from hsa.- Parameters:
text
- The string to be written to the GraphicsConsole.
-
println
public void println(String text, int fieldSize)
Writes a string to the GraphicsConsole with a specified field size followed by a newline. Adapted from hsa.- Parameters:
text
- The string to be written to the GraphicsConsole.fieldSize
- The field width that the string is to be written in.
-
println
public void println(short number)
Writes the text representation of an 16-bit integer (a "short") to the GraphicsConsole followed by a newline. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.
-
println
public void println(short number, int fieldSize)
Writes the text representation of an 16-bit integer (a "short") to the GraphicsConsole with a specified field size followed by a newline. Adapted from hsa.- Parameters:
number
- The number to be written to the GraphicsConsole.fieldSize
- The field width that the number is to be written in.
-
println
public void println(boolean value)
Writes the text representation of a boolean to the GraphicsConsole followed by a newline. Adapted from hsa.- Parameters:
value
- The boolean to be written to the GraphicsConsole.
-
println
public void println(boolean value, int fieldSize)
Writes the text representation of a boolean to the GraphicsConsole with a specified field size followed by a newline. Adapted from hsa.- Parameters:
value
- The boolean to be written to the GraphicsConsole.fieldSize
- The field width that the boolean is to be written in.
-
println
public void println(Object o)
-
print
public void print(Object o)
-
getChar
public char getChar()
Pauses to read a character from the GraphicsConsole without showing the cursor.- Returns:
- the character read.
-
getChar
public char getChar(boolean cursor)
Pauses to read the next character entered on the keyboard. Ignores characters currently in the line buffer.- Parameters:
cursor
- T/F to indicate whether the cursor is displayed.- Returns:
- The next character entered on the keyboard.
-
nextChar
public char nextChar()
Reads a single character from the Console. Note that this discards any whitespace. If you want to get every character on the line, use the nextLine() method.- Returns:
- The character read from the Console
-
nextBoolean
public boolean nextBoolean()
Reads a boolean from the GraphicsConsole. The actual text in the GraphicsConsole must be either "true" or "false" although case is irrelevant.- Returns:
- The boolean value read from the GraphicsConsole.
-
nextByte
public byte nextByte()
Reads an 8-bit integer (a "byte") from the GraphicsConsole. The actual text in the GraphicsConsole must be a number from -128 to 127.- Returns:
- The byte value read from the GraphicsConsole.
-
nextDouble
public double nextDouble()
Reads a double precision floating point number (a "double") from the GraphicsConsole.- Returns:
- The double value read from the GraphicsConsole.
-
nextFloat
public float nextFloat()
Reads a floating point number (a "float") from the GraphicsConsole.- Returns:
- The float value read from the GraphicsConsole.
-
nextInt
public int nextInt()
Reads a 32-bit integer (an "int") from the GraphicsConsole. The actual text in the GraphicsConsole must be a number from -2147483648 to 2147483647.- Returns:
- The int value read from the GraphicsConsole.
-
nextLine
public String nextLine()
Reads a full line of text from the GraphicsConsole.- Returns:
- The line of text read from the GraphicsConsole.
-
nextLong
public long nextLong()
Reads a 64-bit integer (a "long") from the GraphicsConsole.- Returns:
- The long value read from the GraphicsConsole.
-
nextShort
public short nextShort()
Reads a 16-bit integer (a "short") from the GraphicsConsole. The actual text in the GraphicsConsole must be a number from -32768 to 32767.- Returns:
- The short value read from the GraphicsConsole.
-
next
public String next()
Reads in input from the keyboard buffer until it hits a whitespace, which indicates the end of a token.- Returns:
- the string read.
-
getKeyCode
public String getKeyCode()
Returns the code for the key currently held down.- Returns:
- A String value representing the keycode
-
getKeyChar
public char getKeyChar()
Returns the char for the key currently held down.- Returns:
- the char that was typed
-
getLastKeyCode
public String getLastKeyCode()
Returns the code for the last key pressed.- Returns:
- A String value representing the keycode
-
getLastKeyChar
public char getLastKeyChar()
Returns the char for the last key that was pressed.- Returns:
- the char that was typed
-
isKeyDown
public boolean isKeyDown(String key)
Call this to find out whether or not a particular key is being held down.- Parameters:
key
- The String keycode for the key you are interested in (the same one returned by getKeyCode())- Returns:
- True if the key is currently held down, false otherwise
-
isKeyDown
public boolean isKeyDown(char key)
Call this to find out whether or not a particular character is being held down.- Parameters:
key
- The character code for the key- Returns:
- True if the key is currently held down, false otherwise
-
sleep
public void sleep(long milliSeconds)
A simplified sleep function handles the try/catch or "throws InterruptedException" that Thread.sleep() produces.- Parameters:
milliSeconds
- the time to sleep in ms.
-
quit
public void quit()
Quits the program.
-
refresh
public void refresh()
Refreshes the screen. This will be called automatically unless you turn auto-refresh off.
-
autoRefreshOn
public void autoRefreshOn()
Turns on auto-refresh (refreshes the screen 60 times per second)
-
autoRefreshOff
public void autoRefreshOff()
Turns off auto-refresh. With auto-refresh off, you must call refresh() yourself to see the results of any drawing. Use this if you are doing animation or coding a game loop. Do all the drawing first, then call refresh(), then sleep(), then repeat.
-
showDialog
public void showDialog(String message, String title)
Shows a popup dialog using Swing JOptionPane. It does not display an icon- Parameters:
message
- to displaytitle
- for the popup message box
-
showInputDialog
public String showInputDialog(String message, String title)
This uses Swing JOptionPane to get text from the user. The text can be from one letter, or a word, to a whole line. The text is terminated with Enter. It displays the Question icon.Sample code:
name = c.showInputDialog("What is your name?", ""); //handle CANCEL option if(name == null){ System.out.println("Cancel pressed"); System.exit(0); //or do something else } //handle OK option with no text if (name.equals("")) { name = "No Name"; }
- Parameters:
message
- to displaytitle
- for the popup message box- Returns:
- The string typed in. If the Cancel button is pressed, the return value is equal to null. If OK is pressed without anything typed in, the return value is a zero length string.
-
getMouseButton
public boolean getMouseButton(int buttonNum)
Returns true if the specified button is pressed, false otherwise.Buttons are numbered 0, 1 or 2.
- Parameters:
buttonNum
- mouse button number (0,1,2)- Returns:
- T/F if that button has been pressed.
-
getMouseClick
public int getMouseClick()
Returns non-zero if the mouse has been clicked since the last time the click was queried, zero if the mouse was not clicked.1 = single click 2 = double click 3 = triple click etc.
- Returns:
- number of clicks
-
getMouseX
public double getMouseX()
Returns the X coordinate of the mouse pointer position within the drawing area.- Returns:
- x coordinate of mouse pointer position
-
getMouseY
public double getMouseY()
Returns the Y coordinate of the mouse pointer position within the drawing area.- Returns:
- y coordinate of mouse pointer position
-
isMouseDragged
public boolean isMouseDragged()
Returns true of mouse is being dragged (button down and mouse moving triggers a drag event)- Returns:
-
getMouseDX
public double getMouseDX()
Returns the distance dragged in the x-axis (positive or negative)- Returns:
- distance in pixels dragged in x-direction
-
getMouseDY
public double getMouseDY()
Returns the distance dragged in the y-axis (positive or negative)- Returns:
- distance in pixels dragged in y-direction
-
actionPerformed
public void actionPerformed(ActionEvent e)
- Specified by:
actionPerformed
in interfaceActionListener
-
mouseMoved
public void mouseMoved(javafx.scene.input.MouseEvent e)
-
-