home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / ticker.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  11.3 KB  |  425 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.     @(#)PjTicker.java  0.00 8-Feb-96
  4.  
  5.         A prototype of a scrolling, word-wrapping text area.
  6.  
  7.     Authors:
  8.  
  9.         lning    Lindee Ning
  10.  
  11.     Version Ident:
  12.  
  13.         $Header:$
  14.  
  15.     History:
  16.  
  17.         0.00  8-Feb-96  lning   Initial Creation
  18.              25-Mar-96  jlee    Changed Ticker'super class from Panel to Canvas for
  19.                                 consistent use of Canvas component for drawing.
  20.              25-Mar-96  jlee    Changed ticker's height from 25 to 27
  21.              28-Mar-96  jlee    Used PjFinals for Ticker size
  22. ---------------------------------------------------------------------------*/
  23.  
  24. package pj.awt;
  25.  
  26. import pj.awt.PjFinals;
  27.  
  28. import java.awt.Canvas;
  29. import java.awt.Color;
  30. import java.awt.Dimension;
  31. import java.awt.Event;
  32. import java.awt.Font;
  33. import java.awt.FontMetrics;
  34. import java.awt.Graphics;
  35. import java.awt.Image;
  36.  
  37. import java.util.Date;
  38.  
  39. /**
  40.  * Display the default messages in a scrolling text area.
  41.  *
  42.  * @version 0.00 09-Feb-96
  43.  * @author Lindee Ning
  44. */
  45. public class Ticker extends Canvas implements Runnable
  46. {
  47.     // --- Public constructors: set default message, speed, text color, background color and size
  48.  
  49.     public Ticker()
  50.         {
  51.         m_astrMessages[0] = "Welcome to Personal Journal!  ...  ";
  52.         m_astrMessages[1] = "This Just In From Personal Journal  ...  ";
  53.         m_astrMessages[2] = "See This Just In For The Latest News Summaries  ...    At  ";
  54.         m_astrMessages[3] = GetTimeDate();
  55.  
  56.         m_Speed = 4;
  57.         m_MessageX = size().width;
  58.         m_TxtColor = Color.white;
  59.         m_BgColor = Color.black;
  60.         m_Size = PjFinals.dimTickerSize;
  61.         } // Ticker
  62.  
  63.     // --- Public operations
  64.  
  65.  
  66.     /**
  67.      * Set the message
  68.     */
  69.  
  70.     public void SetMessage(String str)
  71.         {
  72.         if(!(str == ""))
  73.             {
  74.             m_astrNewMessages[0] = "This Just In From Personal Journal  ...  See This Just In For The Latest News Summaries  ...   ";
  75.             m_astrNewMessages[1] = "Your Quotes (At Least 15-Minute Delayed):   ";
  76.             m_astrNewMessages[2] = str + "  ...   At  ";
  77.             m_astrNewMessages[3] = GetTimeDate();
  78.             m_ChangeMessage = true;
  79.             }
  80.         } //SetMessage
  81.  
  82.     /**
  83.      * Set the size
  84.     */
  85.  
  86.     public void SetSize(Dimension size)
  87.         {
  88.         m_Size = size;
  89.         } //SetSize
  90.  
  91.  
  92.     /**
  93.      * Set the speed
  94.     */
  95.     public void SetSpeed(int speed)
  96.         {
  97.         m_Speed = speed;
  98.         } //SetSpeed
  99.  
  100.     /**
  101.      * Set back the default speed
  102.     */
  103.     public void SetSpeed()
  104.         {
  105.         m_Speed = 4;
  106.         } //SetSpeed
  107.  
  108.  
  109.     /**
  110.      * Set the start x-position on the ticker
  111.     */
  112.     public void SetStartPosition(int x)
  113.         {
  114.         m_MessageX = x;
  115.         } //SetStartPosition
  116.  
  117.     /**
  118.      * Set back the default start x-position on the ticker
  119.     */
  120.     public void SetStartPosition()
  121.         {
  122.         m_MessageX = size().width;
  123.         } //SetStartPosition
  124.  
  125.  
  126.  
  127.     /**
  128.      * Set the background color
  129.     */
  130.  
  131.     public void SetBgColor(Color col)
  132.         {
  133.         m_BgColor = col;
  134.         } //SetBgColor
  135.  
  136.     /**
  137.      * Set the test color
  138.     */
  139.  
  140.     public void SetTxtColor(Color col)
  141.         {
  142.         m_TxtColor = col;
  143.         } //SetTxtColor
  144.  
  145.     /**
  146.      * Set the minimum size of the ticker
  147.     */
  148.  
  149.     public Dimension minimumSize()
  150.         {
  151.         return m_Size;
  152.         } //minimunSize
  153.  
  154.     /**
  155.      * Set the preferred size of the ticker
  156.     */
  157.  
  158.     public Dimension preferredSize()
  159.         {
  160.         return minimumSize();
  161.         } //preferredSize
  162.  
  163.     /**
  164.      * Get the number of messages being displayed
  165.     */
  166.  
  167.     public int GetNumMessages()
  168.         {
  169.         return m_astrMessages.length;
  170.         } //GetNumMessages
  171.  
  172.  
  173.     /**
  174.      * Catch the mouse clicking
  175.     */
  176.  
  177.     public boolean mouseDown(Event evt, int x, int y)
  178.         {
  179.         if(m_Action)
  180.             {
  181.             m_Action = false;
  182.             m_Speed = 0;
  183.             }
  184.         else
  185.             {
  186.             m_Action = true;
  187.             m_Speed = 4;
  188.             }
  189.         return true;
  190.         } //mouseDown
  191.  
  192.  
  193.     /**
  194.      * Run the loop.
  195.     */
  196.  
  197.     public void run()
  198.         {
  199.         while(true)
  200.             {
  201.             NextPos();
  202.             try {Thread.sleep(100);}
  203.             catch (InterruptedException e){ }
  204.             }
  205.         } //run
  206.  
  207.  
  208.     /**
  209.      * Paint
  210.     */
  211.  
  212.     public void paint(Graphics gr) { update(gr); }
  213.  
  214.     /**
  215.      *  Show the stuff
  216.     */
  217.  
  218.     public synchronized void update(Graphics gr)
  219.         {
  220.         CreateParams();
  221.  
  222.         // fill area with bgcolor
  223.         m_Graphics.setColor(m_BgColor);
  224.         m_Graphics.fillRect(0,0,m_PrevRec.width,m_PrevRec.height);
  225.         // draw the text
  226.         m_Graphics.setColor(m_TxtColor);
  227.         m_Graphics.setFont(m_Font);
  228.  
  229.         int head = m_MessageX;
  230.         int tail = 0;
  231.         for(int i=0;i<m_astrMessages.length;i++)
  232.         {
  233.             tail = head + m_MsgLengthVec[i];
  234.             if(! ((head > m_PrevRec.width) || (tail < 0)) )
  235.             {
  236.                         m_Graphics.drawString((String) m_astrMessages[i], head, m_MessageY);
  237.             }
  238.             head = tail + 2;
  239.         }
  240.  
  241.         // Display message on the screen
  242.         gr.drawImage(m_Image,0,0,this);
  243.  
  244.         } //update
  245.  
  246.  
  247.  
  248.     // --- Private operations
  249.  
  250.  
  251.     /**
  252.      * Create the image Parameters.
  253.     */
  254.     private void CreateParams()
  255.         {
  256.         // if size changed. re-create image
  257.         boolean bSizeChanged = false;
  258.         if ((size().height != m_PrevRec.height) ||
  259.             (size().width != m_PrevRec.width))
  260.             {
  261.             bSizeChanged = true;
  262.  
  263.             // delete old
  264.             if (m_Graphics != null) { m_Graphics.finalize(); }
  265.             if (m_Image != null)    { m_Image = null; }
  266.  
  267.             m_Image = createImage(size().width, size().height);
  268.             m_Graphics = m_Image.getGraphics();
  269.  
  270.  
  271.             m_PrevRec = size();
  272.             }
  273.  
  274.             // if msg or size changed, re-calculate each message length and font
  275.         if(m_ChangeMessage && m_endofdisplay)
  276.             {
  277.             m_astrMessages = m_astrNewMessages;
  278.             m_endofdisplay = false;
  279.             m_ChangeMessage = false;
  280.             m_resetMessage = true;
  281.             }
  282.         if( m_resetMessage || bSizeChanged )
  283.             {
  284.             // Select a proper font size
  285.             int refFont = 10;
  286.             Font font = new Font("TimesRoman", Font.BOLD + Font.ITALIC, refFont);
  287.             FontMetrics fontM = getFontMetrics(font);
  288.  
  289.             int fontH = fontM.getHeight();
  290.             if(size().height > 10)
  291.                 fontH = refFont*(size().height - 10)/fontH;
  292.             else
  293.             fontH = 0;
  294.  
  295.             // calculate message length and X,Y position
  296.             m_Font = new Font("TimesRoman", Font.BOLD + Font.ITALIC, fontH);
  297.             FontMetrics fontMtx = getFontMetrics(m_Font);
  298.             fontH = fontMtx.getHeight();
  299.             m_MessageY = ((size().height-fontH) >> 1)+fontMtx.getAscent();
  300.  
  301.             m_MsgLength = 0;
  302.             for(int i=0; i<m_astrMessages.length;i++)
  303.                 {
  304.                 m_MsgLengthVec[i] = fontMtx.stringWidth((String) m_astrMessages[i]);
  305.                 m_MsgLength += m_MsgLengthVec[i];
  306.                 }
  307.             m_resetMessage = false;
  308.             }//endif
  309.         }//CreateParams
  310.  
  311.  
  312.     /**
  313.      * Calculate the next position of message
  314.      * and call repaint
  315.     */
  316.  
  317.     private synchronized void NextPos()
  318.         {
  319.         m_MessageX -= m_Speed;
  320.         if ((m_MessageX + m_MsgLength) < 0)
  321.             {
  322.             m_endofdisplay = true;
  323.             }
  324.         if ((m_MessageX + m_MsgLength) < 0)
  325.             {
  326.             m_MessageX = m_PrevRec.width;
  327.             }
  328.         repaint();
  329.         }
  330.  
  331.     protected String GetTimeDate()
  332.         {
  333.         String time = new String();
  334.  
  335.         String month[] = {"January","February","March","April","May","June",
  336.                         "July","August","September","October","November","December"};
  337.  
  338.         String day[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
  339.         String dd = new String();
  340.         String mm = new String();
  341.         String yy = new String();
  342.         String ddmmyy = new String();
  343.         int min = 0;
  344.         int pm= 0;
  345.         Date date = new Date();
  346.         int itemp = 0;
  347.         String str = new String();
  348.  
  349.         // Get the current time!
  350.         if(date.getHours() >= 12)
  351.             pm = 1;
  352.         else
  353.             pm = 0;
  354.  
  355.         if(pm == 1)
  356.             {
  357.             itemp = date.getHours();
  358.             if(itemp == 12)
  359.                 time  = String.valueOf(12);
  360.             else
  361.                 time = String.valueOf(date.getHours()-12);
  362.             }
  363.         else
  364.             {
  365.             itemp = date.getHours();
  366.             if(itemp == 0)
  367.                 time = String.valueOf(12);
  368.             else
  369.                 time = String.valueOf(itemp);
  370.             }
  371.  
  372.         time = time.concat(":");
  373.  
  374.         min = date.getMinutes();
  375.         if(min >= 10)
  376.             time = time.concat(String.valueOf(min));
  377.         else
  378.             {
  379.             time = time.concat("0");
  380.             time = time.concat(String.valueOf(min));
  381.             }
  382.  
  383.         if(pm == 1)
  384.             time = time.concat(" pm ET ");
  385.         else
  386.             time = time.concat(" am ET ");
  387.         dd = String.valueOf(date.getDate());
  388.         mm = month[date.getMonth()];
  389.         yy = String.valueOf(date.getYear() + 1900);
  390.         str = str.concat(mm + " " + dd + ", " + yy );
  391.         str = time.concat(str);
  392.         return str;
  393.         }// End GetTimeDate
  394.  
  395.  
  396.     // --- Private attributes
  397.  
  398.     private Color m_TxtColor;                               // The Text Color
  399.     private Color m_BgColor;                                // The background Color
  400.     private Font  m_Font;                                           // The Font to be displayed
  401.  
  402.     private Dimension m_Size = new Dimension();         // Tne size of the ticker
  403.     private Dimension m_PrevRec = new Dimension(-1,-1); // The rect previously used
  404.  
  405.     private String m_astrMessages[] = new String[4];    // The messages to be displayed
  406.     private boolean m_endofdisplay = false;              // The end of displaying the old messages
  407.     private String m_astrNewMessages[] = new String[4]; // New messages needed to be displayed
  408.     private int m_MsgLengthVec[]   = new int[4];        // length of each message
  409.  
  410.     private int m_MessageX = 0;                                 // x-position of the massage
  411.     private int m_MessageY = 0;                                 // y-position of the message
  412.     private int m_MsgLength = 0;                                // Length of the the message
  413.  
  414.     private int m_Speed;
  415.  
  416.     private Image    m_Image = null;                    // The offscreen image
  417.     private Graphics m_Graphics = null;                 // The image's Graphics context
  418.  
  419.     private boolean m_ChangeMessage = false;            // if message is changed
  420.     private boolean m_resetMessage = false;                         // if message has been reset
  421.     private boolean m_Action = true;
  422.  
  423. } // Ticker
  424.  
  425.