home *** CD-ROM | disk | FTP | other *** search
/ Internet 1996 World Exposition / park.org.s3.amazonaws.com.7z / park.org.s3.amazonaws.com / Java / LEDSign / LED / Letters.java < prev    next >
Encoding:
Java Source  |  2017-09-21  |  6.1 KB  |  240 lines

  1. /////////////////////////////////////////////////////////////////////
  2. //  Letters.java   -- LED Sign V2.5
  3. //  
  4. //  This class parses the font file and stores
  5. //  each letter in an array of boolean (on/off).
  6. //  It takes care of all the storage and
  7. //  retrieval of letters data structure.
  8. //
  9. //  Revisions:
  10. //     V2.5: Fixed all known bugs in previous versions!  Added
  11. //           the new feature of ledsize, which allows the user
  12. //           to specify in pixels how big the LED's (1-4).
  13. //           Thanks to Robert B. Denny (rdenny@dc3.com) for
  14. //           code and input!
  15. //           Modified Dec 20-26, 1995
  16. //
  17. //     V2.0beta: Modified V1.0 to comply with Pre-Beta java.
  18. //               A problem with delay causes a jerky display.
  19. //               Modified Oct 20 - 29, 1995
  20. //
  21. //     V1.0: Written July 13 - 14, 1995
  22. //
  23. //  By Darrick Brown
  24. //     dbrown@cs.hope.edu
  25. //     http://www.cs.hope.edu/~dbrown/
  26. //
  27. //  ⌐ Copyright 1995
  28. /////////////////////////////////////////////////////////////////////
  29.  
  30. /*package LED;*/
  31.  
  32. import java.awt.*;
  33. import java.io.*;
  34. import java.net.*;
  35.  
  36. //////////////////////////////////////////////////////////////////
  37. // The Letters Class
  38. //////////////////////////////////////////////////////////////////
  39. public class Letters
  40. {
  41.    int HEIGHT,TOTAL;
  42.    String let;
  43.    URL url;
  44.    InputStream file;
  45.    DataInputStream dis;
  46.    int w,h,num,place,len,space,swidth;
  47.    Index index[];
  48.    
  49.    
  50.    //////////////////////////////////////////////////////////////////
  51.    // The class constructor
  52.    public Letters(URL url, String URLfile, int width)
  53.    {
  54.       try
  55.       {
  56.          // Set some initial variables
  57.          file = (new URL(url,URLfile)).openStream();
  58.          dis = new DataInputStream(file);
  59.          swidth = width;
  60.          initLetters();  
  61.       }
  62.       catch (IOException e)
  63.       {
  64.          e.printStackTrace();
  65.       }
  66.    }
  67.  
  68.    //////////////////////////////////////////////////////////////////
  69.    public int height()
  70.    {
  71.       return HEIGHT;
  72.    }
  73.  
  74.    //////////////////////////////////////////////////////////////////
  75.    // Read in the letters
  76.    void initLetters()
  77.    {
  78.       int a,b,c;
  79.       byte ch;     // the character of the letter
  80.       int i,j,k;
  81.       String s;    // A line in the font file
  82.       boolean done;
  83.       int width;
  84.  
  85.       // Just to make the compiler shut up about
  86.       // these "may not be initialized".
  87.       w = 5;
  88.       h = 5;
  89.       num = 100;
  90.  
  91.       try
  92.       {
  93.          // find the height
  94.          done = false;
  95.          while(!done)
  96.          {
  97.             s = dis.readLine();
  98.             if(!s.startsWith("!!")) // If is not a comment line
  99.             {
  100.                h = (new Integer(s)).intValue();
  101.                HEIGHT = h;
  102.                done = true;
  103.             }
  104.          }
  105.  
  106.          // find the width
  107.          done = false;
  108.          while(!done)
  109.          {
  110.             s = dis.readLine();
  111.             if(!s.startsWith("!!")) // If is not a comment line
  112.             {
  113.                w = (new Integer(s)).intValue();
  114.                done = true;
  115.             }
  116.          }
  117.  
  118.          // Find the number of characters
  119.          done = false;
  120.          while(!done)
  121.          {
  122.             s = dis.readLine();
  123.             if(!s.startsWith("!!")) // If is not a comment line
  124.             {
  125.                num = (new Integer(s)).intValue();
  126.                done = true;
  127.             }
  128.          }
  129.  
  130.          // The "num+1" allocates the extra array position for " " (space)
  131.          index = new Index[num+1];
  132.  
  133.          // Ok we gots the data, lets read in the characters!
  134.          for(i=0;i<num;i++)
  135.          {
  136.             // to make the compiler shut up about how
  137.             // these "may not have been initialized"
  138.             ch = 2;
  139.             width = 10;
  140.  
  141.             //read the header for the letter
  142.             done = false;
  143.             while(!done)
  144.             {
  145.                s = dis.readLine();
  146.                if(!s.startsWith("!!")) // If is not a comment line
  147.                {
  148.                   ch = (byte)s.charAt(0);
  149.                   done = true;
  150.                }
  151.             }
  152.             done = false;
  153.             while(!done)
  154.             {
  155.                s = dis.readLine();
  156.                if(!s.startsWith("!!")) // If is not a comment line
  157.                {
  158.                   width = (new Integer(s)).intValue();
  159.                   done = true;
  160.                }
  161.             }
  162.  
  163.             // initialize the struct
  164.             index[i] = new Index(ch,width,h);
  165.  
  166.             // read in the character
  167.             for(j=0;j<h;j++)
  168.             {
  169.                done = false;
  170.                s = "";
  171.                while(!done)
  172.                {
  173.                   s = dis.readLine();
  174.  
  175.                   if(s.length() > 0)
  176.                   {
  177.                      if(!s.startsWith("!!")) // If is not a comment line
  178.                      {
  179.                         done = true;
  180.                      }
  181.                   }
  182.                   else
  183.                   {
  184.                      s = " ";
  185.                      done = true;
  186.                   }
  187.                }
  188.  
  189.                for(k=0;k<index[i].width;k++)
  190.                {
  191.                   if(k>=s.length())
  192.                   {
  193.                      index[i].letter[k][j] = false;
  194.                   }
  195.                   else
  196.                   {
  197.                      if(s.charAt(k) == '#')
  198.                         index[i].letter[k][j] = true;
  199.                      else
  200.                         index[i].letter[k][j] = false;
  201.                   }
  202.                }
  203.             }
  204.          } // end reading in the letters
  205.  
  206.          index[num] = new Index((byte)32,swidth,h);
  207.  
  208.          // close the datastreams
  209.          file.close();
  210.          dis.close();
  211.       }
  212.       catch (IOException e)
  213.       {
  214.          e.printStackTrace();
  215.       }
  216.  
  217.    } // end of InitLetters()
  218.  
  219.    //////////////////////////////////////////////////////////////////
  220.    // find the LED letter and return it
  221.    public Index getLetter(char c)
  222.    {
  223.       int j;
  224.  
  225.       if(c == (char)(32))
  226.       {
  227.          j = num; // I know where this one is!
  228.       }
  229.       else
  230.       {
  231.          // look for it
  232.          j = 0;
  233.          while(c != index[j].ch && j < num)
  234.             j++;
  235.       }
  236.  
  237.       return index[j];
  238.    } // End getLetter()
  239. } // End Letters Class
  240.