home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / iis4_07.cab / CoolScroll.java < prev    next >
Text File  |  1997-11-01  |  10KB  |  349 lines

  1. ////////////////////////////////////////////////////////////////////////////
  2. // Implementatation of CoolScroll class
  3. //
  4. // Applet to load and scroll a series of images 
  5. // and have URLs associated with them
  6. //
  7. // It loads the images and URLs and places the details 
  8. // in a vector of ImageJump objects. The images then scroll in a sequential
  9. // order and when they come to rest the user can select one by 
  10. // clicking the mouse and hence jump to the URL in question.
  11. //
  12. // ********************************************************
  13. // Note that the applet must be the same size as the images
  14. // ********************************************************
  15. //
  16. // This is a part of the Internet Information Server SDK Samples
  17. // Copyright (C) 1997 Microsoft Corporation
  18. // All rights reserved.
  19. //
  20. // This source code is only intended as a supplement to the Software 
  21. // Development Kit Reference and related electronic documentation provided.
  22. //
  23. ////////////////////////////////////////////////////////////////////////////
  24.  
  25. import java.applet.*;
  26. import java.awt.*;
  27. import java.util.*;
  28. import java.net.*;
  29. import ImageJump;
  30.  
  31. public class CoolScroll extends Applet implements Runnable
  32. {
  33.     // default size of applet images
  34.     private int m_xsize = 240;
  35.     private int m_ysize = 240;
  36.  
  37.     // all the possible scrolling directions
  38.     static private final int SCROLL_LEFT       = 0;
  39.     static private final int SCROLL_RIGHT       = 1;
  40.     static private final int SCROLL_DOWN       = 2;
  41.     static private final int SCROLL_UP           = 3;
  42.     static private final int SCROLL_LEFT_UP       = 4;
  43.     static private final int SCROLL_LEFT_DOWN  = 5;
  44.     static private final int SCROLL_RIGHT_UP   = 6;
  45.     static private final int SCROLL_RIGHT_DOWN = 7;
  46.  
  47.     // main worker thread
  48.     private Thread    m_Thread = null;
  49.  
  50.     // has the image stopped scrolling?
  51.     private boolean m_fImageAtRest = true;
  52.  
  53.     // information about the images
  54.     private int        m_iNumItems = 0;
  55.     private int        m_iCurrentItem = 0;
  56.     private Vector    m_vectItems;
  57.     private String    m_strURLPrefix;
  58.  
  59.     // what's the scroll rate and delay
  60.     private int        m_iScrollRate = 50;
  61.     private int        m_iDelay = 4;
  62.  
  63.     // info about the 'click here' message
  64.     private String    m_strClickHere="Click Here";
  65.     private Font    m_font;
  66.     private FontMetrics m_fm;
  67.  
  68.     // constants used to parse <param> tags
  69.     private final String PARAM_NumItems = "NumItems";
  70.     private final String PARAM_ScrollRate = "ScrollRate";
  71.     private final String PARAM_Delay = "Delay";
  72.     private final String PARAM_ClickHereMessage = "ClickHereMsg";
  73.     private final String PARAM_URLPrefix="URLPrefix";
  74.  
  75.     ////////////////////////////////////////////////////////////////////////////
  76.     // ctor
  77.     public CoolScroll()
  78.     {
  79.         m_vectItems = new Vector();
  80.         m_iCurrentItem = 0;
  81.     }
  82.  
  83.     ////////////////////////////////////////////////////////////////////////////
  84.     public String getAppletInfo()
  85.     {
  86.         return "Name: CoolScroll\r\n" +
  87.                "Author: Michael Howard (mikehow@microsoft.com)\r\n" +
  88.                "Created with Microsoft Visual J++ Version 1.1";
  89.     }
  90.  
  91.     ////////////////////////////////////////////////////////////////////////////
  92.     public String[][] getParameterInfo()
  93.     {
  94.         String[][] info =
  95.         {
  96.             { PARAM_NumItems,         "int",   "Number of items" },
  97.             { PARAM_ScrollRate,         "int",   "Scroll delay (msec)" },
  98.             { PARAM_Delay,             "int",   "How long to wait until next image shows (secs)" },
  99.             { PARAM_ClickHereMessage,"String","Text to display when image comes to rest"},
  100.             { PARAM_URLPrefix,         "String","Prefix for all .htm or .asp files"},
  101.         };
  102.  
  103.         return info;        
  104.     }
  105.  
  106.     ////////////////////////////////////////////////////////////////////////////
  107.     public void init()
  108.     {
  109.         String param;
  110.  
  111.         // get the data from the <param> tags
  112.         param = getParameter(PARAM_NumItems);
  113.         if (param != null)
  114.             m_iNumItems = Integer.parseInt(param);
  115.  
  116.         param = getParameter(PARAM_ScrollRate);
  117.         if (param != null)
  118.             m_iScrollRate = Integer.parseInt(param);
  119.  
  120.         param = getParameter(PARAM_Delay);
  121.         if (param != null)
  122.             m_iDelay = Integer.parseInt(param);
  123.  
  124.         param = getParameter(PARAM_ClickHereMessage);
  125.         if (param != null)
  126.             m_strClickHere = param;
  127.  
  128.         param = getParameter(PARAM_URLPrefix);
  129.         if (param != null)
  130.             m_strURLPrefix = param;
  131.  
  132.         // store the size of the applet
  133.         Dimension d = size();
  134.         m_xsize = d.width;
  135.         m_ysize = d.height;
  136.  
  137.         m_font = new Font("Helvetica",Font.BOLD,14);
  138.         m_fm   = getFontMetrics(m_font);
  139.  
  140.         getImages();
  141.     }
  142.  
  143.     ////////////////////////////////////////////////////////////////////////////
  144.     // getImages
  145.     // Read all the images from the param tags
  146.     private void getImages()
  147.     {
  148.         // may be a slow link
  149.         MediaTracker tracker = new MediaTracker(this);
  150.         for (int i=0; i<m_iNumItems; i++)
  151.         {
  152.             String strImage = getParameter("Image"+i);
  153.             String strURL = getParameter("URL"+i);
  154.             if (strImage != null)
  155.             {
  156.                 // build up a new ImageJump object
  157.                 Image img = getImage(getDocumentBase(),strImage);
  158.                 m_vectItems.addElement(new ImageJump(strURL,img));
  159.                 tracker.addImage(img,0);
  160.             }
  161.         }
  162.  
  163.         // wait for the images to load
  164.         try  { tracker.waitForAll(); } 
  165.         catch (InterruptedException  e) { }
  166.     }
  167.  
  168.     ////////////////////////////////////////////////////////////////////////////
  169.     // paintItems
  170.     // Does all of the scrolling work. Is called by run() only.
  171.     // iImage is the image index in the vector
  172.     private void paintItems(int iImage)
  173.     {
  174.         // some defaults
  175.         int iScrollType = SCROLL_LEFT;
  176.         int iScrollOptions = 4;
  177.  
  178.         // if the image is a square then we support diagonal scrolling also
  179.         if (m_ysize == m_xsize)
  180.             iScrollOptions = 8;
  181.  
  182.         // choose a scrolling method
  183.         switch((int)(Math.random() * iScrollOptions))
  184.         {
  185.             case 0 : iScrollType = SCROLL_LEFT;            break;
  186.             case 1 : iScrollType = SCROLL_RIGHT;        break;
  187.             case 2 : iScrollType = SCROLL_DOWN;            break;
  188.             case 3 : iScrollType = SCROLL_UP;            break;
  189.             
  190.             case 4 : iScrollType = SCROLL_LEFT_UP;        break;
  191.             case 5 : iScrollType = SCROLL_LEFT_DOWN;    break;
  192.             case 6 : iScrollType = SCROLL_RIGHT_UP;        break;
  193.             default : iScrollType = SCROLL_RIGHT_DOWN;    break;
  194.         }
  195.  
  196.         // get the image
  197.         Image img = ((ImageJump)m_vectItems.elementAt(iImage)).getImage();
  198.         if (img == null)
  199.         {
  200.             showStatus("No images!");
  201.         }
  202.         else
  203.         {
  204.             // determine how many iterations
  205.             int iIterations = m_ysize;
  206.  
  207.             // if rectangle and scrolling left or right then iterations is width
  208.             if (m_ysize != m_xsize)
  209.                 if (iScrollType == SCROLL_LEFT || iScrollType == SCROLL_RIGHT)
  210.                     iIterations = m_xsize;
  211.  
  212.             // do the scrolling
  213.             for (int i=0; i<=iIterations; i++)
  214.             {
  215.                 int x,y;
  216.                 switch(iScrollType)
  217.                 {
  218.                     case SCROLL_LEFT        : x=m_xsize-i;    y=0;            break;
  219.                     case SCROLL_RIGHT        : x=i-m_xsize;    y=0;            break;
  220.                     case SCROLL_DOWN        : x=0;            y=i-m_ysize;    break;
  221.                     case SCROLL_UP            : x=0;            y=m_ysize-i;    break;
  222.  
  223.                     case SCROLL_LEFT_UP        : x=m_xsize-i;    y=m_ysize-i;    break;
  224.                     case SCROLL_LEFT_DOWN    : x=m_xsize-i;    y=i-m_ysize;    break;
  225.                     case SCROLL_RIGHT_UP    : x=i-m_xsize;    y=m_ysize-i;    break;
  226.                     default                    : x=i-m_xsize;    y=i-m_ysize;    break;
  227.                 }
  228.  
  229.                 // pause a moment
  230.                 try {Thread.sleep(m_iScrollRate);} catch (InterruptedException e){}
  231.  
  232.                 // draw the image
  233.                 if (m_Thread!=null)
  234.                     drawAdvert(img,x,y);
  235.             }
  236.         }
  237.     }
  238.  
  239.     ////////////////////////////////////////////////////////////////////////////
  240.     private void drawAdvert(Image img, int x, int y)
  241.     {
  242.         getGraphics().drawImage(img,x,y,this);
  243.     }
  244.  
  245.     ////////////////////////////////////////////////////////////////////////////
  246.     public void start()
  247.     {
  248.         if (m_Thread == null)
  249.         {
  250.             m_Thread = new Thread(this);
  251.             m_Thread.start();
  252.         }
  253.     }
  254.     
  255.     ////////////////////////////////////////////////////////////////////////////
  256.     public void stop()
  257.     {
  258.         m_Thread.stop();
  259.         m_Thread = null;
  260.     }
  261.  
  262.     ////////////////////////////////////////////////////////////////////////////
  263.     // run
  264.     // Called when thread is created. Counts through each element in the vector
  265.     // and gets them scrolled by paintImages
  266.     public void run()
  267.     {
  268.         int iSize = m_vectItems.size();
  269.         
  270.         while (m_Thread != null)
  271.         {
  272.             // loop through all images
  273.             m_iCurrentItem++;
  274.             m_iCurrentItem%=iSize;
  275.           
  276.             m_fImageAtRest = false;
  277.             
  278.             if (m_Thread != null)
  279.             {
  280.                 paintItems(m_iCurrentItem);
  281.                 m_fImageAtRest = true;
  282.  
  283.                 drawAtRestMessage();
  284.  
  285.                 // allow user to read message
  286.                 try { m_Thread.sleep(m_iDelay * 1000); } 
  287.                 catch (InterruptedException e) {  }
  288.  
  289.                 // redraw the image to clean up the message 
  290.                 if (m_Thread != null)
  291.                     drawAdvert(((ImageJump)m_vectItems.elementAt(m_iCurrentItem)).getImage(),0,0);
  292.             }
  293.         }
  294.     }
  295.  
  296.     ////////////////////////////////////////////////////////////////////////////
  297.     // drawAtRestMessage
  298.     // When an image comes to rest a small message is displayed in the bottom-right
  299.     // corner to inform the user they can click on the image
  300.     private void drawAtRestMessage()
  301.     {
  302.         Graphics g = getGraphics();
  303.         int iWidth = m_fm.stringWidth(m_strClickHere);
  304.         int iHeight = m_fm.getLeading() + m_fm.getMaxAscent() + m_fm.getMaxDescent();
  305.  
  306.         // draw the white background
  307.         g.setColor(Color.white);
  308.         g.fillRect(m_xsize-iWidth-4,m_ysize-iHeight-2,iWidth+2,iHeight);
  309.  
  310.         // draw a blue border around the background
  311.         g.setColor(Color.blue);
  312.         g.drawRect(m_xsize-iWidth-4,m_ysize-iHeight-2,iWidth+2,iHeight);
  313.  
  314.         // draw the text
  315.         g.setFont(m_font);
  316.         g.setColor(Color.black);
  317.         g.drawString(m_strClickHere,m_xsize-iWidth-2,m_ysize-4);
  318.  
  319.         g.dispose();
  320.     }
  321.  
  322.     ////////////////////////////////////////////////////////////////////////////
  323.     // mouseDown
  324.     // If user clicked mouse then....
  325.     public boolean mouseDown(Event evt, int x, int y)
  326.     {
  327.         // only valid if image has come to rest (ie; not scrolling)
  328.         if (m_fImageAtRest)
  329.         {
  330.             try
  331.             {
  332.                 m_Thread.suspend();
  333.  
  334.                 // Jump to the detail page 
  335.                 String strSampleURL = m_strURLPrefix + "/" + (((ImageJump)m_vectItems.elementAt(m_iCurrentItem)).getURL());
  336.                 showStatus(strSampleURL);
  337.                 getAppletContext().showDocument(new URL(strSampleURL));
  338.  
  339.             } 
  340.             catch(MalformedURLException e) 
  341.             { 
  342.                 m_Thread.resume();
  343.             }
  344.         }
  345.  
  346.         return true;
  347.     }
  348. }
  349.