ch.aplu.util
Class Console

java.lang.Object
  |
  +--ch.aplu.util.Console
All Implemented Interfaces:
java.awt.event.ActionListener, java.util.EventListener, java.awt.event.KeyListener, java.awt.print.Printable, java.lang.Runnable, java.awt.event.WindowListener

public class Console
extends java.lang.Object
implements java.awt.event.WindowListener, java.awt.event.ActionListener, java.awt.event.KeyListener, java.lang.Runnable, java.awt.print.Printable

Class to define a console window.
Once the console window is instantiated all output to stdout and stderr is redirected to this window.
To avoid creating an object the static function init() may be used. Only one console instance is allowed.
Part of code from Rjhm van den Bergh (rvdb@comweb.nl) with thanks for the permission to use and distribute


Field Summary
 
Fields inherited from interface java.awt.print.Printable
NO_SUCH_PAGE, PAGE_EXISTS
 
Constructor Summary
Console()
          Construct a Console with default attributes ( see init() ).
Console(Position position, Size size, java.awt.Font font)
          Construct a Console with attributes.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent evt)
          For internal use only
static void clear()
          Erase all text in console window
static void delay(int time)
          Delay execution for the given amount of time ( in ms ).
static void end()
          Close the console instance.
 java.lang.String getAbout()
          Return copywrite information
static java.lang.Double getDouble()
          Same as readDouble() but returns a Double object.
 javax.swing.JFrame getFrame()
          Return a reference to the JFrame instance used by the console
static java.lang.Integer getInt()
          Same as readInt() but returns an Integer object.
static char getKey()
          Return the unicode character associated with last key pressed.
static int getKeyCode()
          Return the keycode associated with last key pressed.
static int getKeyCodeWait()
          Wait until a key is typed and return the keycode associated with last key pressed.
static char getKeyWait()
          Wait until a key is typed and return the unicode character associated with last key pressed.
static char getLastKeyChar()
          Return the key character associated with last key pressed.
static int getLastKeyCode()
          Return the key code associated with last key pressed.
static int getLastModifiers()
          Return the modifiers associated with last key pressed.
static java.lang.String getLastModifiersText()
          Return the modifiers text associated with last key pressed.
static javax.swing.JTextArea getTextArea()
          Return a reference to the JTextArea of the console window
 java.lang.String getVersion()
          Return version information
static Console init()
          Create a new Console with default attributes
and returns a reference to it.
static Console init(java.awt.Font font)
          Create a Console with specified font and returns a reference to it.
static Console init(Position position, Size size)
          Create a Console with specified size and position and returns a reference to it.
static Console init(Position position, Size size, java.awt.Font font)
          Create a new Console with specified size, position and font and returns a reference to it.
static Console init(java.lang.String filename)
          Redirect all output to stdout and stderr to a text file with the given filename.
static boolean kbhit()
          Return true if a key was hit since the last time the one character buffer was read with getKeyChar() oder getKeyCharWait().
 void keyPressed(java.awt.event.KeyEvent evt)
          For internal use only.
 void keyReleased(java.awt.event.KeyEvent evt)
          For internal use only.
 void keyTyped(java.awt.event.KeyEvent evt)
          For internal use only.
static java.lang.String pad(java.lang.String num, int fieldWidth)
          Right justify the given number in a field with the given field width (pad the field with leading spaces).
static java.lang.String pad(java.lang.String num, int fieldWidth, int decimalWidth)
          Pad given number with trailing spaces to optain decimal width and right justify in a field with the given width (pad the the field with leading spaces).
static Position position(int ulx, int uly)
          Return a Position ref with specified upperleft x and y coordinates.
static Console print(boolean b)
          Print a boolean value.
static Console print(char c)
          Print a character.
static Console print(char[] s)
          Print an array of characters.
static Console print(double d)
          Print a double-precision floating-point number.
static Console print(float f)
          Print a floating-point number.
 int print(java.awt.Graphics g, java.awt.print.PageFormat pf, int pageIndex)
          For internal use only.
static Console print(int i)
          Print an integer.
static Console print(long l)
          Print a long integer.
static Console print(java.lang.Object obj)
          Print an object.
static Console print(java.lang.String s)
          Print a string.
static Console println()
          Terminate the current line by writing the line separator string.
static Console println(boolean b)
          Print a boolean and then terminate the line.
static Console println(char c)
          Print a character and then terminate the line.
static Console println(char[] s)
          Print an array of characters and then terminate the line.
static Console println(double d)
          Print a double and then terminate the line.
static Console println(float f)
          Print a float and then terminate the line.
static Console println(int i)
          Print an integer and then terminate the line.
static Console println(long l)
          Print a long and then terminate the line.
static Console println(java.lang.Object obj)
          Print an Object and then terminate the line.
static Console println(java.lang.String s)
          Print a String and then terminate the line.
 boolean printScreen()
          Same as printScreen(scale) with scale = 1
 boolean printScreen(double scale)
          Print the current text area to an attached printer with the given magnification scale factor.
static char readChar()
          Wait until a key is typed and return the unicode character associated it.
static double readDouble()
          Wait until a sequence of numbers with trailing newline is typed.
static int readInt()
          Wait until a sequence of numbers with trailing newline is typed.
static java.lang.String readLine()
          Wait until a sequence of characters with trailing newline is typed.
 void run()
          For internal use only.
static void showFonts()
          Show all available fonts.
static Size size(int width, int height)
          Return a Size ref with specified width and height.
static void terminate()
          Terminate application.
 void windowActivated(java.awt.event.WindowEvent e)
          For internal use only.
 void windowClosed(java.awt.event.WindowEvent evt)
          For internal use only.
 void windowClosing(java.awt.event.WindowEvent evt)
          For internal use only.
 void windowDeactivated(java.awt.event.WindowEvent e)
          For internal use only.
 void windowDeiconified(java.awt.event.WindowEvent e)
          For internal use only.
 void windowIconified(java.awt.event.WindowEvent e)
          For internal use only.
 void windowOpened(java.awt.event.WindowEvent e)
          For internal use only.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Console

public Console(Position position,
               Size size,
               java.awt.Font font)
Construct a Console with attributes.

Parameters:
position - a ref to a Position object
size - a ref to a Size object
font - a ref to a Font object
See Also:
Position, Size, Font

Console

public Console()
Construct a Console with default attributes ( see init() ).

Method Detail

init

public static Console init()
Create a new Console with default attributes
and returns a reference to it. Default size: half the screen dimensions
Default position: centered to screen
Default font: Font( "Courier New", Font.PLAIN, 16 )


init

public static Console init(Position position,
                           Size size)
Create a Console with specified size and position and returns a reference to it.

Parameters:
position - a ref to a Position object
size - a ref to a Size object
See Also:
Position, Size

init

public static Console init(java.awt.Font font)
Create a Console with specified font and returns a reference to it.

Parameters:
font - a ref to a Font object
See Also:
Font

init

public static Console init(Position position,
                           Size size,
                           java.awt.Font font)
Create a new Console with specified size, position and font and returns a reference to it.

Parameters:
position - a ref to a Position object
size - a ref to a Size object
font - a ref to a Font object
See Also:
Position, Size, Font

init

public static Console init(java.lang.String filename)
Redirect all output to stdout and stderr to a text file with the given filename. If the file exists the text is appended, otherwise the file is created. (No console window is shown, so no input is possible.) If null is given, all output to stdout and stderr is ignored. This may be used to hide output from library functions.


end

public static void end()
Close the console instance.


clear

public static void clear()
Erase all text in console window


position

public static Position position(int ulx,
                                int uly)
Return a Position ref with specified upperleft x and y coordinates. May be used in init() to avoid the keyword new


size

public static Size size(int width,
                        int height)
Return a Size ref with specified width and height. May be used in init() to avoid the keyword new


windowClosed

public void windowClosed(java.awt.event.WindowEvent evt)
For internal use only.

Specified by:
windowClosed in interface java.awt.event.WindowListener

windowClosing

public void windowClosing(java.awt.event.WindowEvent evt)
For internal use only.

Specified by:
windowClosing in interface java.awt.event.WindowListener

windowActivated

public void windowActivated(java.awt.event.WindowEvent e)
For internal use only.

Specified by:
windowActivated in interface java.awt.event.WindowListener

windowDeactivated

public void windowDeactivated(java.awt.event.WindowEvent e)
For internal use only.

Specified by:
windowDeactivated in interface java.awt.event.WindowListener

windowDeiconified

public void windowDeiconified(java.awt.event.WindowEvent e)
For internal use only.

Specified by:
windowDeiconified in interface java.awt.event.WindowListener

windowIconified

public void windowIconified(java.awt.event.WindowEvent e)
For internal use only.

Specified by:
windowIconified in interface java.awt.event.WindowListener

windowOpened

public void windowOpened(java.awt.event.WindowEvent e)
For internal use only.

Specified by:
windowOpened in interface java.awt.event.WindowListener

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent evt)
For internal use only

Specified by:
actionPerformed in interface java.awt.event.ActionListener

run

public void run()
For internal use only.

Specified by:
run in interface java.lang.Runnable

kbhit

public static boolean kbhit()
Return true if a key was hit since the last time the one character buffer was read with getKeyChar() oder getKeyCharWait(). (Put the current thread to sleep for 1 ms).


getKey

public static char getKey()
Return the unicode character associated with last key pressed. Return KeyEvent.CHAR_UNDEFINED if the one character buffer is empty. (No echo in the console window.)


getKeyCode

public static int getKeyCode()
Return the keycode associated with last key pressed. Return KeyEvent.CHAR_UNDEFINED if the one character buffer is empty. (No echo in the console window.)


getKeyWait

public static char getKeyWait()
Wait until a key is typed and return the unicode character associated with last key pressed. (No echo in the console window.)


getKeyCodeWait

public static int getKeyCodeWait()
Wait until a key is typed and return the keycode associated with last key pressed. (No echo in the console window.)


readChar

public static char readChar()
Wait until a key is typed and return the unicode character associated it. (Echo the character in the console window.)


readLine

public static java.lang.String readLine()
Wait until a sequence of characters with trailing newline is typed. Return the string (without the trailing newline) (Echo the characters in the console wiindow.)


getLastKeyCode

public static int getLastKeyCode()
Return the key code associated with last key pressed.


getLastKeyChar

public static char getLastKeyChar()
Return the key character associated with last key pressed.


getLastModifiers

public static int getLastModifiers()
Return the modifiers associated with last key pressed.


getLastModifiersText

public static java.lang.String getLastModifiersText()
Return the modifiers text associated with last key pressed.


readInt

public static int readInt()
Wait until a sequence of numbers with trailing newline is typed. Return the converted integer (if possible) (Echo the character in the console window.)


getInt

public static java.lang.Integer getInt()
Same as readInt() but returns an Integer object. Return null, if entered character sequence cannot converted to an integer.


readDouble

public static double readDouble()
Wait until a sequence of numbers with trailing newline is typed. Return the converted double (if possible) (Echo the character in the console window.)


getDouble

public static java.lang.Double getDouble()
Same as readDouble() but returns a Double object. Return null, if entered character sequence cannot converted to a double.


terminate

public static void terminate()
Terminate application.


keyPressed

public void keyPressed(java.awt.event.KeyEvent evt)
For internal use only.

Specified by:
keyPressed in interface java.awt.event.KeyListener

keyReleased

public void keyReleased(java.awt.event.KeyEvent evt)
For internal use only.

Specified by:
keyReleased in interface java.awt.event.KeyListener

keyTyped

public void keyTyped(java.awt.event.KeyEvent evt)
For internal use only.

Specified by:
keyTyped in interface java.awt.event.KeyListener

showFonts

public static void showFonts()
Show all available fonts.


delay

public static void delay(int time)
Delay execution for the given amount of time ( in ms ).


pad

public static java.lang.String pad(java.lang.String num,
                                   int fieldWidth)
Right justify the given number in a field with the given field width (pad the field with leading spaces). Return the padded string (if possible)


pad

public static java.lang.String pad(java.lang.String num,
                                   int fieldWidth,
                                   int decimalWidth)
Pad given number with trailing spaces to optain decimal width and right justify in a field with the given width (pad the the field with leading spaces). Return the padded string (if possible)


getTextArea

public static javax.swing.JTextArea getTextArea()
Return a reference to the JTextArea of the console window


print

public static Console print(boolean b)
Print a boolean value. Return a reference to Console to concatenate print methods.


print

public static Console print(char c)
Print a character. Return a reference to Console to concatenate print methods.


print

public static Console print(char[] s)
Print an array of characters. Return a reference to Console to concatenate print methods.


print

public static Console print(double d)
Print a double-precision floating-point number. Return a reference to Console to concatenate print methods.


print

public static Console print(float f)
Print a floating-point number. Return a reference to Console to concatenate print methods.


print

public static Console print(int i)
Print an integer. Return a reference to Console to concatenate print methods.


print

public static Console print(long l)
Print a long integer. Return a reference to Console to concatenate print methods.


print

public static Console print(java.lang.Object obj)
Print an object. Return a reference to Console to concatenate print methods.


print

public static Console print(java.lang.String s)
Print a string. Return a reference to Console to concatenate print methods.


println

public static Console println()
Terminate the current line by writing the line separator string. Return a reference to Console to concatenate print methods.


println

public static Console println(boolean b)
Print a boolean and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(char c)
Print a character and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(char[] s)
Print an array of characters and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(double d)
Print a double and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(float f)
Print a float and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(int i)
Print an integer and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(long l)
Print a long and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(java.lang.Object obj)
Print an Object and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(java.lang.String s)
Print a String and then terminate the line. Return a reference to Console to concatenate print methods.


print

public int print(java.awt.Graphics g,
                 java.awt.print.PageFormat pf,
                 int pageIndex)
For internal use only. Implements Printable.print().

Specified by:
print in interface java.awt.print.Printable

printScreen

public boolean printScreen(double scale)
Print the current text area to an attached printer with the given magnification scale factor. A standard printer dialog is shown before printing is started.
Return false, if printing is canceled in this dialog, return true, when print data is sent to the printer spooler.
Only the text thats fits on one page is printed.


printScreen

public boolean printScreen()
Same as printScreen(scale) with scale = 1


getFrame

public javax.swing.JFrame getFrame()
Return a reference to the JFrame instance used by the console


getVersion

public java.lang.String getVersion()
Return version information


getAbout

public java.lang.String getAbout()
Return copywrite information