home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 11.3 KB | 425 lines |
- /*---------------------------------------------------------------------------
-
- @(#)PjTicker.java 0.00 8-Feb-96
-
- A prototype of a scrolling, word-wrapping text area.
-
- Authors:
-
- lning Lindee Ning
-
- Version Ident:
-
- $Header:$
-
- History:
-
- 0.00 8-Feb-96 lning Initial Creation
- 25-Mar-96 jlee Changed Ticker'super class from Panel to Canvas for
- consistent use of Canvas component for drawing.
- 25-Mar-96 jlee Changed ticker's height from 25 to 27
- 28-Mar-96 jlee Used PjFinals for Ticker size
- ---------------------------------------------------------------------------*/
-
- package pj.awt;
-
- import pj.awt.PjFinals;
-
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Image;
-
- import java.util.Date;
-
- /**
- * Display the default messages in a scrolling text area.
- *
- * @version 0.00 09-Feb-96
- * @author Lindee Ning
- */
- public class Ticker extends Canvas implements Runnable
- {
- // --- Public constructors: set default message, speed, text color, background color and size
-
- public Ticker()
- {
- m_astrMessages[0] = "Welcome to Personal Journal! ... ";
- m_astrMessages[1] = "This Just In From Personal Journal ... ";
- m_astrMessages[2] = "See This Just In For The Latest News Summaries ... At ";
- m_astrMessages[3] = GetTimeDate();
-
- m_Speed = 4;
- m_MessageX = size().width;
- m_TxtColor = Color.white;
- m_BgColor = Color.black;
- m_Size = PjFinals.dimTickerSize;
- } // Ticker
-
- // --- Public operations
-
-
- /**
- * Set the message
- */
-
- public void SetMessage(String str)
- {
- if(!(str == ""))
- {
- m_astrNewMessages[0] = "This Just In From Personal Journal ... See This Just In For The Latest News Summaries ... ";
- m_astrNewMessages[1] = "Your Quotes (At Least 15-Minute Delayed): ";
- m_astrNewMessages[2] = str + " ... At ";
- m_astrNewMessages[3] = GetTimeDate();
- m_ChangeMessage = true;
- }
- } //SetMessage
-
- /**
- * Set the size
- */
-
- public void SetSize(Dimension size)
- {
- m_Size = size;
- } //SetSize
-
-
- /**
- * Set the speed
- */
- public void SetSpeed(int speed)
- {
- m_Speed = speed;
- } //SetSpeed
-
- /**
- * Set back the default speed
- */
- public void SetSpeed()
- {
- m_Speed = 4;
- } //SetSpeed
-
-
- /**
- * Set the start x-position on the ticker
- */
- public void SetStartPosition(int x)
- {
- m_MessageX = x;
- } //SetStartPosition
-
- /**
- * Set back the default start x-position on the ticker
- */
- public void SetStartPosition()
- {
- m_MessageX = size().width;
- } //SetStartPosition
-
-
-
- /**
- * Set the background color
- */
-
- public void SetBgColor(Color col)
- {
- m_BgColor = col;
- } //SetBgColor
-
- /**
- * Set the test color
- */
-
- public void SetTxtColor(Color col)
- {
- m_TxtColor = col;
- } //SetTxtColor
-
- /**
- * Set the minimum size of the ticker
- */
-
- public Dimension minimumSize()
- {
- return m_Size;
- } //minimunSize
-
- /**
- * Set the preferred size of the ticker
- */
-
- public Dimension preferredSize()
- {
- return minimumSize();
- } //preferredSize
-
- /**
- * Get the number of messages being displayed
- */
-
- public int GetNumMessages()
- {
- return m_astrMessages.length;
- } //GetNumMessages
-
-
- /**
- * Catch the mouse clicking
- */
-
- public boolean mouseDown(Event evt, int x, int y)
- {
- if(m_Action)
- {
- m_Action = false;
- m_Speed = 0;
- }
- else
- {
- m_Action = true;
- m_Speed = 4;
- }
- return true;
- } //mouseDown
-
-
- /**
- * Run the loop.
- */
-
- public void run()
- {
- while(true)
- {
- NextPos();
- try {Thread.sleep(100);}
- catch (InterruptedException e){ }
- }
- } //run
-
-
- /**
- * Paint
- */
-
- public void paint(Graphics gr) { update(gr); }
-
- /**
- * Show the stuff
- */
-
- public synchronized void update(Graphics gr)
- {
- CreateParams();
-
- // fill area with bgcolor
- m_Graphics.setColor(m_BgColor);
- m_Graphics.fillRect(0,0,m_PrevRec.width,m_PrevRec.height);
- // draw the text
- m_Graphics.setColor(m_TxtColor);
- m_Graphics.setFont(m_Font);
-
- int head = m_MessageX;
- int tail = 0;
- for(int i=0;i<m_astrMessages.length;i++)
- {
- tail = head + m_MsgLengthVec[i];
- if(! ((head > m_PrevRec.width) || (tail < 0)) )
- {
- m_Graphics.drawString((String) m_astrMessages[i], head, m_MessageY);
- }
- head = tail + 2;
- }
-
- // Display message on the screen
- gr.drawImage(m_Image,0,0,this);
-
- } //update
-
-
-
- // --- Private operations
-
-
- /**
- * Create the image Parameters.
- */
- private void CreateParams()
- {
- // if size changed. re-create image
- boolean bSizeChanged = false;
- if ((size().height != m_PrevRec.height) ||
- (size().width != m_PrevRec.width))
- {
- bSizeChanged = true;
-
- // delete old
- if (m_Graphics != null) { m_Graphics.finalize(); }
- if (m_Image != null) { m_Image = null; }
-
- m_Image = createImage(size().width, size().height);
- m_Graphics = m_Image.getGraphics();
-
-
- m_PrevRec = size();
- }
-
- // if msg or size changed, re-calculate each message length and font
- if(m_ChangeMessage && m_endofdisplay)
- {
- m_astrMessages = m_astrNewMessages;
- m_endofdisplay = false;
- m_ChangeMessage = false;
- m_resetMessage = true;
- }
- if( m_resetMessage || bSizeChanged )
- {
- // Select a proper font size
- int refFont = 10;
- Font font = new Font("TimesRoman", Font.BOLD + Font.ITALIC, refFont);
- FontMetrics fontM = getFontMetrics(font);
-
- int fontH = fontM.getHeight();
- if(size().height > 10)
- fontH = refFont*(size().height - 10)/fontH;
- else
- fontH = 0;
-
- // calculate message length and X,Y position
- m_Font = new Font("TimesRoman", Font.BOLD + Font.ITALIC, fontH);
- FontMetrics fontMtx = getFontMetrics(m_Font);
- fontH = fontMtx.getHeight();
- m_MessageY = ((size().height-fontH) >> 1)+fontMtx.getAscent();
-
- m_MsgLength = 0;
- for(int i=0; i<m_astrMessages.length;i++)
- {
- m_MsgLengthVec[i] = fontMtx.stringWidth((String) m_astrMessages[i]);
- m_MsgLength += m_MsgLengthVec[i];
- }
- m_resetMessage = false;
- }//endif
- }//CreateParams
-
-
- /**
- * Calculate the next position of message
- * and call repaint
- */
-
- private synchronized void NextPos()
- {
- m_MessageX -= m_Speed;
- if ((m_MessageX + m_MsgLength) < 0)
- {
- m_endofdisplay = true;
- }
- if ((m_MessageX + m_MsgLength) < 0)
- {
- m_MessageX = m_PrevRec.width;
- }
- repaint();
- }
-
- protected String GetTimeDate()
- {
- String time = new String();
-
- String month[] = {"January","February","March","April","May","June",
- "July","August","September","October","November","December"};
-
- String day[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
- String dd = new String();
- String mm = new String();
- String yy = new String();
- String ddmmyy = new String();
- int min = 0;
- int pm= 0;
- Date date = new Date();
- int itemp = 0;
- String str = new String();
-
- // Get the current time!
- if(date.getHours() >= 12)
- pm = 1;
- else
- pm = 0;
-
- if(pm == 1)
- {
- itemp = date.getHours();
- if(itemp == 12)
- time = String.valueOf(12);
- else
- time = String.valueOf(date.getHours()-12);
- }
- else
- {
- itemp = date.getHours();
- if(itemp == 0)
- time = String.valueOf(12);
- else
- time = String.valueOf(itemp);
- }
-
- time = time.concat(":");
-
- min = date.getMinutes();
- if(min >= 10)
- time = time.concat(String.valueOf(min));
- else
- {
- time = time.concat("0");
- time = time.concat(String.valueOf(min));
- }
-
- if(pm == 1)
- time = time.concat(" pm ET ");
- else
- time = time.concat(" am ET ");
- dd = String.valueOf(date.getDate());
- mm = month[date.getMonth()];
- yy = String.valueOf(date.getYear() + 1900);
- str = str.concat(mm + " " + dd + ", " + yy );
- str = time.concat(str);
- return str;
- }// End GetTimeDate
-
-
- // --- Private attributes
-
- private Color m_TxtColor; // The Text Color
- private Color m_BgColor; // The background Color
- private Font m_Font; // The Font to be displayed
-
- private Dimension m_Size = new Dimension(); // Tne size of the ticker
- private Dimension m_PrevRec = new Dimension(-1,-1); // The rect previously used
-
- private String m_astrMessages[] = new String[4]; // The messages to be displayed
- private boolean m_endofdisplay = false; // The end of displaying the old messages
- private String m_astrNewMessages[] = new String[4]; // New messages needed to be displayed
- private int m_MsgLengthVec[] = new int[4]; // length of each message
-
- private int m_MessageX = 0; // x-position of the massage
- private int m_MessageY = 0; // y-position of the message
- private int m_MsgLength = 0; // Length of the the message
-
- private int m_Speed;
-
- private Image m_Image = null; // The offscreen image
- private Graphics m_Graphics = null; // The image's Graphics context
-
- private boolean m_ChangeMessage = false; // if message is changed
- private boolean m_resetMessage = false; // if message has been reset
- private boolean m_Action = true;
-
- } // Ticker
-
-