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