[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Moving a text from left to right using applets

Posted on 2003-03-24
11
Medium Priority
?
227 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:karthickkrishnan
  • 5
  • 3
  • 3
11 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8194592
0
 
LVL 3

Expert Comment

by:msterjev
ID: 8194651
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>
0
 

Author Comment

by:karthickkrishnan
ID: 8195056
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
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 35

Expert Comment

by:girionis
ID: 8195091
 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.
0
 

Author Comment

by:karthickkrishnan
ID: 8195666
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
0
 
LVL 35

Expert Comment

by:girionis
ID: 8195778
 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.
0
 
LVL 3

Expert Comment

by:msterjev
ID: 8197140
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);
    }
0
 
LVL 3

Expert Comment

by:msterjev
ID: 8197158
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!
0
 
LVL 3

Accepted Solution

by:
msterjev earned 200 total points
ID: 8200726
Here is a complete code:

TextScroller.java

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

public class TextScroller extends Applet implements Runnable
{
     private String filename,scrollText;
     private int width,height,strWidth,y,pos,speed;
     private Thread animator;
     private Font font;
     
     public void init()
     {
          filename=getParameter("filename");
          try
          {
               URL url=new URL(getCodeBase().toExternalForm()+filename);
               URLConnection con=(URLConnection)url.openConnection();
               BufferedReader in=new BufferedReader(new InputStreamReader(con.getInputStream()));
               String line;
               StringBuffer sb=new StringBuffer();
               while((line=in.readLine())!=null)
               {
                    sb.append(line);
               }
               scrollText=sb.toString();
          }
          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);
          setBackground(Color.gray);
     }

     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();
          if(bufferGraphics!=null)
          {
               bufferGraphics.setColor(Color.cyan);
               bufferGraphics.drawString(scrollText,pos,(height+y)/2);
               bufferGraphics.setColor(Color.blue);
               bufferGraphics.drawString(scrollText,pos+1,(height+y)/2+1);
               g.drawImage(bufferImage,0,0,width,height,null);
          }
     }
}

Test.htm

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
</HEAD>
<BODY>

<P>&nbsp;</P>
<APPLET  codebase="." code="TextScroller" width=100% height=50>
<PARAM NAME=filename VALUE=text.txt>
<PARAM NAME=speed VALUE=100>
</APPLET>
</BODY>
</HTML>

Text.txt

Hello karthickkrishnan!!!

Put the compiled class and Test.htm,Text.txt into the same directory!
0
 

Author Comment

by:karthickkrishnan
ID: 8208992
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
0
 
LVL 3

Expert Comment

by:msterjev
ID: 8209875
Thank you!
I will try to do my best in answering your questions!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month7 days, 16 hours left to enroll

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question