home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / JAVA / NOTES / SOURCE / console.jav < prev    next >
Text File  |  1996-12-20  |  28KB  |  750 lines

  1.  
  2. /*   
  3.     A class for making "console windows", which can be used for doing basic
  4.     character-oriented input/output.  Methods are provided for reading and
  5.     writing the primitive data types plus Strings.
  6.     
  7.     Problems:  (1) This works with CodeWarrior on the Mac, but when
  8.                    I tried it on a Sun under AppletViewer, I found that
  9.                    the upperleft corner of the drawing area of a Frame
  10.                    is not (0,0)!  Looks like it's at the upper left of the
  11.                    window's border instead.  Is this device independence???
  12.  
  13.     I wrote this for use in an intro programming course as a kind of fudge,
  14.     because System.in doesn't work on the Mac.  (And because neither InputStream
  15.     nor any other standard input stream has any decent input methods anyway.)
  16.     
  17.     David Eck
  18.     Department of Mathematics and Computer Science
  19.     Hobart and William Smith Colleges
  20.     Geneva, NY 14456
  21.     eck@hws.edu
  22.     
  23.     June 30, 1996
  24.     
  25.     Modified August 27, 1996 to use an off-screeen canvas to hold a copy of 
  26.          the stuff that's on the screen.
  27.          
  28.     Modified September 14, 1996 to keep the console window open after
  29.          the program ends, until the user hits some key.
  30.  
  31. */
  32.  
  33.    
  34. import java.awt.*;
  35.  
  36. public class Console extends Frame {
  37.  
  38.    // ***************************** Constructors *******************************
  39.    
  40.    public Console() {   // default constructor just provides default window title and size
  41.       this("Java I/O Console",25,80);
  42.    }
  43.    
  44.    public Console(String title) {  // open window with specified title and default size
  45.       this(title,25,80);
  46.    }
  47.    
  48.    public Console(String title, int R, int C) {
  49.               // create and open console window with given title and size.
  50.               // R is the number of rows of text to be displayed in the window;
  51.               // C is the number of columns of text
  52.  
  53.       super(title);
  54.       
  55.       rows = R;
  56.       cols = C;
  57.  
  58.       setBackground(Color.white);
  59.       setForeground(Color.black);
  60.       font = new Font("Courier",Font.PLAIN,12);   // want monospaced font
  61.       setFont(font);
  62.       FontMetrics fm = getFontMetrics(font);
  63.  
  64.       lineHeight = fm.getHeight();   // initialize instance variables
  65.       baseOffset = fm.getAscent();
  66.       charWidth = fm.charWidth('W');
  67.       contents = new char[rows][cols];
  68.       lineLength = new int[rows];
  69.  
  70.       resize(2*border + cols * charWidth, border + rows * lineHeight);
  71.       setResizable(false);
  72.       
  73.       show();
  74.  
  75.       makeOSC();  // make an off-screen canvas for double buffering.
  76.                   //   (N.B.:  This wouldn't work if I did it before calling show() !!
  77.       
  78.    }
  79.    
  80.    public void close() {  // close the window; can't use the window after doing this!
  81.       putln();
  82.       put("   *** Console ready to close; Hit any key ***");
  83.       closing = true;
  84.    }
  85.    
  86.    public void clear() {  // clear the console window, return cursor to origin
  87.       clearWin();  // (private method, defined below)
  88.    }
  89.    
  90.    // *************************** I/O Methods *********************************
  91.    
  92.          // Methods for writing the primitive types, plus type String,
  93.          // to the console window, with no extra spaces.
  94.  
  95.    public void put(int x)     { put(x,0); }   // Note: also handles byte and short!
  96.    public void put(long x)    { put(x,0); }
  97.    public void put(float x)   { put(x,0); }
  98.    public void put(double x)  { put(x,0); }
  99.    public void put(char x)    { put(x,0); }
  100.    public void put(boolean x) { put(x,0); }
  101.    public void put(String x)  { put(x,0); }
  102.  
  103.  
  104.          // Methods for writing the primitive types, plus type String,
  105.          // to the console window,followed by a carriage return, with
  106.          // no extra spaces.
  107.  
  108.    public void putln(int x)      { put(x,0); newLine(); }  // Note: also handles byte and short!
  109.    public void putln(long x)     { put(x,0); newLine(); }
  110.    public void putln(float x)    { put(x,0); newLine(); }
  111.    public void putln(double x)   { put(x,0); newLine(); }
  112.    public void putln(char x)     { put(x,0); newLine(); }
  113.    public void putln(boolean x)  { put(x,0); newLine(); }
  114.    public void putln(String x)   { put(x,0); newLine(); }
  115.   
  116.  
  117.          // Methods for writing the primitive types, plus type String,
  118.          // to the console window, with a minimum field width of w,
  119.          // and followed by a carriage  return.
  120.          // If outut value is less than w characters, it is padded
  121.          // with extra spaces in front of the value.
  122.  
  123.    public void putln(int x, int w)     { put(x,w); newLine(); }   // Note: also handles byte and short!
  124.    public void putln(long x, int w)    { put(x,w); newLine(); }
  125.    public void putln(float x, int w)   { put(x,w); newLine(); }
  126.    public void putln(double x, int w)  { put(x,w); newLine(); }
  127.    public void putln(char x, int w)    { put(x,w); newLine(); }
  128.    public void putln(boolean x, int w) { put(x,w); newLine(); }
  129.    public void putln(String x, int w)  { put(x,w); newLine(); }
  130.  
  131.  
  132.           // Method for outputting a carriage return
  133.  
  134.    public void putln() { newLine(); }
  135.    
  136.  
  137.          // Methods for writing the primitive types, plus type String,
  138.          // to the console window, with minimum field width w.
  139.    
  140.    public void put(int x, int w)     { dumpString(String.valueOf(x), w); }   // Note: also handles byte and short!
  141.    public void put(long x, int w)    { dumpString(String.valueOf(x), w); }
  142.    public void put(float x, int w)   { dumpString(String.valueOf(x), w); }
  143.    public void put(double x, int w)  { dumpString(String.valueOf(x), w); }
  144.    public void put(char x, int w)    { dumpString(String.valueOf(x), w); }
  145.    public void put(boolean x, int w) { dumpString(String.valueOf(x), w); }
  146.    public void put(String x, int w)  { dumpString(x, w); }
  147.    
  148.    
  149.          // Methods for reading in the primitive types, plus "words" and "lines".
  150.          // The "getln..." methods discard any extra input, up to the next carriage return.
  151.          // A "word" read by getlnWord() is any sequence of non-blank characters.
  152.          // A "line" read by getlnString() or getln() is everything up to next CR;
  153.          //    the carriage return is not part of the returned value, but it is
  154.          //    read and discarded.
  155.          // Note that all input methods except those for char's and lines will
  156.          // skip past any blanks and carriage returns to find a non-blank value.
  157.          // getln() can return an empty string; getChar() and getlnChar() can 
  158.          //    return a space or a linefeed ('\n') character.
  159.          // Acceptable boolean values are the "words": true, false, t, f, yes,
  160.          //    no, y, n, 0, or 1;  uppercase letters are OK.
  161.          // None of these can produce an error; if an error is found in input,
  162.          //    the user is forced to re-enter.
  163.    
  164.    public byte getlnByte()       { byte x=getByte();       emptyBuffer();  return x; }
  165.    public short getlnShort()     { short x=getShort();     emptyBuffer();  return x; }
  166.    public int getlnInt()         { int x=getInt();         emptyBuffer();  return x; }
  167.    public long getlnLong()       { long x=getLong();       emptyBuffer();  return x; }
  168.    public float getlnFloat()     { float x=getFloat();     emptyBuffer();  return x; }
  169.    public double getlnDouble()   { double x=getDouble();   emptyBuffer();  return x; }
  170.    public char getlnChar()       { char x=getChar();       emptyBuffer();  return x; }
  171.    public boolean getlnBoolean() { boolean x=getBoolean(); emptyBuffer();  return x; }
  172.    public String getlnWord()     { String x=getWord();     emptyBuffer();  return x; }
  173.    public String getlnString()   { return getln(); }  // same as getln()
  174.    public String getln() {
  175.       StringBuffer s = new StringBuffer(100);
  176.       char ch = readChar();
  177.       while (ch != '\n') {
  178.          s.append(ch);
  179.          ch = readChar();
  180.       }
  181.       return s.toString();
  182.    }
  183.    
  184.    
  185.    public byte getByte()   { return (byte)readInteger(-128L,127L); }
  186.    public short getShort() { return (short)readInteger(-32768L,32767L); }   
  187.    public int getInt()     { return (int)readInteger((long)Integer.MIN_VALUE, (long)Integer.MAX_VALUE); }
  188.    public long getLong()   { return readI