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 / LIVINGLI / LIVINGLI.EXE / LivingLink.java < prev    next >
Encoding:
Java Source  |  1996-03-15  |  4.9 KB  |  237 lines

  1. import java.applet.*;
  2.  
  3. import java.awt.*;
  4.  
  5. import java.awt.image.*;
  6.  
  7. import java.lang.*;
  8.  
  9. import java.net.*;
  10.  
  11. import java.io.*;
  12.  
  13.  
  14.  
  15. public class LivingLink extends Applet implements Runnable{
  16.  
  17.  
  18.  
  19.   Image gif=null;
  20.  
  21.   int gifIndex;
  22.  
  23.   int gifX=0;
  24.  
  25.   int Height=100;
  26.  
  27.   int Width=100;
  28.  
  29.   int pauseLen=150;
  30.  
  31.   int gifWidth;
  32.  
  33.   String gifStrip=null;
  34.  
  35.   Image offScrImage;
  36.  
  37.   Graphics offScrGC=null;
  38.  
  39.   Thread kicker=null;
  40.  
  41.   boolean imageLoadError=false;
  42.  
  43.   boolean urlError=false;
  44.  
  45.   boolean debug=false;
  46.  
  47.   URL link=null;
  48.  
  49.   Color backGround;
  50.  
  51.  
  52.  
  53.   public void init() {
  54.  
  55.     String param;
  56.  
  57.     int r,g,b;
  58.  
  59.  
  60.  
  61.     param=getParameter("height");
  62.  
  63.     if (param != null)
  64.  
  65.       Height=Integer.valueOf(param).intValue();
  66.  
  67.  
  68.  
  69.     param=getParameter("width");
  70.  
  71.     if (param!=null)
  72.  
  73.       Width=Integer.valueOf(param).intValue();
  74.  
  75.  
  76.  
  77.     param=getParameter("DEBUG");
  78.  
  79.     if(param!=null)
  80.  
  81.       debug=true;
  82.  
  83.  
  84.  
  85.     param=getParameter("LINK");
  86.  
  87.     if(param!=null)
  88.  
  89.     {
  90.  
  91.       try
  92.  
  93.       {
  94.  
  95.         link=new URL(param);
  96.  
  97.       } catch(MalformedURLException e){urlError=true;}
  98.  
  99.  
  100.  
  101.     }
  102.  
  103.     
  104.  
  105.     if(debug)
  106.  
  107.     {
  108.  
  109.       if(urlError)
  110.  
  111.         System.out.println("URL Error");
  112.  
  113.       else
  114.  
  115.         System.out.println("No URL Error");
  116.  
  117.     }
  118.  
  119.  
  120.  
  121.     param=getParameter("GIFSTRIP");
  122.  
  123.     if(param!=null)
  124.  
  125.       gifStrip=param;
  126.  
  127.  
  128.  
  129.     if(debug)
  130.  
  131.     {
  132.  
  133.       if(gifStrip==null)
  134.  
  135.         System.out.println("No Gif Strip");
  136.  
  137.       else
  138.  
  139.         System.out.println("Gif Strip = " + gifStrip);
  140.  
  141.     }
  142.  
  143.  
  144.  
  145.     param=getParameter("PAUSE");
  146.  
  147.     if (param!=null)
  148.  
  149.       pauseLen=Integer.valueOf(param).intValue();
  150.  
  151.  
  152.  
  153.     r=g=b=255;
  154.  
  155.     param=getParameter("BGCOLOR");
  156.  
  157.     if(param!=null)
  158.  
  159.     {
  160.  
  161.       if(param.charAt(0)=='#')
  162.  
  163.       {
  164.  
  165.         r=readColorValue(param,1,3);
  166.  
  167.         g=readColorValue(param,3,5);
  168.  
  169.         b=readColorValue(param,5,7);
  170.  
  171.       }
  172.  
  173.     }
  174.  
  175.     
  176.  
  177.     if(debug)
  178.  
  179.         System.out.println("Colors, r="+r+" g="+g+" b="+b);
  180.  
  181.  
  182.  
  183.     backGround=new Color(r,g,b);
  184.  
  185.     
  186.  
  187.   }
  188.  
  189.   
  190.  
  191.   public int readColorValue(String param,int start,int stop)
  192.  
  193.   {
  194.  
  195.     String temp;
  196.  
  197.  
  198.  
  199.     try
  200.  
  201.     {
  202.  
  203.       temp=param.substring(start,stop);
  204.  
  205.     } catch(StringIndexOutOfBoundsException e){return 0;}
  206.  
  207.  
  208.  
  209.     try
  210.  
  211.     {
  212.  
  213.       return Integer.valueOf(temp,16).intValue();
  214.  
  215.     } catch(NumberFormatException e){return 0;}
  216.  
  217.   }
  218.  
  219.    
  220.  
  221.   public void paint(Graphics g)
  222.  
  223.   {  
  224.  
  225.     if(urlError)
  226.  
  227.       g.drawString("Malformed URL",10,Height/2-10);
  228.  
  229.     else if(imageLoadError)
  230.  
  231.        g.drawString("Images load error",10,Height/2-10);
  232.  
  233.      else if((offScrGC!=null))
  234.  
  235.     {
  236.  
  237.       offScrGC.fillRect(0,0,Width,Height);
  238.  
  239.       offScrGC.drawImage(gif,-1*gifX,0,this);
  240.  
  241.       g.drawImage(offScrImage,0,0,this);
  242.  
  243.     }
  244.  
  245.   }
  246.  
  247.  
  248.  
  249.   public void update(Graphics g){
  250.  
  251.     paint(g);
  252.  
  253.   }
  254.  
  255.  
  256.  
  257.   public synchronized boolean imageUpdate(Image img, int infoFlags,
  258.  
  259.                                           int x, int y,
  260.  
  261.                                           int width, int height) 
  262.  
  263.   {
  264.  
  265.  
  266.  
  267.     if ((infoFlags & ERROR) != 0) {
  268.  
  269.       imageLoadError = true;
  270.  
  271.     }
  272.  
  273.     notifyAll();
  274.  
  275.     return true;
  276.  
  277.   }
  278.  
  279.  
  280.  
  281.   public synchronized void loadImages()
  282.  
  283.   {
  284.  
  285.     gif=getImage(getCodeBase(),gifStrip);
  286.  
  287.  
  288.  
  289.     if (gif==null)
  290.  
  291.       imageLoadError=true;
  292.  
  293.  
  294.  
  295.     else
  296.  
  297.       while((gifWidth=gif.getWidth(this))<0)
  298.  
  299.       {
  300.  
  301.         try
  302.  
  303.         {
  304.  
  305.           wait();
  306.  
  307.         } catch(InterruptedException e){}
  308.  
  309.       }
  310.  
  311.  
  312.  
  313.     if(debug)
  314.  
  315.       System.out.println("Gif Strip Loaded");  
  316.  
  317.   }
  318.  
  319.  
  320.  
  321.     
  322.  
  323.   public void run() {
  324.  
  325.     int i,j;
  326.  
  327.  
  328.  
  329.     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  330.  
  331.  
  332.  
  333.     loadImages();
  334.  
  335.  
  336.  
  337.     offScrImage=createImage(Width,Height);
  338.  
  339.     offScrGC=offScrImage.getGraphics();
  340.  
  341.     offScrGC.setColor(backGround);
  342.  
  343.  
  344.  
  345.     repaint();
  346.  
  347.  
  348.  
  349.     for(i=0;i<40;i++)
  350.  
  351.     {
  352.  
  353.       try
  354.  
  355.       {
  356.  
  357.         kicker.sleep(50);
  358.  
  359.       } catch (InterruptedException e){}
  360.  
  361.     }
  362.  
  363.  
  364.  
  365.     repaint();
  366.  
  367.  
  368.  
  369.     while(true)
  370.  
  371.     {
  372.  
  373.       gifX+=Width;
  374.  
  375.  
  376.  
  377.       if (gifX>(gifWidth-Width))
  378.  
  379.         gifX=0;
  380.  
  381.  
  382.  
  383.       if(debug)
  384.  
  385.         System.out.println("gifX ="+gifX);
  386.  
  387.  
  388.  
  389.       repaint();
  390.  
  391.       try
  392.  
  393.       {
  394.  
  395.         kicker.sleep(pauseLen);
  396.  
  397.       } catch (InterruptedException e){}
  398.  
  399.     
  400.  
  401.     }
  402.  
  403.   }
  404.  
  405.   
  406.  
  407.   public void start() {
  408.  
  409.     if (kicker == null) {
  410.  
  411.       kicker = new Thread(this);
  412.  
  413.       kicker.start();
  414.  
  415.     }
  416.  
  417.   }
  418.  
  419.  
  420.  
  421.   public void stop() {
  422.  
  423.     kicker.stop();
  424.  
  425.     kicker = null;
  426.  
  427.   }
  428.  
  429.   
  430.  
  431.   public boolean mouseDown(Event evt,int x, int y)
  432.  
  433.   {
  434.  
  435.     if(debug)
  436.  
  437.       System.out.println("Mouse Click");
  438.  
  439.  
  440.  
  441.     if(link!=null)
  442.  
  443.     {
  444.  
  445.       if(debug)
  446.  
  447.         System.out.println("Launching URL");
  448.  
  449.  
  450.  
  451.       getAppletContext().showDocument(link);
  452.  
  453.       return true;     
  454.  
  455.     }
  456.  
  457.     
  458.  
  459.     if(debug)
  460.  
  461.       System.out.println("No URL to launch");
  462.  
  463.  
  464.  
  465.     return false;
  466.  
  467.   }
  468.  
  469.  
  470.  
  471. }
  472.  
  473.