Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java ScrollBanner

 
Brijesh Shah
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI ,

I have a code here below:
import java.awt.*;
import java.applet.*;
/*
<applet code="ScrollBanner" width=600 height=300>
<param name=message1 value="A Simple Banner Number ONE. ">
<param name=message2 value="A Simple Banner Number TWO. ">
</applet>
*/
public class ScrollBanner extends Applet implements Runnable
{
String msg1, msg2;
Font f;
int x1=100, y1=50, x2=100, y2=200;
Thread t=null;
boolean stopFlag;

// Set font, colors and initialize the applet thread and prepare to start
public void init()
{
msg1=getParameter("message1");
if (msg1 == null)
{
msg1="Missing parameter message.";
}
msg1=" "+msg1;
msg2=getParameter("message2");
if (msg2 == null)
{
msg2="Missing parameter message.";
}
msg2=" "+msg2;
f=new Font("Arial, Helvetica", Font.BOLD,18);
setFont(f);
setBackground(Color.yellow);
setForeground(Color.red);
}// closing init()

// Start the scroll banner applet running, creating a new thread as necessary
public void start()
{
t=new Thread(this);
stopFlag=false;
t.start();
}// closing start()

/** Entry point for the thread that runs the banner. This method
* repaints the message repeatedly, which will cause it to move across
* the display. To save CPU, it sleeps between repaint operations.
*/
public void run()
{
char ch1,ch2;

// Display the banner moving
for ( ; ; )
{
try
{
repaint();
Thread.sleep(250);
ch1=msg1.charAt(0);
msg1=msg1.substring(1,msg1.length());
msg1=msg1+ch1;
if(stopFlag)
break;
}
catch(InterruptedException e)
{
// Prints this throwable and its backtrace to the standard error stream (System.err)
e.printStackTrace();
}
try
{
//repaint();
Thread.sleep(250);
ch2=msg2.charAt(0);
msg2=msg2.substring(1,msg2.length());
msg2=msg2+ch2;
if(stopFlag)
break;
}
catch(InterruptedException e)
{
// Prints this throwable and its backtrace to the standard error stream (System.err)
e.printStackTrace();
}
}// closing for()
}// closing run()

// Stop the scroll banner applet running
public void stop()
{
stopFlag=true;
t=null;
}// closing stop()

// Paint the applet with the message text
public void paint(Graphics g)
{
g.drawString(msg1,x1,y1);
g.drawString(msg2,x2,y2);
}// closing paint()
}// closing class SimpleBanner

In the above code I want to move 2 applet banners one moving from left to right and another right to left .
I am having trouble for coding for the banner moving from left to right.
Could you please assist...
Thanks in advance!!!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After a quick look at this ... are they both moving right to left now? Looks like removing the first character and adding it to the end works for that. The opposite would take the last character and move it to the front. Look carefully at the arguments for substring and look into length.
 
Brijesh Shah
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

If I interchange the substring statement it isn't working:

try
{
//repaint();
Thread.sleep(250);
ch2=msg2.charAt(0);
msg2=msg2.substring(msg2.length(),1);
msg2=msg2+ch2;
if(stopFlag)
break;
}
catch(InterruptedException e)
{
// Prints this throwable and its backtrace to the standard error stream (System.err)
e.printStackTrace();
}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ch2=msg2.charAt(0);//you need the last character

msg2=msg2.substring(msg2.length(),1);//problem, see below

msg2=msg2.substring(1,msg2.length());//from first post
this line creates a string, excluding the first char
you want the excluded char to be the last
(hint: deduct 1 from both above args)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic