FileDocCategorySizeDatePackage
CoolHeadLines.javaAPI DocExample13761Mon Jun 01 01:00:00 BST 1998None

CoolHeadLines

public class CoolHeadLines extends Applet implements Runnable

Fields Summary
private final String
PARAM_BackColor
private final String
PARAM_TextColor
private final String
PARAM_HiliteTextColor
private final String
PARAM_ScrollDelay
private final String
PARAM_MessageDelay
private final String
PARAM_NumItems
private final String
PARAM_URLPrefix
private Color
m_colBackColor
private Color
m_colTextColor
private Color
m_colHiliteTextColor
private int
m_iScrollDelay
private int
m_iMessageDelay
private int
m_iNumItems
private String
m_strURLPrefix
private Thread
m_CoolHeadLines
private Dimension
m_dimAppletSize
private Vector
m_vectData
private Font
m_font
private FontMetrics
m_fm
private int
m_iFontHeight
private DoubleBuffer
m_buff
private boolean
m_fStoppedScrolling
private int
m_iCurrentMessage
private int
m_iMaxMessage
private int
m_iOldMessageSelected
private int
m_iOldMessageSelectedYOffset
Constructors Summary
public CoolHeadLines()

	// what was the last message location on screen?

	////////////////////////////////////////////////////////////////////////////
	// ctor
	 
	
		// set some default colors
		m_colBackColor =	   new Color(255,255,128);
		m_colTextColor = 	   new Color(0,0,0);
		m_colHiliteTextColor = new Color(255,0,0);
	
Methods Summary
private voiddrawHeadlines()

		// get the next message and loop round to message #0 
		// if we reach the last message
		m_iCurrentMessage++;
		m_iCurrentMessage %= m_vectData.size();

		// if we are about to display the first message then clear the screen
		if (m_iCurrentMessage == 0)
			for (int i=0; i<m_iMaxMessage; i++)
				scrollOneLine();

		// write in the text in the off-screen buffer. 
		// note the text is drawn BELOW the bottom of the applet so when it
		// gets scrolled it appears to rise from nowhere
		drawString(m_buff.getGraphics(),
				   m_iCurrentMessage,
				   0,
				   m_buff.getSize().height-m_fm.getDescent(),
				   m_colTextColor);

		// scroll one line
		scrollOneLine();
	
private voiddrawString(java.awt.Graphics g, java.lang.String str, int x, int y, java.awt.Color col)

		int iFontWidth = m_fm.charWidth('W");
		int iOvalWidth = Math.min(iFontWidth/2,m_iFontHeight/2);
		g.setFont(m_font);
		g.setColor(col);
		g.drawString(str,x + iFontWidth,y);
		g.fillOval(x + iOvalWidth/2,y-iOvalWidth-2,iOvalWidth,iOvalWidth);
	
private voiddrawString(java.awt.Graphics g, int iMsg, int x, int y, java.awt.Color col)

		drawString(g,getStringFromVector(iMsg),x,y,col);
	
public java.lang.StringgetAppletInfo()

		return "Name: CoolHeadLines\r\n" +
		       "Author: Michael Howard (mikehow@microsoft.com)\r\n" +
		       "Created with Microsoft Visual J++ Version 1.1";
	
public java.awt.ColorgetColorFromRGB(java.lang.String strRGB)

		StringTokenizer st  = new StringTokenizer(strRGB);
		int iRed   = Integer.parseInt(st.nextToken());
		int iGreen = Integer.parseInt(st.nextToken());
		int iBlue  = Integer.parseInt(st.nextToken());
		return new Color(iRed,iGreen,iBlue);
	
public java.lang.String[][]getParameterInfo()

		String[][] info =
		{
			{ PARAM_BackColor,			"String",	"Background Color" },
			{ PARAM_TextColor,			"String",	"Text Color" },
			{ PARAM_HiliteTextColor,	"String",	"Hilited Text Color" },
			{ PARAM_ScrollDelay,		"int",		"msec delay between each scroll" },
			{ PARAM_MessageDelay,		"int",		"sec delay between each message" },
			{ PARAM_NumItems,			"int",		"Number of items" },
			{ PARAM_URLPrefix,			"String",	"Prefix for all .htm or .asp files" },
		};

		return info;		
	
private java.lang.StringgetStringFromVector(int iIndex)

		TextJump t = (TextJump)m_vectData.elementAt(iIndex);
		return (String)t.getString();
	
private intgetYOffset(int y)

		int iY = (m_dimAppletSize.height - y) / m_iFontHeight;
		return iY;
	
public voidinit()

		String param;

		// background color
		param = getParameter(PARAM_BackColor);
		if (param != null)
			m_colBackColor = getColorFromRGB(param);

		// text color
		param = getParameter(PARAM_TextColor);
		if (param != null)
			m_colTextColor = getColorFromRGB(param);

		// high-lighted text color
		param = getParameter(PARAM_HiliteTextColor);
		if (param != null)
			m_colHiliteTextColor = getColorFromRGB(param);

		// scroll delay in msec
		param = getParameter(PARAM_ScrollDelay);
		if (param != null)
			m_iScrollDelay = Integer.parseInt(param);

		// pause between messages in seconds
		param = getParameter(PARAM_MessageDelay);
		if (param != null)
			m_iMessageDelay = Integer.parseInt(param);

		// number of items to be displayed
		param = getParameter(PARAM_NumItems);
		if (param != null)
			m_iNumItems = Integer.parseInt(param);

		// path for .htm and .asp files
		param = getParameter(PARAM_URLPrefix);
		if (param != null)
			m_strURLPrefix = param;

		// various vars
		m_dimAppletSize = size();
		m_font			= new Font("Helvetica",Font.PLAIN,12);
		m_fm			= getFontMetrics(m_font);
		m_iFontHeight	= m_fm.getMaxDescent() + m_fm.getLeading() + m_fm.getMaxAscent();
		Rectangle r		= new Rectangle(0,0,m_dimAppletSize.width,m_dimAppletSize.height + m_iFontHeight);
		m_buff			= new DoubleBuffer(this,r);

		// fill in the background of the off-screen buffer and the applet
		m_buff.getGraphics().setColor(m_colBackColor);
		m_buff.getGraphics().fillRect(r.x, r.y, r.width, r.height);
		setBackground(m_colBackColor);

		// maximum number of messages
		m_iMaxMessage = 1 +( m_dimAppletSize.height / m_iFontHeight);

		// get all the headlines from the <param> tags
		m_vectData = new Vector();
		for (int i=0; i<m_iNumItems; i++)
		{
			String strText = new String();
			strText = getParameter("Text"+i);

			String strURL = new String();
			strURL = getParameter("URL"+i);
			m_vectData.addElement(new TextJump(strURL,strText));
		}
	
public booleanmouseDown(java.awt.Event evt, int x, int y)

		// if the image is still scrolling then bail
		if (!m_fStoppedScrolling)
			return true;

		// get the message we must be over
		int iYOffset = getYOffset(y);
		int iWhichMsg = m_iCurrentMessage - iYOffset;

		try
		{
			m_CoolHeadLines.suspend();

 			// Build the url for the sample
			String strSampleURL = m_strURLPrefix + "/" + (((TextJump)m_vectData.elementAt(iWhichMsg)).getURL());
			showStatus(strSampleURL);
			getAppletContext().showDocument(new URL(strSampleURL));

		} catch(MalformedURLException e) { }

		return true;
	
public booleanmouseExit(java.awt.Event evt, int x, int y)

		if (!m_fStoppedScrolling)
			return true;

		if (m_iOldMessageSelected >= 0)
		{
			drawString(getGraphics(),
					   m_iOldMessageSelected,
					   0,
					   m_dimAppletSize.height - (m_iOldMessageSelectedYOffset * m_iFontHeight) - m_fm.getDescent() + 1,
					   m_colTextColor);

			m_iOldMessageSelected=-1;
		}

		return true;
	
public booleanmouseMove(java.awt.Event evt, int x, int y)

		// if the image is still scrolling then bail
		if (!m_fStoppedScrolling)
			return true;

		// get the message we must be over
		int iYOffset = getYOffset(y);
		int iWhichMsg = m_iCurrentMessage - iYOffset;

		// if we are still over the same message then don't do anything
		if (m_iOldMessageSelected == iWhichMsg)
			return true;

		// if there was an old message we were over then re-draw it in it's non-highlighted color
		if (m_iOldMessageSelected >= 0)
		{
			drawString(getGraphics(),
					   m_iOldMessageSelected,
					   0,
					   m_dimAppletSize.height - (m_iOldMessageSelectedYOffset * m_iFontHeight) - m_fm.getDescent() + 1,
					   m_colTextColor);

			if (iWhichMsg < 0)
				m_iOldMessageSelected=-1;
		}

		// if we are over a valid message then draw it in a highlighted color
		if (iWhichMsg >= 0)
		{
			// set the old message to be this message
			m_iOldMessageSelected = iWhichMsg;
			m_iOldMessageSelectedYOffset = iYOffset;

			drawString(getGraphics(),
					   iWhichMsg,
					   0,
					   m_dimAppletSize.height - (iYOffset * m_iFontHeight) - m_fm.getDescent() + 1,
					   m_colHiliteTextColor);

			// put the string on the status line
			String strStatusMsg = getStringFromVector(iWhichMsg) + 
								  " at " + m_strURLPrefix + 
								  "/" + 
								  (((TextJump)m_vectData.elementAt(iWhichMsg)).getURL());
			getAppletContext().showStatus(strStatusMsg);
		}

		return true;
	
public voidrun()

		while (true)
		{
			try
			{
				// the applet is about to scroll
				m_fStoppedScrolling = false;

				// there is no old message because we are about to 
				// scroll which invalidates the last message
				m_iOldMessageSelected=-1;
				m_iOldMessageSelectedYOffset=-1;

				// re-draw the headlines
				drawHeadlines();

				// ok, we've stopped scrolling, we can now accept mouse input
				m_fStoppedScrolling = true;

				// ZZZzzzz...
				Thread.sleep(m_iMessageDelay * 1000);
			}
			catch (InterruptedException e)
			{
				stop();
			}
		}
	
private voidscrollOneLine()

		// smoothly draw the off-screen buffer
		for (int j=0; j < m_iFontHeight; j++)
		{
			getGraphics().drawImage(m_buff.getImage(),0,-j,this);
			try { Thread.sleep(m_iScrollDelay);}
			catch (InterruptedException e) { }
		}

		Graphics g = m_buff.getGraphics();

		// move image up one-line inside the off-screen buffer
		g.drawImage(m_buff.getImage(),0,-m_iFontHeight,this);

		// erase old text at bottom of image
		g.setColor(m_colBackColor);
		g.fillRect(0,
				   m_dimAppletSize.height,
				   m_dimAppletSize.width,
				   m_dimAppletSize.height+m_iFontHeight);
	
public voidstart()

		if (m_CoolHeadLines == null)
		{
			m_CoolHeadLines = new Thread(this);
			m_CoolHeadLines.start();
		}
		else
		{
			// This is so we get a quick refresh when we move back to the homepage
			drawHeadlines();
		}
	
public voidstop()

		if (m_CoolHeadLines != null)
		{
			m_CoolHeadLines.stop();
			m_CoolHeadLines = null;
		}