home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-04-27 | 10.0 KB | 349 lines |
- ////////////////////////////////////////////////////////////////////////////
- // Implementatation of CoolScroll class
- //
- // Applet to load and scroll a series of images
- // and have URLs associated with them
- //
- // It loads the images and URLs and places the details
- // in a vector of ImageJump objects. The images then scroll in a sequential
- // order and when they come to rest the user can select one by
- // clicking the mouse and hence jump to the URL in question.
- //
- // ********************************************************
- // Note that the applet must be the same size as the images
- // ********************************************************
- //
- // This is a part of the Internet Information Server SDK Samples
- // Copyright (C) 1997 Microsoft Corporation
- // All rights reserved.
- //
- // This source code is only intended as a supplement to the Software
- // Development Kit Reference and related electronic documentation provided.
- //
- ////////////////////////////////////////////////////////////////////////////
-
- import java.applet.*;
- import java.awt.*;
- import java.util.*;
- import java.net.*;
- import ImageJump;
-
- public class CoolScroll extends Applet implements Runnable
- {
- // default size of applet images
- private int m_xsize = 240;
- private int m_ysize = 240;
-
- // all the possible scrolling directions
- static private final int SCROLL_LEFT = 0;
- static private final int SCROLL_RIGHT = 1;
- static private final int SCROLL_DOWN = 2;
- static private final int SCROLL_UP = 3;
- static private final int SCROLL_LEFT_UP = 4;
- static private final int SCROLL_LEFT_DOWN = 5;
- static private final int SCROLL_RIGHT_UP = 6;
- static private final int SCROLL_RIGHT_DOWN = 7;
-
- // main worker thread
- private Thread m_Thread = null;
-
- // has the image stopped scrolling?
- private boolean m_fImageAtRest = true;
-
- // information about the images
- private int m_iNumItems = 0;
- private int m_iCurrentItem = 0;
- private Vector m_vectItems;
- private String m_strURLPrefix;
-
- // what's the scroll rate and delay
- private int m_iScrollRate = 50;
- private int m_iDelay = 4;
-
- // info about the 'click here' message
- private String m_strClickHere="Click Here";
- private Font m_font;
- private FontMetrics m_fm;
-
- // constants used to parse <param> tags
- private final String PARAM_NumItems = "NumItems";
- private final String PARAM_ScrollRate = "ScrollRate";
- private final String PARAM_Delay = "Delay";
- private final String PARAM_ClickHereMessage = "ClickHereMsg";
- private final String PARAM_URLPrefix="URLPrefix";
-
- ////////////////////////////////////////////////////////////////////////////
- // ctor
- public CoolScroll()
- {
- m_vectItems = new Vector();
- m_iCurrentItem = 0;
- }
-
- ////////////////////////////////////////////////////////////////////////////
- public String getAppletInfo()
- {
- return "Name: CoolScroll\r\n" +
- "Author: Michael Howard (mikehow@microsoft.com)\r\n" +
- "Created with Microsoft Visual J++ Version 1.1";
- }
-
- ////////////////////////////////////////////////////////////////////////////
- public String[][] getParameterInfo()
- {
- String[][] info =
- {
- { PARAM_NumItems, "int", "Number of items" },
- { PARAM_ScrollRate, "int", "Scroll delay (msec)" },
- { PARAM_Delay, "int", "How long to wait until next image shows (secs)" },
- { PARAM_ClickHereMessage,"String","Text to display when image comes to rest"},
- { PARAM_URLPrefix, "String","Prefix for all .htm or .asp files"},
- };
-
- return info;
- }
-
- ////////////////////////////////////////////////////////////////////////////
- public void init()
- {
- String param;
-
- // get the data from the <param> tags
- param = getParameter(PARAM_NumItems);
- if (param != null)
- m_iNumItems = Integer.parseInt(param);
-
- param = getParameter(PARAM_ScrollRate);
- if (param != null)
- m_iScrollRate = Integer.parseInt(param);
-
- param = getParameter(PARAM_Delay);
- if (param != null)
- m_iDelay = Integer.parseInt(param);
-
- param = getParameter(PARAM_ClickHereMessage);
- if (param != null)
- m_strClickHere = param;
-
- param = getParameter(PARAM_URLPrefix);
- if (param != null)
- m_strURLPrefix = param;
-
- // store the size of the applet
- Dimension d = size();
- m_xsize = d.width;
- m_ysize = d.height;
-
- m_font = new Font("Helvetica",Font.BOLD,14);
- m_fm = getFontMetrics(m_font);
-
- getImages();
- }
-
- ////////////////////////////////////////////////////////////////////////////
- // getImages
- // Read all the images from the param tags
- private void getImages()
- {
- // may be a slow link
- MediaTracker tracker = new MediaTracker(this);
- for (int i=0; i<m_iNumItems; i++)
- {
- String strImage = getParameter("Image"+i);
- String strURL = getParameter("URL"+i);
- if (strImage != null)
- {
- // build up a new ImageJump object
- Image img = getImage(getDocumentBase(),strImage);
- m_vectItems.addElement(new ImageJump(strURL,img));
- tracker.addImage(img,0);
- }
- }
-
- // wait for the images to load
- try { tracker.waitForAll(); }
- catch (InterruptedException e) { }
- }
-
- ////////////////////////////////////////////////////////////////////////////
- // paintItems
- // Does all of the scrolling work. Is called by run() only.
- // iImage is the image index in the vector
- private void paintItems(int iImage)
- {
- // some defaults
- int iScrollType = SCROLL_LEFT;
- int iScrollOptions = 4;
-
- // if the image is a square then we support diagonal scrolling also
- if (m_ysize == m_xsize)
- iScrollOptions = 8;
-
- // choose a scrolling method
- switch((int)(Math.random() * iScrollOptions))
- {
- case 0 : iScrollType = SCROLL_LEFT; break;
- case 1 : iScrollType = SCROLL_RIGHT; break;
- case 2 : iScrollType = SCROLL_DOWN; break;
- case 3 : iScrollType = SCROLL_UP; break;
-
- case 4 : iScrollType = SCROLL_LEFT_UP; break;
- case 5 : iScrollType = SCROLL_LEFT_DOWN; break;
- case 6 : iScrollType = SCROLL_RIGHT_UP; break;
- default : iScrollType = SCROLL_RIGHT_DOWN; break;
- }
-
- // get the image
- Image img = ((ImageJump)m_vectItems.elementAt(iImage)).getImage();
- if (img == null)
- {
- showStatus("No images!");
- }
- else
- {
- // determine how many iterations
- int iIterations = m_ysize;
-
- // if rectangle and scrolling left or right then iterations is width
- if (m_ysize != m_xsize)
- if (iScrollType == SCROLL_LEFT || iScrollType == SCROLL_RIGHT)
- iIterations = m_xsize;
-
- // do the scrolling
- for (int i=0; i<=iIterations; i++)
- {
- int x,y;
- switch(iScrollType)
- {
- case SCROLL_LEFT : x=m_xsize-i; y=0; break;
- case SCROLL_RIGHT : x=i-m_xsize; y=0; break;
- case SCROLL_DOWN : x=0; y=i-m_ysize; break;
- case SCROLL_UP : x=0; y=m_ysize-i; break;
-
- case SCROLL_LEFT_UP : x=m_xsize-i; y=m_ysize-i; break;
- case SCROLL_LEFT_DOWN : x=m_xsize-i; y=i-m_ysize; break;
- case SCROLL_RIGHT_UP : x=i-m_xsize; y=m_ysize-i; break;
- default : x=i-m_xsize; y=i-m_ysize; break;
- }
-
- // pause a moment
- try {Thread.sleep(m_iScrollRate);} catch (InterruptedException e){}
-
- // draw the image
- if (m_Thread!=null)
- drawAdvert(img,x,y);
- }
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////
- private void drawAdvert(Image img, int x, int y)
- {
- getGraphics().drawImage(img,x,y,this);
- }
-
- ////////////////////////////////////////////////////////////////////////////
- public void start()
- {
- if (m_Thread == null)
- {
- m_Thread = new Thread(this);
- m_Thread.start();
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////
- public void stop()
- {
- m_Thread.stop();
- m_Thread = null;
- }
-
- ////////////////////////////////////////////////////////////////////////////
- // run
- // Called when thread is created. Counts through each element in the vector
- // and gets them scrolled by paintImages
- public void run()
- {
- int iSize = m_vectItems.size();
-
- while (m_Thread != null)
- {
- // loop through all images
- m_iCurrentItem++;
- m_iCurrentItem%=iSize;
-
- m_fImageAtRest = false;
-
- if (m_Thread != null)
- {
- paintItems(m_iCurrentItem);
- m_fImageAtRest = true;
-
- drawAtRestMessage();
-
- // allow user to read message
- try { m_Thread.sleep(m_iDelay * 1000); }
- catch (InterruptedException e) { }
-
- // redraw the image to clean up the message
- if (m_Thread != null)
- drawAdvert(((ImageJump)m_vectItems.elementAt(m_iCurrentItem)).getImage(),0,0);
- }
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////
- // drawAtRestMessage
- // When an image comes to rest a small message is displayed in the bottom-right
- // corner to inform the user they can click on the image
- private void drawAtRestMessage()
- {
- Graphics g = getGraphics();
- int iWidth = m_fm.stringWidth(m_strClickHere);
- int iHeight = m_fm.getLeading() + m_fm.getMaxAscent() + m_fm.getMaxDescent();
-
- // draw the white background
- g.setColor(Color.white);
- g.fillRect(m_xsize-iWidth-4,m_ysize-iHeight-2,iWidth+2,iHeight);
-
- // draw a blue border around the background
- g.setColor(Color.blue);
- g.drawRect(m_xsize-iWidth-4,m_ysize-iHeight-2,iWidth+2,iHeight);
-
- // draw the text
- g.setFont(m_font);
- g.setColor(Color.black);
- g.drawString(m_strClickHere,m_xsize-iWidth-2,m_ysize-4);
-
- g.dispose();
- }
-
- ////////////////////////////////////////////////////////////////////////////
- // mouseDown
- // If user clicked mouse then....
- public boolean mouseDown(Event evt, int x, int y)
- {
- // only valid if image has come to rest (ie; not scrolling)
- if (m_fImageAtRest)
- {
- try
- {
- m_Thread.suspend();
-
- // Jump to the detail page
- String strSampleURL = m_strURLPrefix + "/" + (((ImageJump)m_vectItems.elementAt(m_iCurrentItem)).getURL());
- showStatus(strSampleURL);
- getAppletContext().showDocument(new URL(strSampleURL));
-
- }
- catch(MalformedURLException e)
- {
- m_Thread.resume();
- }
- }
-
- return true;
- }
- }
-