Link to home
Start Free TrialLog in
Avatar of karthickkrishnan
karthickkrishnanFlag for India

asked on

Moving a text from left to right using applets

In my application, i m making use of java applets.My requirement is
 i ve to display a text that moves from the left to right of the applet window..
 the problem is ...   the text which is going to move from the left to right is not a static one. i m supposed to have a text file..
   All i will do in the text file. is. i ll type what ever text i need to display in  the applet.. and save that text file.
   
   My applet should get the text from the text file and should display the text from left to right of the applet window.
   
   This is very urgent requirement..

please provide me a solution.
regards
karthick
Avatar of girionis
girionis
Flag of Greece image

Avatar of msterjev
msterjev

import java.awt.*;
import java.applet.*;

public class TextScroller extends Applet implements Runnable
{
     private String scrollText;
     private int width,height,strWidth,y,pos,speed;
     private Thread animator;
     private Font font;
     public void init()
     {
          scrollText=getParameter("text");
          if(scrollText==null)
               scrollText="Enter scroll text!";
          try
          {
               speed=Integer.parseInt(getParameter("speed"));
          }
          catch(Exception e)
          {
               speed=100;
          }
          width=getSize().width;
          height=getSize().height;
          pos=width;
          font=new Font("Arial",Font.BOLD,18);
          setFont(font);
     }

     public void start()
     {
          FontMetrics fm=getGraphics().getFontMetrics();
          y=fm.getHeight();
          strWidth=-fm.stringWidth(scrollText);
          animator=new Thread(this);
          animator.start();
     }
     
     public void stop()
     {
          if(animator!=null)
          {
               animator.stop();
          }
     }
     
     public void run()
     {
          if(Thread.currentThread()!=animator)
               return;
          while(true)
          {
               try
               {
                    animator.sleep(speed);
                    repaint();
                    pos--;
                    if(pos<strWidth)
                         pos=width;
               }
               catch(Exception e)
               {
               }
          }
     }
     
     public void update(Graphics g)
     {
          paint(g);
     }
     public void paint(Graphics g)
     {
          Image bufferImage=this.createImage(width,height);
          Graphics bufferGraphics=bufferImage.getGraphics();
          bufferGraphics.drawString(scrollText,pos,(height+y)/2);
          g.drawImage(bufferImage,0,0,width,height,null);
     }
}



<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>

<P>&nbsp;</P>
<APPLET  codebase="." code="TextScroller" width=400 height=100>
<PARAM NAME=speed VALUE=10>
</APPLET>
</BODY>
</HTML>
Avatar of karthickkrishnan

ASKER

Dear Experts,
 the code which msterjev has given is really useful and it has matched 80% of my requirement..
My actual requirement is , i ve to read the text which i m going to display in the applet from a txt file
where as what Mr.msterjev has suggested is
to get the text from the applet itself..

My requirement is
 I need to read the text from a file and that text needs to b displayed in the applet screen.
please provide me the solution.
regards
karthick
 You need to open a conenction to the server. If the file is on a web server just do:

URLConnection urlconnection = (new URL(<web server url>)).openConnection();
urlconnection.connect();
DataInputStream datainputstream = new DataInputStream(urlconnection.getInputStream());
String s;
while((s = datainputstream.readLine()) != null)
{
    System.out.println(s);
}

  If it resides on a TCP/IP server you have to open a socket conenction.
the file is on the tomcat server..
I will b reading that file to display the text on the applet
more over, i will be changing the text file as my wish..
so whenever there is a change in the text file..that should be reflected in my applet
please provide me a solution
regards
karthick
 If you use my code you should do whatever you want. Btw did you want the text from left to right or from right to left? I am a bit confused here. If the text comes from the right hand side then it is scrolled from right to left. If it comes from the left hand side then it is scrolled from left-to-right.
Move the girionis code into the init() method and you have the complete solution!

public void init()
    {
         scrollText="";
         try
         {
         }
         catch(Exception e)
         {

         try
         {
              speed=Integer.parseInt(getParameter("speed"));
         }
         catch(Exception e)
         {
              speed=100;
         }
         width=getSize().width;
         height=getSize().height;
         pos=width;
         font=new Font("Arial",Font.BOLD,18);
         setFont(font);
    }
Move the girionis code into the init() method and you have the complete solution!

public void init()
    {
         scrollText="";

         try
         {
            URLConnection urlconnection = (new URL(<web server url>)).openConnection();
urlconnection.connect();
DataInputStream datainputstream = new DataInputStream(urlconnection.getInputStream());
String s;
while((s = datainputstream.readLine()) != null)
{
   scrollText+=s;
}

         }
         catch(Exception e)
         {
           scrollText=e.getMessage();
         }
         try
         {
              speed=Integer.parseInt(getParameter("speed"));
         }
         catch(Exception e)
         {
              speed=100;
         }
         width=getSize().width;
         height=getSize().height;
         pos=width;
         font=new Font("Arial",Font.BOLD,18);
         setFont(font);
    }


You can use StringBuffer for performace enhancement!
ASKER CERTIFIED SOLUTION
Avatar of msterjev
msterjev

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dear MsterJev,
  Your solutions was really superb and i m very much pleased with the way u ve answered..
Hope to get more solutions like this..from u.
Thank YOu
regards
karthickkrishnan
Thank you!
I will try to do my best in answering your questions!