home *** CD-ROM | disk | FTP | other *** search
/ Inside Dreamweaver 4 / IDW4.ISO / pc / Projects / ch20 / java / quote / src / JavaQuote.java < prev    next >
Encoding:
Java Source  |  1998-08-28  |  4.0 KB  |  168 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.applet.*;
  29. import java.net.*;
  30.  
  31. public class JavaQuote extends Applet implements Runnable
  32. {
  33.    private Dimension size;
  34.    private Image offImage;
  35.    private Graphics offG;
  36.    private ParamParser param;
  37.    private Color bgcolor;
  38.    private Thread animate;
  39.    private long delay;
  40.    private String link;
  41.    private String target;
  42.    private TextScript script;
  43.  
  44.    public void init()
  45.    {
  46.       size = size();
  47.  
  48.       param = new ParamParser(this);
  49.       bgcolor = param.parseColor("bgcolor", Color.white);
  50.       setBackground(bgcolor);
  51.  
  52.       delay = param.parseInt("delay", 100);
  53.       link = param.parseString("link", null);
  54.       target = param.parseString("target", "_self");
  55.  
  56.       // Create the offImage for double-buffering.
  57.  
  58.       offImage = createImage(size.width, size.height);
  59.       offG = offImage.getGraphics();
  60.  
  61.       // Create and initialize the script.
  62.  
  63.       script = new TextScript(offG, param, size);
  64.       script.init();
  65.    }
  66.  
  67.    public void start()
  68.    {
  69.       if ((animate == null) || (!animate.isAlive()))
  70.       {
  71.          animate = new Thread(this);
  72.       }
  73.  
  74.       animate.start();
  75.       script.start();
  76.    }
  77.  
  78.    public void run()
  79.    {
  80.       while (Thread.currentThread() == animate)
  81.       {
  82.          try
  83.          {
  84.             script.update();
  85.             repaint();
  86.  
  87.             Thread.sleep(delay);
  88.          }
  89.  
  90.          catch(InterruptedException e)
  91.          {
  92.             e.printStackTrace();
  93.          }
  94.       }
  95.    }
  96.  
  97.    public void stop()
  98.    {
  99.       if ((animate != null) && (animate.isAlive()))
  100.       {
  101.          script.stop();
  102.          animate.stop();
  103.       }
  104.    }
  105.  
  106.    public void destroy()
  107.    {
  108.       animate = null;
  109.    }
  110.  
  111.    public void update(Graphics g)
  112.    {
  113.       script.paint(offG);
  114.       paint(g);
  115.    }
  116.  
  117.    public void paint(Graphics g)
  118.    {
  119.       g.drawImage(offImage, 0, 0, this);
  120.    }
  121.  
  122.    public boolean mouseDown(Event evt, int x, int y)
  123.    {
  124.       if (link != null)
  125.       {
  126.          try
  127.          {
  128.             URL url = new URL(getDocumentBase(), link);
  129.             getAppletContext().showDocument(url, target);
  130.  
  131.             if (target.equals("_self"))
  132.             {
  133.                stop();
  134.             }
  135.          }
  136.  
  137.          catch(MalformedURLException e)
  138.          {
  139.             e.printStackTrace();
  140.          }
  141.       }
  142.  
  143.       return(true);
  144.    }
  145.  
  146.    public boolean mouseEnter(Event evt, int x, int y)
  147.    {
  148.       if (link != null)
  149.       {
  150.          showStatus(link);
  151.       }
  152.  
  153.       return(true);
  154.    }
  155.  
  156.    public boolean mouseExit(Event evt, int x, int y)
  157.    {
  158.       if (link != null)
  159.       {
  160.          showStatus("");
  161.       }
  162.  
  163.       return(true);
  164.    }
  165. }
  166.