Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Moving a text from left to right using applets

Posted on 2003-03-24
11
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

722 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