home *** CD-ROM | disk | FTP | other *** search
/ Inside Dreamweaver 4 / IDW4.ISO / pc / Projects / ch20 / java / quote / src / TextScript.java < prev   
Encoding:
Java Source  |  1998-08-28  |  5.1 KB  |  194 lines

  1. /*
  2. * Copyright (c) 1994-1998 Sun Microsystems, Inc. All Rights Reserved.
  3. *
  4. * Permission to use, copy, modify, and distribute this software and its
  5. * documentation for NON-COMMERCIAL or COMMERCIAL purposes and without fee is
  6. * hereby granted. Please refer to the file
  7. * http://java.sun.com/nav/business/trademark_guidelines.html for further
  8. * important copyright and trademark information and to
  9. * http://java.sun.com/nav/business/index.html for further important licensing
  10. * information for the Java (tm) Technology.
  11. * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  12. * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
  14. * NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
  15. * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
  16. * ITS DERIVATIVES.
  17. * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  18. * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE PERFORMANCE,
  19. * SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT NAVIGATION OR
  20. * COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE SUPPORT MACHINES, OR
  21. * WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE SOFTWARE COULD LEAD DIRECTLY TO
  22. * DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  23. * RISK ACTIVITIES"). SUN SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED
  24. * WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  25. */
  26.  
  27. import java.awt.*;
  28. import java.util.*;
  29.  
  30. public class TextScript extends Object
  31. {
  32.    private Graphics gc;
  33.    private ParamParser param;
  34.  
  35.    private FontMetrics fm;
  36.    private Font fontPlain;
  37.    private Font fontItalic;
  38.    private String fontName;
  39.    private int fontSize;
  40.  
  41.    private Dimension size;
  42.    private long last;
  43.  
  44.    private int number;
  45.    private int bWidth;
  46.    private int bHeight;
  47.    private int space;
  48.  
  49.    private Random random;
  50.    private boolean randomOrder = false;
  51.  
  52.    private TextQuote quote;
  53.    private Vector current;
  54.    private Vector discard;
  55.  
  56.    TextScript(Graphics gc, ParamParser param, Dimension size)
  57.    {
  58.       this.param = param;
  59.       this.size = size;
  60.       this.gc = gc;
  61.    }
  62.  
  63.    public void init()
  64.    {
  65.       bWidth = param.parseInt("bwidth", 10);
  66.       bHeight = param.parseInt("bheight", 10);
  67.       space = param.parseInt("space", 20);
  68.  
  69.       randomOrder = param.parseBoolean("random", false);
  70.       random = new Random(System.currentTimeMillis());
  71.  
  72.       current = new Vector(0, 1);
  73.       discard = new Vector(0, 1);
  74.  
  75.       fontSize = param.parseInt("fontsize", 10);
  76.       fontName = param.parseString("fontname", "TimesRoman");
  77.       fontPlain = new Font(fontName, Font.PLAIN, fontSize);
  78.       fontItalic = new Font(fontName, Font.ITALIC, fontSize);
  79.       fm = gc.getFontMetrics(fontPlain);
  80.  
  81.       number = param.parseInt("number", 0);
  82.  
  83.       // Read in the quotes.
  84.  
  85.       for (int i = 0; i < number; i++)
  86.       {
  87.          String[] s = param.parseStrings("quote" + i, "|", 5);
  88.  
  89.          if (s != null)
  90.          {
  91.             String[] q = param.parser.wordWrap(s[0], fm,
  92.                                                size.width - (2 * bWidth));
  93.  
  94.             Color fg = new Color(Integer.parseInt(s[2], 16));
  95.             Color bg = new Color(Integer.parseInt(s[3], 16));
  96.             int l = Integer.parseInt(s[4]);
  97.  
  98.             current.insertElementAt(new TextQuote(q, s[1], l, fg, bg), i);
  99.          }
  100.       }
  101.  
  102.       current.trimToSize();
  103.    }
  104.  
  105.    public void start()
  106.    {
  107.       quote = getQuote(null);
  108.       last = System.currentTimeMillis();
  109.    }
  110.  
  111.    private TextQuote getQuote(TextQuote old)
  112.    {
  113.       TextQuote rv = null;
  114.  
  115.       // Discard the old quote.
  116.  
  117.       if (old != null)
  118.       {
  119.          discard.addElement(old);
  120.       }
  121.  
  122.       // Return the next quote in the current list.
  123.  
  124.       if (!randomOrder)
  125.       {
  126.          rv = (TextQuote) (current.elementAt(0));
  127.          current.removeElementAt(0);
  128.       }
  129.  
  130.       // Return a quote randomly from the current list.
  131.  
  132.       else
  133.       {
  134.          int index = (int) (random.nextFloat() * (current.size() - 1));
  135.          rv = (TextQuote) (current.elementAt(index));
  136.          current.removeElementAt(index);
  137.       }
  138.  
  139.       // Set current to discard list, and allocate a new discard list.
  140.  
  141.       if (current.isEmpty())
  142.       {
  143.          current = discard;
  144.          discard = new Vector(0, 1);
  145.       }
  146.  
  147.       return rv;
  148.    }
  149.  
  150.    public void update()
  151.    {
  152.       long time = System.currentTimeMillis();
  153.  
  154.       if ((time - last) >= quote.length)
  155.       {
  156.          last = time;
  157.          quote = getQuote(quote);
  158.       }
  159.    }
  160.  
  161.    public void paint(Graphics g)
  162.    {
  163.       if (quote != null)
  164.       {
  165.          int x = bWidth;
  166.          int y = bHeight;
  167.  
  168.          int ascent = fm.getAscent();
  169.          int height = fm.getHeight();
  170.  
  171.          g.setColor(quote.bgColor);
  172.          g.fillRect(0, 0, size.width, size.height);
  173.  
  174.          g.setColor(quote.fgColor);
  175.          g.setFont(fontPlain);
  176.  
  177.          for (int i = 0; i < quote.text.length; i++)
  178.          {
  179.             g.drawString(quote.text[i], x, y + ascent);
  180.             y += height;
  181.          }
  182.  
  183.          g.setFont(fontItalic);
  184.          g.drawString(quote.company, x + 10, y + space);
  185.       }
  186.    }
  187.  
  188.    public void stop()
  189.    {
  190.    }
  191. }
  192.