home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 12,000 to 12,999 / 12000.zip / AOLDLs / Online-Tools / Java-Applets / JAVAAPPS.lzh / JAVAAPPS / ANIMATEB / ANIMATEB.EXE / animateButton.java < prev    next >
Encoding:
Java Source  |  1996-03-13  |  3.5 KB  |  175 lines

  1.  
  2.  
  3.  
  4.  
  5. import java.applet.*;
  6.  
  7. import java.awt.*;
  8.  
  9. import java.net.*;
  10.  
  11.  
  12.  
  13. class Animate extends Thread
  14.  
  15. {
  16.  
  17.   animateButton b;
  18.  
  19.  
  20.  
  21.   public Animate(animateButton who)
  22.  
  23.   {
  24.  
  25.     b = who;
  26.  
  27.   }
  28.  
  29.  
  30.  
  31.   public void run()
  32.  
  33.   {
  34.  
  35.     while(b.running)
  36.  
  37.     {
  38.  
  39.     b.advanceFrame();
  40.  
  41.       b.repaint();
  42.  
  43.     try
  44.  
  45.     {
  46.  
  47.       sleep(b.sleeptime);
  48.  
  49.     }
  50.  
  51.     catch(Exception e){}
  52.  
  53.     }
  54.  
  55.   }
  56.  
  57. }
  58.  
  59.  
  60.  
  61. public class animateButton extends Applet
  62.  
  63. {
  64.  
  65.   int nframe;
  66.  
  67.   Image image[];
  68.  
  69.   AudioClip audio;
  70.  
  71.   URL url;
  72.  
  73.   String target;
  74.  
  75.   int sleeptime;
  76.  
  77.   MediaTracker tracker;
  78.  
  79.   Animate animate;
  80.  
  81.   int frame;
  82.  
  83.   boolean running;
  84.  
  85.  
  86.  
  87.   private Image offScreenImage;
  88.  
  89.   private Dimension offScreenSize;
  90.  
  91.   private Graphics offScreenGraphics;
  92.  
  93.  
  94.  
  95.   public void advanceFrame()
  96.  
  97.   {
  98.  
  99.     frame = (frame + 1) % nframe;
  100.  
  101.   }
  102.  
  103.  
  104.  
  105.   public void init()
  106.  
  107.   {
  108.  
  109.     String parameter;
  110.  
  111.  
  112.  
  113.     // init number of frames
  114.  
  115.     parameter = getParameter("nframe");
  116.  
  117.     if (parameter == null)
  118.  
  119.       System.out.println("Error: invalid parameter: nframe");
  120.  
  121.     else
  122.  
  123.       nframe = Integer.parseInt(parameter);
  124.  
  125.  
  126.  
  127.     // init images
  128.  
  129.     image = new Image[nframe];
  130.  
  131.     tracker = new MediaTracker(this);
  132.  
  133.     for (int i = 0; i < nframe; i++)
  134.  
  135.     {
  136.  
  137.       parameter = getParameter("image"+i);
  138.  
  139.       if (parameter == null)
  140.  
  141.         System.out.println("Error: invalid parameter: image"+i);
  142.  
  143.       else
  144.  
  145.       {
  146.  
  147.         image[i] = getImage(getDocumentBase(), parameter);
  148.  
  149.         tracker.addImage(image[i], i);
  150.  
  151.       }
  152.  
  153.     }
  154.  
  155.     try
  156.  
  157.     {
  158.  
  159.       tracker.waitForAll();
  160.  
  161.     }
  162.  
  163.     catch (InterruptedException e)
  164.  
  165.     {
  166.  
  167.       System.out.println("Error waiting for image to load.");
  168.  
  169.     }
  170.  
  171.  
  172.  
  173.     // init audio
  174.  
  175.     parameter = getParameter("audio");
  176.  
  177.     if (parameter != null)
  178.  
  179.       audio = getAudioClip(getDocumentBase(), parameter);
  180.  
  181.  
  182.  
  183.     // init url
  184.  
  185.     parameter = getParameter("url");
  186.  
  187.     if (parameter != null)
  188.  
  189.     {
  190.  
  191.       try
  192.  
  193.       {
  194.  
  195.         url= new URL(parameter);
  196.  
  197.       }
  198.  
  199.       catch(MalformedURLException mal)
  200.  
  201.       {
  202.  
  203.         System.out.println("Error locating URL address.");
  204.  
  205.       }
  206.  
  207.     }
  208.  
  209.  
  210.  
  211.     // init target window
  212.  
  213.     target = getParameter("target");
  214.  
  215.  
  216.  
  217.     // init sleep time
  218.  
  219.     parameter = getParameter("sleeptime");
  220.  
  221.     if (parameter == null)
  222.  
  223.       sleeptime = 1000;
  224.  
  225.     else
  226.  
  227.       sleeptime = Integer.parseInt(parameter);      
  228.  
  229.   }
  230.  
  231.  
  232.  
  233.   public void paint (Graphics g)
  234.  
  235.   {
  236.  
  237.     g.drawImage(image[frame], 0, 0, null);    
  238.  
  239.   }
  240.  
  241.  
  242.  
  243.   public final synchronized void update (Graphics g)
  244.  
  245.   {
  246.  
  247.     Dimension d = size();
  248.  
  249.     if((offScreenImage == null) || (d.width != offScreenSize.width) ||  (d.height != offScreenSize.height))
  250.  
  251.     {
  252.  
  253.       offScreenImage = createImage(d.width, d.height);
  254.  
  255.       offScreenSize = d;
  256.  
  257.       offScreenGraphics = offScreenImage.getGraphics();
  258.  
  259.     }
  260.  
  261.     offScreenGraphics.setColor(getBackground());
  262.  
  263.     offScreenGraphics.fillRect(0, 0, d.width, d.height);
  264.  
  265.     paint(offScreenGraphics);
  266.  
  267.     g.drawImage(offScreenImage, 0, 0, null);
  268.  
  269.   }
  270.  
  271.  
  272.  
  273.   public void stop()
  274.  
  275.   {
  276.  
  277.     running = false;
  278.  
  279.     destroy();
  280.  
  281.   }
  282.  
  283.  
  284.  
  285.   public boolean mouseDown(Event evt, int x, int y)
  286.  
  287.   {
  288.  
  289.     if (audio != null)
  290.  
  291.           audio.play();
  292.  
  293.     return true;
  294.  
  295.   }
  296.  
  297.   public boolean mouseUp(Event evt, int x, int y)
  298.  
  299.   {
  300.  
  301.     if (url != null)
  302.  
  303.     {
  304.  
  305.       if (target != null)
  306.  
  307.         getAppletContext().showDocument(url, target);
  308.  
  309.       else
  310.  
  311.       getAppletContext().showDocument(url);
  312.  
  313.     }
  314.  
  315.     return true;
  316.  
  317.   }
  318.  
  319.  
  320.  
  321.   public boolean mouseEnter(Event evt, int x, int y)
  322.  
  323.   {
  324.  
  325.     running = true;
  326.  
  327.         animate = new Animate(this);
  328.  
  329.         animate.start();
  330.  
  331.     return true;
  332.  
  333.   }
  334.  
  335.  
  336.  
  337.   public boolean mouseExit(Event evt, int x, int y)
  338.  
  339.   {
  340.  
  341.     running = false;
  342.  
  343.     return true;
  344.  
  345.   }
  346.  
  347. }
  348.  
  349.