home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
BURKS
/
LANGUAGE
/
JAVA
/
NOTES
/
SOURCE
/
console.jav
< prev
next >
Wrap
Text File
|
1996-12-20
|
28KB
|
750 lines
/*
A class for making "console windows", which can be used for doing basic
character-oriented input/output. Methods are provided for reading and
writing the primitive data types plus Strings.
Problems: (1) This works with CodeWarrior on the Mac, but when
I tried it on a Sun under AppletViewer, I found that
the upperleft corner of the drawing area of a Frame
is not (0,0)! Looks like it's at the upper left of the
window's border instead. Is this device independence???
I wrote this for use in an intro programming course as a kind of fudge,
because System.in doesn't work on the Mac. (And because neither InputStream
nor any other standard input stream has any decent input methods anyway.)
David Eck
Department of Mathematics and Computer Science
Hobart and William Smith Colleges
Geneva, NY 14456
eck@hws.edu
June 30, 1996
Modified August 27, 1996 to use an off-screeen canvas to hold a copy of
the stuff that's on the screen.
Modified September 14, 1996 to keep the console window open after
the program ends, until the user hits some key.
*/
import java.awt.*;
public class Console extends Frame {
// ***************************** Constructors *******************************
public Console() { // default constructor just provides default window title and size
this("Java I/O Console",25,80);
}
public Console(String title) { // open window with specified title and default size
this(title,25,80);
}
public Console(String title, int R, int C) {
// create and open console window with given title and size.
// R is the number of rows of text to be displayed in the window;
// C is the number of columns of text
super(title);
rows = R;
cols = C;
setBackground(Color.white);
setForeground(Color.black);
font = new Font("Courier",Font.PLAIN,12); // want monospaced font
setFont(font);
FontMetrics fm = getFontMetrics(font);
lineHeight = fm.getHeight(); // initialize instance variables
baseOffset = fm.getAscent();
charWidth = fm.charWidth('W');
contents = new char[rows][cols];
lineLength = new int[rows];
resize(2*border + cols * charWidth, border + rows * lineHeight);
setResizable(false);
show();
makeOSC(); // make an off-screen canvas for double buffering.
// (N.B.: This wouldn't work if I did it before calling show() !!
}
public void close() { // close the window; can't use the window after doing this!
putln();
put(" *** Console ready to close; Hit any key ***");
closing = true;
}
public void clear() { // clear the console window, return cursor to origin
clearWin(); // (private method, defined below)
}
// *************************** I/O Methods *********************************
// Methods for writing the primitive types, plus type String,
// to the console window, with no extra spaces.
public void put(int x) { put(x,0); } // Note: also handles byte and short!
public void put(long x) { put(x,0); }
public void put(float x) { put(x,0); }
public void put(double x) { put(x,0); }
public void put(char x) { put(x,0); }
public void put(boolean x) { put(x,0); }
public void put(String x) { put(x,0); }
// Methods for writing the primitive types, plus type String,
// to the console window,followed by a carriage return, with
// no extra spaces.
public void putln(int x) { put(x,0); newLine(); } // Note: also handles byte and short!
public void putln(long x) { put(x,0); newLine(); }
public void putln(float x) { put(x,0); newLine(); }
public void putln(double x) { put(x,0); newLine(); }
public void putln(char x) { put(x,0); newLine(); }
public void putln(boolean x) { put(x,0); newLine(); }
public void putln(String x) { put(x,0); newLine(); }
// Methods for writing the primitive types, plus type String,
// to the console window, with a minimum field width of w,
// and followed by a carriage return.
// If outut value is less than w characters, it is padded
// with extra spaces in front of the value.
public void putln(int x, int w) { put(x,w); newLine(); } // Note: also handles byte and short!
public void putln(long x, int w) { put(x,w); newLine(); }
public void putln(float x, int w) { put(x,w); newLine(); }
public void putln(double x, int w) { put(x,w); newLine(); }
public void putln(char x, int w) { put(x,w); newLine(); }
public void putln(boolean x, int w) { put(x,w); newLine(); }
public void putln(String x, int w) { put(x,w); newLine(); }
// Method for outputting a carriage return
public void putln() { newLine(); }
// Methods for writing the primitive types, plus type String,
// to the console window, with minimum field width w.
public void put(int x, int w) { dumpString(String.valueOf(x), w); } // Note: also handles byte and short!
public void put(long x, int w) { dumpString(String.valueOf(x), w); }
public void put(float x, int w) { dumpString(String.valueOf(x), w); }
public void put(double x, int w) { dumpString(String.valueOf(x), w); }
public void put(char x, int w) { dumpString(String.valueOf(x), w); }
public void put(boolean x, int w) { dumpString(String.valueOf(x), w); }
public void put(String x, int w) { dumpString(x, w); }
// Methods for reading in the primitive types, plus "words" and "lines".
// The "getln..." methods discard any extra input, up to the next carriage return.
// A "word" read by getlnWord() is any sequence of non-blank characters.
// A "line" read by getlnString() or getln() is everything up to next CR;
// the carriage return is not part of the returned value, but it is
// read and discarded.
// Note that all input methods except those for char's and lines will
// skip past any blanks and carriage returns to find a non-blank value.
// getln() can return an empty string; getChar() and getlnChar() can
// return a space or a linefeed ('\n') character.
// Acceptable boolean values are the "words": true, false, t, f, yes,
// no, y, n, 0, or 1; uppercase letters are OK.
// None of these can produce an error; if an error is found in input,
// the user is forced to re-enter.
public byte getlnByte() { byte x=getByte(); emptyBuffer(); return x; }
public short getlnShort() { short x=getShort(); emptyBuffer(); return x; }
public int getlnInt() { int x=getInt(); emptyBuffer(); return x; }
public long getlnLong() { long x=getLong(); emptyBuffer(); return x; }
public float getlnFloat() { float x=getFloat(); emptyBuffer(); return x; }
public double getlnDouble() { double x=getDouble(); emptyBuffer(); return x; }
public char getlnChar() { char x=getChar(); emptyBuffer(); return x; }
public boolean getlnBoolean() { boolean x=getBoolean(); emptyBuffer(); return x; }
public String getlnWord() { String x=getWord(); emptyBuffer(); return x; }
public String getlnString() { return getln(); } // same as getln()
public String getln() {
StringBuffer s = new StringBuffer(100);
char ch = readChar();
while (ch != '\n') {
s.append(ch);
ch = readChar();
}
return s.toString();
}
public byte getByte() { return (byte)readInteger(-128L,127L); }
public short getShort() { return (short)readInteger(-32768L,32767L); }
public int getInt() { return (int)readInteger((long)Integer.MIN_VALUE, (long)Integer.MAX_VALUE); }
public long getLong() { return readI