home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
BURKS
/
LANGUAGE
/
JAVA
/
NOTES
/
SOURCE
/
ccanvas.jav
< prev
next >
Wrap
Text File
|
1996-12-20
|
26KB
|
699 lines
/*
A class for making "consoles", which can be used for doing basic
character-oriented input/output. Methods are provided for reading and
writing the primitive data types plus Strings.
The console is implemented as a canvas (that can be used in an
applet, for example). Note that the method setSize() MUST be
called before the console is used.
Problem: Scrolling won't work properly if part of the console is
obscured (because Graphics.copyArea() is used, and it
doesn't work if part of the area is obscured). So you
can really only use one window.
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.)
This class is mostly identical to another class that descends from Frame
and is meant to be used as a window in an independent application.
David Eck
Department of Mathematics and Computer Science
Hobart and William Smith Colleges
Geneva, NY 14456
eck@hws.edu
June 30, 1996
*/
import java.awt.*;
public class ConsoleCanvas extends Canvas {
// ***************************** constructors, setup, etc. *******************************
public ConsoleCanvas() {
setBackground(Color.white);
setForeground(Color.black);
}
public void close() { // no effect; for compatibility with FrameConsole
}
public void clear() { // clear the console window, return cursor to origin
clearWin(); // (private method, defined below)
}
synchronized public void clearBuffers() { // to be used when "program" is aborted
bufferCount = 0;
typeaheadCount = 0;
}
public void setSize() { // MUST be called after size is established, before console is used
// for example, in the start method of an applet.
Font oldfont = getFont();
int pointsize = 10; // want to use size of default font, unless it doesn't seem to make sense
if (oldfont != null)
if (oldfont.getSize() > 24)
pointsize = 20;
else if (oldfont.getSize() > 10)
pointsize = oldfont.getSize();
Font font = new Font("Courier",Font.PLAIN,pointsize); // want monospaced font
setFont(font);
FontMetrics fm = getFontMetrics(font);
lineHeight = fm.getHeight(); // initialize instance variables
baseOffset = fm.getAscent();
charWidth = fm.charWidth('W');
cols = (size().width - 2*border) / charWidth;
rows = (size().height - 2*border - 2) / lineHeight;
contents = new char[rows][cols];
lineLength = new int[rows];
for (int i=0; i<rows; i++)
lineLength[i] = 0;
}
// *************************** 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 readInteger(Long.MIN_VALUE, Long.MAX_VALUE); }
public char getChar() { return readChar(); }
public float getFloat() { // can return positive