Home   |  Viewing problem solution  |  About me


 

Java

 
I have written a Java applet, which works as a news ticker. The direction of the scroll is from bottom to up (by default). The applet is highly customizable. You can, by changing paramArgs value, incorporate as many URLs as desired. The index of the Text, URL pair however, begins with the 0 and not from 1, thus special care must be taken care while writing so. paramDelay can be used to control the speed of the ticker. The default values (for the arguments/parameters) will be overridden when you specify the new at the time of implementation. You can check out one implementation by looking at the source code of this page by going through View > Source Code. A sample run of this applet is being shown below for your reference.


Oops! your browser doesn't support Java. Please upgrade to Microsoft Internet Explorer 6 or later.


The source code of Skroll.java is as follows:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

/*
<code>
<applet code="Skroll" width="500" height="30">
<param name="paramArgs" value="2">
<param name="paramText0" value="Indian Oil Corporation Limited">
<param name="paramUrl0" value="http://www.indianoilcorp.com">
<param name="paramText1" value="Krishna Home Page">
<param name="paramUrl1" value="http://khatri-krishna.tripod.com">
<param name="paramDelay" value="2000">
<param name="paramDirection" value="Up">
</applet>
</code>
*/

public class Skroll extends Applet implements Runnable, MouseListener
{
        int arg, y, index, Delay;
        String Text[], Url[], Direction;
        Thread t = null;
        AppletContext ac;

        public void init()
        {
            //setBackground(Color.white);
            Direction = getParameter("paramDirection");
            addMouseListener(this);
            
            try
            {
                arg=Integer.parseInt(getParameter("paramArgs"));
                Delay = Integer.parseInt(getParameter("paramDelay"));
            }

            catch(NumberFormatException e)
            {

            }

            //Local vars to catch params
            String TextL[] = new String[arg];
            String UrlL[] = new String[arg]; 

            for (int i=0; i<arg; i++)
            {
                TextL[i] = getParameter("paramText" + i);
                UrlL[i] = getParameter("paramUrl" + i);
            }

            //Transferring values from local vars to the global vars
            Text = TextL;
            Url = UrlL;
        }

        public void start()
        {
            t = new Thread(this);
            ac = getAppletContext();
            index = 0;
            y = 50;
            t.start();
        }

        public void stop()
        {
        
        }

        public void run()
        {
            for(;;)
            {
                repaint(40,3,Integer.parseInt(getParameter("Width"))-80,20);

                if (Direction.equals("Up"))
                {
                    if (y <= -20)
                    {
                        y=40;
                        if (index == (arg-1))
                            index = 0;
                        else
                            index++;
                    }
                    else
                        y--;
                }
                else
                {
                    if (y >= 70)
                    {
                        y=0;
                        if (index == (arg-1))
                            index = 0;
                        else
                            index++;
                    }
                    else
                        y++;
                }

                if (y==18)
                {
                    try
                    {
                        Thread.sleep(Delay);
                    }

                    catch(InterruptedException e)
                    {

                    }
                }
                else
                {
                    try
                    {
                        Thread.sleep(30);
                    }

                    catch(InterruptedException e)
                    {

                    }
                }
            }
        }

        public void paint(Graphics g)
        {
            //setForeground(Color.black);
            g.drawRect(1,1,Integer.parseInt(getParameter("Width"))-2,25);
            //setForeground(Color.red);
            //g.fillRect(4,4,28,21);
            g.drawString(Text[index], 40, y);
        }

        public void mouseEntered(MouseEvent me)
        {
            //setBackground(Color.yellow);
            showStatus(Url[index]);
        }

        public void mouseExited(MouseEvent me)
        {
            //setBackground(Color.white);
            showStatus(" ");
        }

        public void mousePressed(MouseEvent me)
        {

        }

        public void mouseReleased(MouseEvent me)
        {

        }

        public void mouseClicked(MouseEvent me)
        {
            try
            {
                ac.showDocument(new URL(Url[index]));
            }

            catch(MalformedURLException e)
            {
                showStatus("URL not found");
            }
        }
}

All rights reserved. Copyright © 1999 - . Krishna Kumar Khatri.
Best viewed with Microsoft IE 6 or later, under 800x600 resolution with True Color (24-bit).