Link to home
Start Free TrialLog in
Avatar of suhani
suhani

asked on

Scrolling text

I have found an applet which display Fireworks. I would like to use it and have scrolling text displaying 'Welcome' with the Fireworks applet as my background. How do I do that?
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

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
Avatar of suhani
suhani

ASKER

you can get the source of the applet from the page above. Now everything depens on how much Java you know.
It is fairly easy to modify the applet (1-2 hours max) so to add the scrolling text. You probably can do it yourself. (if you need help - just ask). If you need me to do it, you'll have to describe EXACTLY what effect do you need, and increase the points to 100 ;-)

(of course I will help you doing this yourself, but I want more points for making it myself...)
Avatar of suhani

ASKER

OK, I'll increase your points to 100. Now can you please help me out. I need to get a word "WELCOME" scrolling vertically with the background of Firework applet.
;-)

ok - give me a few days ...
sorry, I couldn't find this question during the weekend (I searched for it in the opened questions :(( )

stupid ... :(

ok - I'll post the code before next Monday

hi again.
I am posting the modified version of the applet.

you can easy change the message text and the message speed (higher values mean slower speed) in the HTML file.

I'm posting the .java file and the .html file.
If you don't have the appropriate tools to compile .java file to .class file, please contact me at heyhey@nettaxi.com, I'll e-mail you the whole working applet.

-------------- start of file Firework.java -----------------
import java.awt.*;
import java.applet.*;
import java.lang.*;
import java.util.*;
import java.net.*;

public class Firework extends Applet implements Runnable
{
public int AnimationSpeed,          
    RocketStyleVariability,          
    MaxRocketNumber,          
    MaxRocketExplosionEnergy,          
    MaxRocketPatchNumber,          
    MaxRocketPatchLength,          
    Gravity;

public String RocketSoundtrack;
private int mx,my;
private Thread thread=null;
private Rocket rocket[];

String mes;
int textSpeed;
private VertScrollingText text;
Image offScreenImage;
Graphics offScreenGraphics;

public void init()  
{
    int i;  
      String p, p2, p3;  
      p=getParameter("AnimationSpeed");  
      AnimationSpeed=(p==null)?100:Integer.valueOf(p).intValue();  
    RocketSoundtrack=getParameter("RocketSoundtrack");  
    p=getParameter("RocketStyleVariability");  
    RocketStyleVariability=(p==null)?20:Integer.valueOf(p).intValue();  
    p=getParameter("MaxRocketNumber");  
    MaxRocketNumber=(p==null)?5:Integer.valueOf(p).intValue();  
    p=getParameter("MaxRocketExplosionEnergy");  
    MaxRocketExplosionEnergy=(p==null)?500:Integer.valueOf(p).intValue();  
    p=getParameter("MaxRocketPatchNumber");  
    MaxRocketPatchNumber=(p==null)?50:Integer.valueOf(p).intValue();  
    p=getParameter("MaxRocketPatchLength");  
    MaxRocketPatchLength=(p==null)?100:Integer.valueOf(p).intValue();  
    p=getParameter("Gravity");  
    Gravity=(p==null)?20:Integer.valueOf(p).intValue();  
    p=getParameter("TextMessage");  
    mes=(p==null)?"Welcome":p;
    p=getParameter("TextSpeed");  
    textSpeed=(p==null)?3:Integer.valueOf(p).intValue();

    mx=size().width-1;  
    my=size().height-1;  
    rocket=new Rocket[MaxRocketNumber];  

    for(i=0;i<MaxRocketNumber;i++)
        rocket[i]=new Rocket(mx,my,Gravity);  
       
    text = new VertScrollingText(mes, new Font("Serif", Font.BOLD, 24), Color.cyan, textSpeed);
    Rectangle rect = this.getBounds();
    text.init(rect);
    offScreenImage = createImage(rect.width, rect.height);
    offScreenGraphics = offScreenImage.getGraphics();
}


public void start()  
{  
    if(thread==null)      
    {      
        thread=new Thread(this);      
        thread.start();      
    }  
}

public void stop()  
{  
    if(thread!=null)      
    {      
        thread.stop();      
        thread=null;      
    }  
}

public void run()  
{  
    int i,      
    e=(int)(Math.random()*MaxRocketExplosionEnergy*3/4)+         MaxRocketExplosionEnergy/4+1,      
    p=(int)(Math.random()*MaxRocketPatchNumber*3/4)+         MaxRocketPatchNumber/4+1,      
    l=(int)(Math.random()*MaxRocketPatchLength*3/4)+         MaxRocketPatchLength/4+1;  
    long s=(long)(Math.random()*10000);  
    boolean sleep;  
    Graphics g=getGraphics();  
    URL u=null;  

    while(true)      
    {      
        try
        {          
            thread.sleep(100/AnimationSpeed);          
        }      
        catch(InterruptedException x){}      

        sleep=true;      
        for(i=0;i<MaxRocketNumber;i++)        
            sleep=sleep&&rocket[i].sleep;      
           
        if(sleep&&Math.random()*100<RocketStyleVariability)        
        {        
            e=(int)(Math.random()*MaxRocketExplosionEnergy*3/4)+           MaxRocketExplosionEnergy/4+1;        
            p=(int)(Math.random()*MaxRocketPatchNumber*3/4)+           MaxRocketPatchNumber/4+1;        
            l=(int)(Math.random()*MaxRocketPatchLength*3/4)+           MaxRocketPatchLength/4+1;        
             s=(long)(Math.random()*10000);        
        }
       
       
        offScreenGraphics.setColor(Color.black);  
        offScreenGraphics.fillRect(0,0,mx+1,my+1);  
        text.show(offScreenGraphics);
       
        for(i=0;i<MaxRocketNumber;i++)        
        {        
            if(rocket[i].sleep&&Math.random()*MaxRocketNumber*l<1)  
            {            
                try
                {                
                    u=new URL(getDocumentBase(),RocketSoundtrack);
                }            
                catch(MalformedURLException x){}            
                play(u);            
                rocket[i].init(e,p,l,s);            
                rocket[i].start();            
            }        
        rocket[i].show(offScreenGraphics);        
        }      
    g.drawImage(offScreenImage, 0, 0, null);
    }  
}


public void paint(Graphics g)  
{  
    g.setColor(Color.black);  
    g.fillRect(0,0,mx+1,my+1);  
}

}


class Rocket
{
public boolean sleep=true;
private int energy,patch,length,            
mx,my,            gravity,            
ox,oy,            vx[],vy[],            
x,y,            red,blue,green,            t;

private Random random;

public Rocket(int a,int b,int g)  
{  
    mx=a;  
    my=b;  
    gravity=g;  
}

public void init(int e,int p,int l,long seed)  
{  
    int i;  
    energy=e;  
    patch=p;  
    length=l;  
    random=new Random(seed);  
    vx=new int[patch];  
    vy=new int[patch];  
    red=(int)(random.nextDouble()*128)+128;  
    blue=(int)(random.nextDouble()*128)+128;  
    green=(int)(random.nextDouble()*128)+128;  
    ox=(int)(Math.random()*mx/2)+mx/4;  
    oy=(int)(Math.random()*my/2)+my/4;  
    for(i=0;i<patch;i++)      
    {      
        vx[i]=(int)(Math.random()*energy)-energy/2;      
        vy[i]=(int)(Math.random()*energy*7/8)-energy/8;      
    }  
}

public void start()  
{  
    t=0;  
    sleep=false;  
}

public void show(Graphics g)  
{  
    if(!sleep)      
    if(t<length)        
    {        
        int i,c;        
        double s;        
        Color color;        
        c=(int)(random.nextDouble()*64)-32+red;        
        if(c>=0&&c<256)red=c;        
        c=(int)(random.nextDouble()*64)-32+blue;        
        if(c>=0&&c<256)blue=c;        
        c=(int)(random.nextDouble()*64)-32+green;        
        if(c>=0&&c<256)green=c;        
        color=new Color(red,blue,green);        
        for(i=0;i<patch;i++)            
        {            
            s=(double)t/100;            
            x=(int)(vx[i]*s);            
            y=(int)(vy[i]*s-gravity*s*s);            
            g.setColor(color);            
            g.drawLine(ox+x,oy-y,ox+x,oy-y);            
            if(t>=length/2)              
            {              
                int j;              
                for(j=0;j<2;j++)                  
                {                  
                    s=(double)((t-length/2)*2+j)/100;                  
                    x=(int)(vx[i]*s);                  
                    y=(int)(vy[i]*s-gravity*s*s);                  
                    g.setColor(Color.black);                  
                    g.drawLine(ox+x,oy-y,ox+x,oy-y);                  
                }              
            }            
        }        
        t++;        
    }      
    else        
    {        
       sleep=true;        
    }  
}
}


class VertScrollingText
{
public boolean sleep=true;
private String mes;
private Font font;
private Color color;

private int strWidth, strHeight;
private int width, height;
private int curX, curY;
private int textSpeed;
private int t;

public VertScrollingText (String theMes, Font theFont, Color theColor, int theSpeed)
{
    mes = theMes;
    font = theFont;
    color = theColor;
    textSpeed = theSpeed;
    t = theSpeed;
    FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics (font);
    strHeight = fm. getHeight();
    strWidth = fm.stringWidth(mes);
}

public void init(Rectangle size)
{
    width = size.width;
    height = size.height;
   
    curX = (width - strWidth) / 2;
    curY = height + strHeight ;
}

public void show(Graphics g)
{
    if (curY > -strHeight)
    {    
        g.setFont(font);
        g.setColor(color);
        g.drawString(mes, curX, curY);
        t--;
        if (t == 0)
        {
            t = textSpeed;
            curY--;
        }
    }
    else
    {
        curY = height + strHeight ;
    }
}

}
-------------- end of file Firework.java -----------------

-------------- start of file Firework.html -----------------
<HTML>
<head>
<title>test page </title>
</head>

<body bgcolor="#ffffff" text="#000000" link="#cc0000" vlink="#999999">

<center><h1>new Firework</h1>
<hr>
<APPLET CODE="Firework.class" WIDTH="399" HEIGHT="349">
<PARAM NAME="AnimationSpeed" VALUE="50">
<PARAM NAME="RocketSoundtrack" VALUE="fire.au">
<PARAM NAME="RocketStyleVariability" VALUE="10">
<PARAM NAME="MaxRocketNumber" VALUE="9">
<PARAM NAME="MaxRocketExplosionEnergy" VALUE="850">
<PARAM NAME="MaxRocketPatchNumber" VALUE="90">
<PARAM NAME="MaxRocketPatchLength" VALUE="68">
<PARAM NAME="Gravity" VALUE="400">
<PARAM NAME="TextMessage" VALUE="Welcome suhani">
<PARAM NAME="TextSpeed" VALUE="4">

</applet>
<hr></center>
<b>Usage:</b>
Place <a href="Firework.class" target="_top"><b>Firework.class</a></b> ,
<a href="Rocket.class" target="_top"><b>Rocket.class</a></b> and
<a href="VertScrollingText.class" target="_top"><b>VertScrollingText.class</a></b>
(along with <b><a href="fire.au">fire.au</a></b>) within your HTML directory.
<p>
<b>Java Source:</B>  <a href="Firework.java" target="_top">Firework.java</a><p>
<B>HTML Source:</B>
<pre>&lt;APPLET CODE="Firework.class" WIDTH="399" HEIGHT="349"&gt;
&lt;PARAM NAME="AnimationSpeed" VALUE="50"&gt;
&lt;PARAM NAME="RocketSoundtrack" VALUE="fire.au"&gt;
&lt;PARAM NAME="RocketStyleVariability" VALUE="10"&gt;
&lt;PARAM NAME="MaxRocketNumber" VALUE="9"&gt;
&lt;PARAM NAME="MaxRocketExplosionEnergy" VALUE="850"&gt;
&lt;PARAM NAME="MaxRocketPatchNumber" VALUE="90"&gt;
&lt;PARAM NAME="MaxRocketPatchLength" VALUE="68"&gt;
&lt;PARAM NAME="Gravity" VALUE="400"&gt;
&lt;PARAM NAME="TextMessage" VALUE="Welcome !!!"&gt;
&lt;PARAM NAME="TextSpeed" VALUE="11"&gt;
&lt;/applet&gt;</pre>
<P>
<B>Author:</B>  David O'Brien
<P>
<B>Modified:</B>  heyhey


<P>
<hr>

</BODY>
</HTML>
-------------- end of file Firework.html -----------------

best regards
  heyhey
Avatar of suhani

ASKER

Wonderful! Thank you. However, I need one more favour. How do you make the messages to be scrolled in different lines and each lines has different font and colors.
take a better look at the VertScrollingText class

 VertScrollingText (String theMes, Font theFont, Color theColor, int theSpeed)

the message nad the color are filled in the constructor !!!

and all the moving algorithm and drawing is in te show() method

you can write your own HorzScrollingText class - it's fairly easy.

you can create more than one textScrolling object and add it to the applet - you'll need only to call their COnstructo / init / show method.

(take a look at the third line - it makes the real drawing of the Scroling text in the applet
>> offScreenGraphics.setColor(Color.black);    
>> offScreenGraphics.fillRect(0,0,mx+1,my+1);    
>> text.show(offScreenGraphics);
)