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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...)
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...)
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 ...
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
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("AnimationS peed");
AnimationSpeed=(p==null)?1 00:Integer .valueOf(p ).intValue ();
RocketSoundtrack=getParame ter("Rocke tSoundtrac k");
p=getParameter("RocketStyl eVariabili ty");
RocketStyleVariability=(p= =null)?20: Integer.va lueOf(p).i ntValue();
p=getParameter("MaxRocketN umber");
MaxRocketNumber=(p==null)? 5:Integer. valueOf(p) .intValue( );
p=getParameter("MaxRocketE xplosionEn ergy");
MaxRocketExplosionEnergy=( p==null)?5 00:Integer .valueOf(p ).intValue ();
p=getParameter("MaxRocketP atchNumber ");
MaxRocketPatchNumber=(p==n ull)?50:In teger.valu eOf(p).int Value();
p=getParameter("MaxRocketP atchLength ");
MaxRocketPatchLength=(p==n ull)?100:I nteger.val ueOf(p).in tValue();
p=getParameter("Gravity");
Gravity=(p==null)?20:Integ er.valueOf (p).intVal ue();
p=getParameter("TextMessag e");
mes=(p==null)?"Welcome":p;
p=getParameter("TextSpeed" );
textSpeed=(p==null)?3:Inte ger.valueO f(p).intVa lue();
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()*MaxR ocketExplo sionEnergy *3/4)+ MaxRocketExplosionEnergy/4 +1,
p=(int)(Math.random()*MaxR ocketPatch Number*3/4 )+ MaxRocketPatchNumber/4+1,
l=(int)(Math.random()*MaxR ocketPatch Length*3/4 )+ MaxRocketPatchLength/4+1;
long s=(long)(Math.random()*100 00);
boolean sleep;
Graphics g=getGraphics();
URL u=null;
while(true)
{
try
{
thread.sleep(100/Animation Speed);
}
catch(InterruptedException x){}
sleep=true;
for(i=0;i<MaxRocketNumber; i++)
sleep=sleep&&rocket[i].sle ep;
if(sleep&&Math.random()*10 0<RocketSt yleVariabi lity)
{
e=(int)(Math.random()*MaxR ocketExplo sionEnergy *3/4)+ MaxRocketExplosionEnergy/4 +1;
p=(int)(Math.random()*MaxR ocketPatch Number*3/4 )+ MaxRocketPatchNumber/4+1;
l=(int)(Math.random()*MaxR ocketPatch Length*3/4 )+ MaxRocketPatchLength/4+1;
s=(long)(Math.random()*100 00);
}
offScreenGraphics.setColor (Color.bla ck);
offScreenGraphics.fillRect (0,0,mx+1, my+1);
text.show(offScreenGraphic s);
for(i=0;i<MaxRocketNumber; i++)
{
if(rocket[i].sleep&&Math.r andom()*Ma xRocketNum ber*l<1)
{
try
{
u=new URL(getDocumentBase(),Rock etSoundtra ck);
}
catch(MalformedURLExceptio n x){}
play(u);
rocket[i].init(e,p,l,s);
rocket[i].start();
}
rocket[i].show(offScreenGr aphics);
}
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.nextDoubl e()*128)+1 28;
blue=(int)(random.nextDoub le()*128)+ 128;
green=(int)(random.nextDou ble()*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)-en ergy/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+r ed;
if(c>=0&&c<256)red=c;
c=(int)(random.nextDouble( )*64)-32+b lue;
if(c>=0&&c<256)blue=c;
c=(int)(random.nextDouble( )*64)-32+g reen;
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( ).getFontM etrics (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="RocketStyleVariabili ty" VALUE="10">
<PARAM NAME="MaxRocketNumber" VALUE="9">
<PARAM NAME="MaxRocketExplosionEn ergy" 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.cl ass</a></b > and
<a href="VertScrollingText.cl ass" target="_top"><b>VertScrol lingText.c lass</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.jav a</a><p>
<B>HTML Source:</B>
<pre><APPLET CODE="Firework.class" WIDTH="399" HEIGHT="349">
<PARAM NAME="AnimationSpeed" VALUE="50">
<PARAM NAME="RocketSoundtrack" VALUE="fire.au">
<PARAM NAME="RocketStyleVariabili ty" VALUE="10">
<PARAM NAME="MaxRocketNumber" VALUE="9">
<PARAM NAME="MaxRocketExplosionEn ergy" VALUE="850">
<PARAM NAME="MaxRocketPatchNumber " VALUE="90">
<PARAM NAME="MaxRocketPatchLength " VALUE="68">
<PARAM NAME="Gravity" VALUE="400">
<PARAM NAME="TextMessage" VALUE="Welcome !!!">
<PARAM NAME="TextSpeed" VALUE="11">
</applet></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
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("AnimationS
AnimationSpeed=(p==null)?1
RocketSoundtrack=getParame
p=getParameter("RocketStyl
RocketStyleVariability=(p=
p=getParameter("MaxRocketN
MaxRocketNumber=(p==null)?
p=getParameter("MaxRocketE
MaxRocketExplosionEnergy=(
p=getParameter("MaxRocketP
MaxRocketPatchNumber=(p==n
p=getParameter("MaxRocketP
MaxRocketPatchLength=(p==n
p=getParameter("Gravity");
Gravity=(p==null)?20:Integ
p=getParameter("TextMessag
mes=(p==null)?"Welcome":p;
p=getParameter("TextSpeed"
textSpeed=(p==null)?3:Inte
mx=size().width-1;
my=size().height-1;
rocket=new Rocket[MaxRocketNumber];
for(i=0;i<MaxRocketNumber;
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()*MaxR
p=(int)(Math.random()*MaxR
l=(int)(Math.random()*MaxR
long s=(long)(Math.random()*100
boolean sleep;
Graphics g=getGraphics();
URL u=null;
while(true)
{
try
{
thread.sleep(100/Animation
}
catch(InterruptedException
sleep=true;
for(i=0;i<MaxRocketNumber;
sleep=sleep&&rocket[i].sle
if(sleep&&Math.random()*10
{
e=(int)(Math.random()*MaxR
p=(int)(Math.random()*MaxR
l=(int)(Math.random()*MaxR
s=(long)(Math.random()*100
}
offScreenGraphics.setColor
offScreenGraphics.fillRect
text.show(offScreenGraphic
for(i=0;i<MaxRocketNumber;
{
if(rocket[i].sleep&&Math.r
{
try
{
u=new URL(getDocumentBase(),Rock
}
catch(MalformedURLExceptio
play(u);
rocket[i].init(e,p,l,s);
rocket[i].start();
}
rocket[i].show(offScreenGr
}
g.drawImage(offScreenImage
}
}
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.nextDoubl
blue=(int)(random.nextDoub
green=(int)(random.nextDou
ox=(int)(Math.random()*mx/
oy=(int)(Math.random()*my/
for(i=0;i<patch;i++)
{
vx[i]=(int)(Math.random()*
vy[i]=(int)(Math.random()*
}
}
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(
if(c>=0&&c<256)red=c;
c=(int)(random.nextDouble(
if(c>=0&&c<256)blue=c;
c=(int)(random.nextDouble(
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*
g.setColor(color);
g.drawLine(ox+x,oy-y,ox+x,
if(t>=length/2)
{
int j;
for(j=0;j<2;j++)
{
s=(double)((t-length/2)*2+
x=(int)(vx[i]*s);
y=(int)(vy[i]*s-gravity*s*
g.setColor(Color.black);
g.drawLine(ox+x,oy-y,ox+x,
}
}
}
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(
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="RocketStyleVariabili
<PARAM NAME="MaxRocketNumber" VALUE="9">
<PARAM NAME="MaxRocketExplosionEn
<PARAM NAME="MaxRocketPatchNumber
<PARAM NAME="MaxRocketPatchLength
<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.
<a href="Rocket.class" target="_top"><b>Rocket.cl
<a href="VertScrollingText.cl
(along with <b><a href="fire.au">fire.au</a>
<p>
<b>Java Source:</B> <a href="Firework.java" target="_top">Firework.jav
<B>HTML Source:</B>
<pre><APPLET CODE="Firework.class" WIDTH="399" HEIGHT="349">
<PARAM NAME="AnimationSpeed" VALUE="50">
<PARAM NAME="RocketSoundtrack" VALUE="fire.au">
<PARAM NAME="RocketStyleVariabili
<PARAM NAME="MaxRocketNumber" VALUE="9">
<PARAM NAME="MaxRocketExplosionEn
<PARAM NAME="MaxRocketPatchNumber
<PARAM NAME="MaxRocketPatchLength
<PARAM NAME="Gravity" VALUE="400">
<PARAM NAME="TextMessage" VALUE="Welcome !!!">
<PARAM NAME="TextSpeed" VALUE="11">
</applet></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
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.bla ck);
>> offScreenGraphics.fillRect (0,0,mx+1, my+1);
>> text.show(offScreenGraphic s);
)
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
>> offScreenGraphics.fillRect
>> text.show(offScreenGraphic
)
ASKER
http://javaboutique.internet.com/Firework/