public class CommandLineProgram extends Object implements ProgramInterface, acm.io.IOModel, MouseListener, MouseMotionListener, KeyListener, ActionListener
ConsoleProgram
in an environment that lacks a graphics context. As of JDK 1.4, it is
illegal even to instantiate an applet in a non-graphics environment
(called "headless" in the Java terminology), which means that the program
can no longer extend Applet
or JApplet
. This
class creates a stripped-down program class that duplicates the operation
of a ConsoleProgram
using the standard I/O streams.Modifier | Constructor and Description |
---|---|
protected |
CommandLineProgram()
This code initializes the program data structures.
|
Modifier and Type | Method and Description |
---|---|
void |
actionPerformed(ActionEvent e)
Called when a component (typically a button) is activated.
|
void |
add(Component comp,
String region,
Object constraints)
Adds the component to the specified border region with the indicated
constraints object.
|
void |
addActionListeners()
Adds the program as an
ActionListener to every button in
the structure that does not have a listener already. |
void |
addActionListeners(ActionListener listener)
Adds the specified listener to every button in
the structure that does not have a listener already.
|
void |
addExitHook(Object obj)
Requests that the program call the
exit method in the
specified object before exiting. |
protected void |
addImpl(Component comp,
Object constraints,
int index)
Adds the specified component to the content pane using the specified constraints and index.
|
static void |
checkIfHeadless(String className)
Checks to see if the program is running in a headless environment and, if so,
runs it in that form.
|
protected IOConsole |
createConsole()
Creates the console used by the
ConsoleProgram . |
protected acm.io.IODialog |
createDialogIO()
Creates the dialog used for interaction (primarily by the
DialogProgram
class). |
protected static HashMap<String,String> |
createParameterTable(String[] args)
Creates a hash table containing the parameters specified in the
argument list.
|
void |
destroy()
Called when the program has been told to destroy itself.
|
protected void |
endHook()
Performs class-specific cleanup for the program just after
it finishes.
|
void |
exit()
Exits from the program.
|
protected AppletStub |
getAppletStub()
Retrieves the applet stub.
|
protected String[] |
getArgumentArray()
Retrieves the array of arguments passed in from the command line, or
null if no arguments are available. |
protected Component |
getBorder(String side)
Returns the component installed as a border on the specified side, which must
be one of the constants from
BorderLayout (NORTH ,
SOUTH , EAST , WEST ). |
IOConsole |
getConsole()
Returns the console associated with this program.
|
acm.io.IODialog |
getDialog()
Returns the dialog used for user interaction.
|
int |
getHeight()
Returns the height of the central region.
|
acm.io.IOModel |
getInputModel()
Returns the
IOModel used for program input, which will
typically be either the default IOConsole or IODialog object. |
LayoutManager |
getLayout()
Gets the layout manager for the central region of the content pane.
|
protected boolean |
getMacMenuBarFlag()
Retrieves the setting of the Mac menu bar flag.
|
acm.io.IOModel |
getOutputModel()
Returns the
IOModel used for program output, which will
typically be either the default IOConsole or IODialog object. |
String |
getParameter(String name)
Returns the parameter associated with name.
|
protected HashMap<String,String> |
getParameterTable()
Retrieves the parameter table.
|
Dimension |
getPreferredSize()
Returns the preferred size of the content pane.
|
BufferedReader |
getReader()
Returns a
BufferedReader whose input comes from the console. |
JPanel |
getRegionPanel(String region)
Gets the
JPanel for the specified region. |
String |
getTitle()
Gets the title of this program.
|
int |
getWidth()
Returns the width of the central region.
|
PrintWriter |
getWriter()
Returns a
PrintWriter whose output is directed to the console. |
void |
init()
The init method is called at startup time before the run method is
called.
|
boolean |
isAppletMode()
Returns
true if this program is running as an applet in a browser. |
static boolean |
isHeadless()
Returns true if the program is running in non-graphical "headless" mode.
|
protected boolean |
isStarted()
Checks to see whether this program has started, usually by checking to see
whether some pane exists.
|
void |
keyPressed(KeyEvent e)
Called when a key is pressed.
|
void |
keyReleased(KeyEvent e)
Called when a key is released.
|
void |
keyTyped(KeyEvent e)
Called when a key is typed (i.e., pressed and released).
|
static void |
main(String[] args)
Every application must either contain a "Main-Class" entry in its
manifest file or include a main method that looks like this, where
MyClass is the name of the program class: |
void |
menuAction(String cmd)
Called whenever an action event is detected in the menu bar.
|
void |
mouseClicked(MouseEvent e)
Called when the mouse is clicked.
|
void |
mouseDragged(MouseEvent e)
Called when the mouse is dragged with the button down.
|
void |
mouseEntered(MouseEvent e)
Called when the mouse enters the source (which may be
either a component or a
GObject ). |
void |
mouseExited(MouseEvent e)
Called when the mouse exits the source (which may be
either a component or a
GObject ). |
void |
mouseMoved(MouseEvent e)
Called when the mouse is moved.
|
void |
mousePressed(MouseEvent e)
Called when the mouse button is pressed.
|
void |
mouseReleased(MouseEvent e)
Called when the mouse button is released.
|
void |
pause(double milliseconds)
Causes the program to delay for the given number of milliseconds.
|
void |
print(boolean x)
Makes sure that
print can display a boolean . |
void |
print(char x)
Makes sure that
print can display a char . |
void |
print(double x)
Makes sure that
print can display a double . |
void |
print(float x)
Makes sure that
print can display a float . |
void |
print(int x)
Makes sure that
print can display an int . |
void |
print(long x)
Makes sure that
print can display a long . |
void |
print(Object x)
Makes sure that
print can display an Object . |
void |
print(String value)
Displays the argument value on the console, leaving the cursor at the end of
the output.
|
void |
printf(String format,
Object... args)
Writes a formatted string to this output stream using the specified format string and arguments.
|
void |
println()
Advances the console cursor to the beginning of the next line.
|
void |
println(boolean x)
Makes sure that
println can display a boolean . |
void |
println(char x)
Makes sure that
println can display a char . |
void |
println(double x)
Makes sure that
println can display a double . |
void |
println(float x)
Makes sure that
println can display a float . |
void |
println(int x)
Makes sure that
println can display an int . |
void |
println(long x)
Makes sure that
println can display a long . |
void |
println(Object x)
Makes sure that
println can display an Object . |
void |
println(String value)
Displays the argument value on the console and then advances the cursor
to the beginning of the next line.
|
boolean |
readBoolean()
Reads and returns a boolean value (
true or false ). |
boolean |
readBoolean(String prompt)
Prompts the user to enter a boolean value, which is returned as
the value of this method.
|
boolean |
readBoolean(String prompt,
String trueLabel,
String falseLabel)
Prompts the user to enter a boolean value, which is matched against the
labels provided.
|
double |
readDouble()
Reads and returns a double-precision value from the user.
|
double |
readDouble(double low,
double high)
Reads and returns a double-precision value from the user, which is
constrained to be within the specified inclusive range.
|
double |
readDouble(String prompt)
Prompts the user to enter an double-precision number, which is then
returned as the value of this method.
|
double |
readDouble(String prompt,
double low,
double high)
Prompts the user to enter an double-precision number, which is then returned
as the value of this method.
|
int |
readInt()
Reads and returns an integer value from the user.
|
int |
readInt(int low,
int high)
Reads and returns an integer value from the user, which is constrained to
be within the specified inclusive range.
|
int |
readInt(String prompt)
Prompts the user to enter an integer, which is then returned as the value
of this method.
|
int |
readInt(String prompt,
int low,
int high)
Prompts the user to enter an integer, which is then returned as the value
of this method.
|
String |
readLine()
Reads and returns a line of input from the console.
|
String |
readLine(String prompt)
Prompts the user for a line of input.
|
void |
remove(Component comp)
Removes the specified component from the central region.
|
void |
remove(int index)
Removes the component at the specified index from the central region.
|
void |
removeAll()
Removes all components from the central region.
|
void |
repaint()
Forwards repaint to the content pane.
|
void |
run()
Contains the code to be executed for each specific program subclass.
|
protected void |
setAppletStub(AppletStub stub)
Sets the applet stub for this program in a way that makes it possible for
clients to retrieve it.
|
void |
setBackground(Color color)
Sets the background for the central region of the content pane.
|
void |
setConsole(IOConsole console)
Sets the console associated with this program.
|
void |
setLayout(LayoutManager layout)
Sets the layout manager for the central region of the content pane.
|
protected void |
setMacMenuBarFlag(boolean flag)
Sets a flag indicating whether applications running on the Macintosh
should use standard Mac menus.
|
protected void |
setParameterTable(HashMap<String,String> ht)
Sets the parameter table for this program.
|
void |
setParameterTable(Map<String,String> table) |
void |
setStartupObject(Object obj) |
void |
setTitle(String title)
Sets the title of this program.
|
void |
showErrorMessage(String msg)
Displays the error message in the standard output model.
|
void |
start() |
void |
start(String[] args)
Starts the program using the specified argument list.
|
protected void |
startHook()
Performs class-specific initialization for the program just before
it starts.
|
void |
validate()
Forwards validate to the content pane.
|
protected CommandLineProgram()
public static boolean isHeadless()
public static void checkIfHeadless(String className)
className
- The name of the main classpublic void run()
run
so that it contains the code for your application.run
in interface ProgramInterface
public void init()
init
method. This method is used only for certain styles
of application development that have their roots in the applet world;
other styles will not ordinarily use or override this method.init
in interface ProgramInterface
public void pause(double milliseconds)
pause
in interface ProgramInterface
public void print(String value)
print
method is overloaded so that
value
can be of any type.print
in interface acm.io.IOModel
print
in interface ProgramInterface
value
- The value to be displayedpublic final void print(boolean x)
print
can display a boolean
.print
in interface acm.io.IOModel
public final void print(char x)
print
can display a char
.print
in interface acm.io.IOModel
public final void print(double x)
print
can display a double
.print
in interface acm.io.IOModel
public final void print(float x)
print
can display a float
.print
in interface acm.io.IOModel
public final void print(int x)
print
can display an int
.print
in interface acm.io.IOModel
public final void print(long x)
print
can display a long
.print
in interface acm.io.IOModel
public final void print(Object x)
print
can display an Object
.print
in interface acm.io.IOModel
print
in interface ProgramInterface
public void printf(String format, Object... args)
printf
in interface ProgramInterface
format
- A format string as described in Java's Format string syntax.args
- Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Javaâ„¢ Virtual Machine Specification. The behavior on a null argument depends on the conversion.IllegalFormatException
- If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions.NullPointerException
- If the format is nullpublic void println()
println
in interface acm.io.IOModel
println
in interface ProgramInterface
public void println(String value)
println
method is
overloaded so that value
can be of any type.println
in interface acm.io.IOModel
println
in interface ProgramInterface
value
- The value to be displayedpublic final void println(boolean x)
println
can display a boolean
.println
in interface acm.io.IOModel
public final void println(char x)
println
can display a char
.println
in interface acm.io.IOModel
public final void println(double x)
println
can display a double
.println
in interface acm.io.IOModel
public final void println(float x)
println
can display a float
.println
in interface acm.io.IOModel
public final void println(int x)
println
can display an int
.println
in interface acm.io.IOModel
public final void println(long x)
println
can display a long
.println
in interface acm.io.IOModel
public final void println(Object x)
println
can display an Object
.println
in interface acm.io.IOModel
println
in interface ProgramInterface
public void showErrorMessage(String msg)
showErrorMessage
in interface acm.io.IOModel
msg
- The error msg to be displayedpublic final String readLine()
readLine
in interface acm.io.IOModel
String
public String readLine(String prompt)
readLine
in interface acm.io.IOModel
prompt
- The prompt string to display to the userString
public final int readInt()
setExceptionOnError
method.readInt
in interface acm.io.IOModel
public final int readInt(int low, int high)
setExceptionOnError
method.readInt
in interface acm.io.IOModel
low
- The lowest value in the permitted rangehigh
- The highest value in the permitted rangepublic final int readInt(String prompt)
setExceptionOnError
method.readInt
in interface acm.io.IOModel
prompt
- The prompt string to display to the userpublic int readInt(String prompt, int low, int high)
low
and high
. If the user types a value that
is not a legal integer or is outside the specified range, the method
ordinarily offers the user a chance to reenter the data,
although this behavior can be changed using the
setExceptionOnError
method.readInt
in interface acm.io.IOModel
prompt
- The prompt string to display to the userlow
- The lowest value in the permitted rangehigh
- The highest value in the permitted rangepublic final double readDouble()
setExceptionOnError
method.readDouble
in interface acm.io.IOModel
double
public final double readDouble(double low, double high)
setExceptionOnError
method.readDouble
in interface acm.io.IOModel
low
- The lowest value in the permitted rangehigh
- The highest value in the permitted rangedouble
public final double readDouble(String prompt)
setExceptionOnError
method.readDouble
in interface acm.io.IOModel
prompt
- The prompt string to display to the userdouble
public double readDouble(String prompt, double low, double high)
low
and high
. If the user types a value
that is not a legal number, the method ordinarily offers the user a chance
to reenter the data, although this behavior can be changed using the
setExceptionOnError
method.readDouble
in interface acm.io.IOModel
prompt
- The prompt string to display to the userlow
- The lowest value in the permitted rangehigh
- The highest value in the permitted rangedouble
public final boolean readBoolean()
true
or false
).
The input must match one of these strings, ignoring case. If the user
types a value that is not one of these possibilities, the method ordinarily
offers the user a chance to reenter the data, although this behavior
can be changed using the
setExceptionOnError
method.readBoolean
in interface acm.io.IOModel
public final boolean readBoolean(String prompt)
setExceptionOnError
method.readBoolean
in interface acm.io.IOModel
prompt
- The prompt string to display to the userpublic boolean readBoolean(String prompt, String trueLabel, String falseLabel)
readBoolean
ordinarily offers the user a chance
to reenter the data, although this behavior can be changed using the
setExceptionOnError
method.readBoolean
in interface acm.io.IOModel
prompt
- The prompt string to display to the usertrueLabel
- The string used to indicate true
falseLabel
- The string used to indicate false
public boolean isAppletMode()
true
if this program is running as an applet in a browser.true
if this program is running as an applet, false
otherwisepublic void setConsole(IOConsole console)
console
- The IOConsole
object used for this programpublic IOConsole getConsole()
IOConsole
object used for this programpublic acm.io.IODialog getDialog()
IODialog
object used for this programpublic acm.io.IOModel getInputModel()
IOModel
used for program input, which will
typically be either the default IOConsole
or IODialog
object.IOModel
used for program inputpublic acm.io.IOModel getOutputModel()
IOModel
used for program output, which will
typically be either the default IOConsole
or IODialog
object.IOModel
used for program outputpublic BufferedReader getReader()
BufferedReader
whose input comes from the console.Reader
for use with this consolepublic PrintWriter getWriter()
PrintWriter
whose output is directed to the console.PrintWriter
for use with this consolepublic JPanel getRegionPanel(String region)
JPanel
for the specified region.region
- The region of the window (NORTH
, SOUTH
,
EAST
, WEST
, or CENTER
)JPanel
for that subregionpublic void add(Component comp, String region, Object constraints)
comp
- The component to be addedregion
- The region of the window (NORTH
, SOUTH
,
EAST
, WEST
, or CENTER
)constraints
- The constraints objectpublic void addActionListeners()
ActionListener
to every button in
the structure that does not have a listener already.public void addActionListeners(ActionListener listener)
listener
- The ActionListener
to be addedpublic void setTitle(String title)
setTitle
in interface ProgramInterface
title
- The title for this programpublic String getTitle()
getTitle
in interface ProgramInterface
public void setParameterTable(Map<String,String> table)
setParameterTable
in interface ProgramInterface
public void setStartupObject(Object obj)
setStartupObject
in interface ProgramInterface
public void start()
start
in interface ProgramInterface
public void start(String[] args)
start
in interface ProgramInterface
args
- An array of strings passed to the programpublic void exit()
super.exit()
at the end of their processing.exit
in interface ProgramInterface
public void addExitHook(Object obj)
exit
method in the
specified object before exiting.public void mouseClicked(MouseEvent e)
mouseClicked
is always preceded by both a mousePressed
and a
mouseReleased
event for the same source.mouseClicked
in interface MouseListener
public void mousePressed(MouseEvent e)
mousePressed
in interface MouseListener
public void mouseReleased(MouseEvent e)
mouseReleased
in interface MouseListener
public void mouseEntered(MouseEvent e)
GObject
).mouseEntered
in interface MouseListener
public void mouseExited(MouseEvent e)
GObject
).mouseExited
in interface MouseListener
public void mouseMoved(MouseEvent e)
mouseMoved
in interface MouseMotionListener
public void mouseDragged(MouseEvent e)
mouseDragged
call is always preceded by a
mousePressed
call for the same source. If the
mouse is pressed elsewhere and then enters a source with
the button down, no drag event occurs. Moreover, once the
mouse button goes down in a particular source, only that
source will receive mouse events until the button goes up.
Those events, moreover, are reported even in the mouse
travels outside the domain of the object.mouseDragged
in interface MouseMotionListener
public void keyTyped(KeyEvent e)
keyTyped
in interface KeyListener
public void keyPressed(KeyEvent e)
keyPressed
in interface KeyListener
public void keyReleased(KeyEvent e)
keyReleased
in interface KeyListener
public void actionPerformed(ActionEvent e)
actionPerformed
in interface ActionListener
protected IOConsole createConsole()
ConsoleProgram
. Subclasses can
override this method to create their own console types.protected acm.io.IODialog createDialogIO()
DialogProgram
class). Subclasses can override this method to create their own dialog types.public Dimension getPreferredSize()
public int getWidth()
public int getHeight()
public String getParameter(String name)
name
- The name of the parameternull
if nonepublic void setLayout(LayoutManager layout)
layout
- The layout manager to usepublic LayoutManager getLayout()
public void setBackground(Color color)
color
- The new background colorprotected void addImpl(Component comp, Object constraints, int index)
public void remove(int index)
index
- The index position of the component to removepublic void remove(Component comp)
comp
- The component to removepublic void removeAll()
public void validate()
public void repaint()
public void destroy()
public static void main(String[] args)
MyClass
is the name of the program class:
public static void main(String[] args) {
new MyClass().start();
}
If the program needs the command line arguments, the args
array can be passed to the start
method and then retrieved
using the getArgumentArray
method.
args
- An array of string argumentspublic void menuAction(String cmd)
protected void setMacMenuBarFlag(boolean flag)
true
.
Setting this value to false
means that Mac programs
use the same in-window JMenuBar
approach used on other
platforms.flag
- true
to use Mac menu style; false
otherwiseprotected boolean getMacMenuBarFlag()
true
if Mac menu style is supported; false
otherwiseprotected Component getBorder(String side)
BorderLayout
(NORTH
,
SOUTH
, EAST
, WEST
).side
- The side (NORTH
, SOUTH
, EAST
,
or WEST
)protected String[] getArgumentArray()
null
if no arguments are available.protected boolean isStarted()
protected void startHook()
protected void endHook()
protected void setAppletStub(AppletStub stub)
stub
- The applet stubprotected AppletStub getAppletStub()
protected void setParameterTable(HashMap<String,String> ht)
ht
- The parameter tableprotected HashMap<String,String> getParameterTable()
protected static HashMap<String,String> createParameterTable(String[] args)
name=
value
All other arguments are collected as a tab-separated string and placed
in an entry under the key "ARGS"
. All named parameters
are converted to lower case to preserve the case-insensitive semantics
of getParameter
.
args
- The array of strings passed to the applicationHashMap
containing the parameter bindings