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

  1.  
  2. /*  
  3.     A class for making "consoles", 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.     The console is implemented as a canvas (that can be used in an
  8.     applet, for example).  Note that the method setSize() MUST be
  9.     called before the console is used.
  10.     
  11.     Problem:  Scrolling won't work properly if part of the console is
  12.               obscured (because Graphics.copyArea() is used, and it
  13.               doesn't work if part of the area is obscured).  So you
  14.               can really only use one window.
  15.  
  16.     I wrote this for use in an intro programming course as a kind of fudge,
  17.     because System.in doesn't work on the Mac.  (And because neither InputStream
  18.     nor any other standard input stream has any decent input methods anyway.)
  19.     This class is mostly identical to another class that descends from Frame
  20.     and is meant to be used as a window in an independent application.
  21.     
  22.     David Eck
  23.     Department of Mathematics and Computer Science
  24.     Hobart and William Smith Colleges
  25.     Geneva, NY 14456
  26.     eck@hws.edu
  27.     
  28.     June 30, 1996
  29.  
  30. */
  31.  
  32.    
  33. import java.awt.*;
  34.  
  35. public class ConsoleCanvas extends Canvas {
  36.  
  37.    // ***************************** constructors, setup, etc. *******************************
  38.    
  39.    public ConsoleCanvas() {
  40.       setBackground(Color.white);
  41.       setForeground(Color.black);
  42.    }
  43.    
  44.    public void close() {  // no effect; for compatibility with FrameConsole
  45.    }
  46.    
  47.    public void clear() {  // clear the console window, return cursor to origin
  48.       clearWin();  // (private method, defined below)
  49.    }
  50.    
  51.    synchronized public void clearBuffers() {  // to be used when "program" is aborted
  52.       bufferCount = 0;
  53.       typeaheadCount = 0;
  54.    }
  55.    
  56.    public void setSize() {  // MUST be called after size is established, before console is used
  57.                             // for example, in the start method of an applet.
  58.       Font oldfont = getFont();
  59.       int pointsize = 10;  // want to use size of default font, unless it doesn't seem to make sense
  60.       if (oldfont != null)
  61.          if (oldfont.getSize() > 24)
  62.             pointsize = 20;
  63.          else if (oldfont.getSize() > 10)
  64.             pointsize = oldfont.getSize();
  65.       Font font = new Font("Courier",Font.PLAIN,pointsize);   // want monospaced font
  66.       setFont(font);
  67.       FontMetrics fm = getFontMetrics(font);
  68.  
  69.       lineHeight = fm.getHeight();   // initialize instance variables
  70.       baseOffset = fm.getAscent();
  71.       charWidth = fm.charWidth('W');
  72.       cols = (size().width - 2*border) / charWidth;
  73.       rows = (size().height - 2*border - 2) / lineHeight;
  74.       contents = new char[rows][cols];
  75.       lineLength = new int[rows];
  76.       for (int i=0; i<rows; i++)
  77.          lineLength[i] = 0;
  78.    }
  79.    
  80.    // *************************** I/O Methods *********************************
  81.    
  82.          // Methods for writing the primitive types, plus type String,
  83.          // to the console window, with no extra spaces.
  84.  
  85.    public void put(int x)     { put(x,0); }   // Note: also handles byte and short!
  86.    public void put(long x)    { put(x,0); }
  87.    public void put(float x)   { put(x,0); }
  88.    public void put(double x)  { put(x,0); }
  89.    public void put(char x)    { put(x,0); }
  90.    public void put(boolean x) { put(x,0); }
  91.    public void put(String x)  { put(x,0); }
  92.  
  93.  
  94.          // Methods for writing the primitive types, plus type String,
  95.          // to the console window,followed by a carriage return, with
  96.          // no extra spaces.
  97.  
  98.    public void putln(int x)      { put(x,0); newLine(); }  // Note: also handles byte and short!
  99.    public void putln(long x)     { put(x,0); newLine(); }
  100.    public void putln(float x)    { put(x,0); newLine(); }
  101.    public void putln(double x)   { put(x,0); newLine(); }
  102.    public void putln(char x)     { put(x,0); newLine(); }
  103.    public void putln(boolean x)  { put(x,0); newLine(); }
  104.    public void putln(String x)   { put(x,0); newLine(); }
  105.   
  106.  
  107.          // Methods for writing the primitive types, plus type String,
  108.          // to the console window, with a minimum field width of w,
  109.          // and followed by a carriage  return.
  110.          // If outut value is less than w characters, it is padded
  111.          // with extra spaces in front of the value.
  112.  
  113.    public void putln(int x, int w)     { put(x,w); newLine(); }   // Note: also handles byte and short!
  114.    public void putln(long x, int w)    { put(x,w); newLine(); }
  115.    public void putln(float x, int w)   { put(x,w); newLine(); }
  116.    public void putln(double x, int w)  { put(x,w); newLine(); }
  117.    public void putln(char x, int w)    { put(x,w); newLine(); }
  118.    public void putln(boolean x, int w) { put(x,w); newLine(); }
  119.    public void putln(String x, int w)  { put(x,w); newLine(); }
  120.  
  121.  
  122.           // Method for outputting a carriage return
  123.  
  124.    public void putln() { newLine(); }
  125.    
  126.  
  127.          // Methods for writing the primitive types, plus type String,
  128.          // to the console window, with minimum field width w.
  129.    
  130.    public void put(int x, int w)     { dumpString(String.valueOf(x), w); }   // Note: also handles byte and short!
  131.    public void put(long x, int w)    { dumpString(String.valueOf(x), w); }
  132.    public void put(float x, int w)   { dumpString(String.valueOf(x), w); }
  133.    public void put(double x, int w)  { dumpString(String.valueOf(x), w); }
  134.    public void put(char x, int w)    { dumpString(String.valueOf(x), w); }
  135.    public void put(boolean x, int w) { dumpString(String.valueOf(x), w); }
  136.    public void put(String x, int w)  { dumpString(x, w); }
  137.    
  138.    
  139.          // Methods for reading in the primitive types, plus "words" and "lines".
  140.          // The "getln..." methods discard any extra input, up to the next carriage return.
  141.          // A "word" read by getlnWord() is any sequence of non-blank characters.
  142.          // A "line" read by getlnString() or getln() is everything up to next CR;
  143.          //    the carriage return is not part of the returned value, but it is
  144.          //    read and discarded.
  145.          // Note that all input methods except those for char's and lines will
  146.          // skip past any blanks and carriage returns to find a non-blank value.
  147.          // getln() can return an empty string; getChar() and getlnChar() can 
  148.          //    return a space or a linefeed ('\n') character.
  149.          // Acceptable boolean values are the "words": true, false, t, f, yes,
  150.          //    no, y, n, 0, or 1;  uppercase letters are OK.
  151.          // None of these can produce an error; if an error is found in input,
  152.          //    the user is forced to re-enter.
  153.    
  154.    public byte getlnByte()       { byte x=getByte();       emptyBuffer();  return x; }
  155.    public short getlnShort()     { short x=getShort();     emptyBuffer();  return x; }
  156.    public int getlnInt()         { int x=getInt();         emptyBuffer();  return x; }
  157.    public long getlnLong()       { long x=getLong();       emptyBuffer();  return x; }
  158.    public float getlnFloat()     { float x=getFloat();     emptyBuffer();  return x; }
  159.    public double getlnDouble()   { double x=getDouble();   emptyBuffer();  return x; }
  160.    public char getlnChar()       { char x=getChar();       emptyBuffer();  return x; }
  161.    public boolean getlnBoolean() { boolean x=getBoolean(); emptyBuffer();  return x; }
  162.    public String getlnWord()     { String x=getWord();     emptyBuffer();  return x; }
  163.    public String getlnString()   { return getln(); }  // same as getln()
  164.    public String getln() {
  165.       StringBuffer s = new StringBuffer(100);
  166.       char ch = readChar();
  167.       while (ch != '\n') {
  168.          s.append(ch);
  169.          ch = readChar();
  170.       }
  171.       return s.toString();
  172.    }
  173.    
  174.    
  175.    public byte getByte()   { return (byte)readInteger(-128L,127L); }
  176.    public short getShort() { return (short)readInteger(-32768L,32767L); }   
  177.    public int getInt()     { return (int)readInteger((long)Integer.MIN_VALUE, (long)Integer.MAX_VALUE); }
  178.    public long getLong()   { return readInteger(Long.MIN_VALUE, Long.MAX_VALUE); }
  179.    
  180.    public char getChar()   { return readChar(); }
  181.    
  182.    public float getFloat() {  // can return positive