hypernova 0 Newbie Poster

Hello Everyone !!

I read a program to create a simple moving banner from my book Java Complete Reference by Herbert Schildt, and after a few days I tried to make it myself from what I understood then.....but unfortunately mine doesnt work properly, even though I think I've done everything correctly, even though I wrote it a little differently. Here's my attempt:
(Please tell me what's wrong)

package Threads;

import java.applet.Applet;
import java.awt.*;

/*
<applet code="NewApplet.java" width=600 height=600>
*/
public class NewApplet extends Applet implements Runnable
{
    Thread t;
    String msg;
    boolean stop;
    public NewApplet()
    {
        t=new Thread(this);
        msg="Moving Banner.";
        stop=false;
    }

    public void run()
    {
        for(;;)
        {
            repaint();
            try
            {
                Thread.sleep(1000);
            }
            catch(InterruptedException e){}

            if(stop)
                break;
        }
    }
    public void init()
    {
        setBackground(Color.DARK_GRAY);
        setForeground(Color.WHITE);

    }
    public void start()
    {
        NewApplet o1=new NewApplet();
        o1.t.start();
        /*try
        {
            o1.t.join();
        }
        catch(InterruptedException e){}*/ //----causes the program to hang
    }
    
    public void paint(Graphics g)
    {
        g.drawString(msg,50,30);
        char c=msg.charAt(0);
        msg=msg.substring(1,msg.length());
        msg+=c;
    }
    public void stop()
    {
        stop=true;
        t=null;
        msg=null;
    }

    
}

OUTPUT: oving Banner.M

I am rather new to applets, so I may not have the proper flow of execution in my mind, but here's why I think my program should work:
1. First init() is called
2. Then start() is called, in which a constructor call is also made, which spawns a new thread besides initializing other things
3. new Thread is started by start()
4. Thus, run() is called
5. in run(), repaint() is called, which in turn calls paint()
6. paint prints (the first time) "Moving Banner." (which is the string in msg right now)
7. msg is changed to "oving Banner.M" after this. paint() ends.
8. Control returns to the infinite for loop, the thread sleeps for 1 sec. and then again repaint() is called, which calls paint()
9. "oving Banner.M" is printed and msg is changed to "ving Banner.Mo"
10. This happens indefinitely....
Seems right to me.....

And also, to give u an idea of what the program is supposed to do, here is the correct version of the program (from herbert Schildt):
As you will see, my program is much the same.....but mine doesnt work.....:-/

Also, another strange thing is noticed while trying to make my program work is that if I un-comment the o1.t.join() statement in start() method, the program hangs. Somehow, I think even init() is not called because the background is white (not DARK_GRAY)...why is this so ? (isnt init() called before start() ?)

package Threads;

import java.applet.Applet;
import java.awt.*;

/*
<applet code="NewApplet.java" width=600 height=600>
*/
public class NewApplet1 extends Applet implements Runnable
{
    Thread t=null;
    String msg="Moving Banner.";
    boolean stop;

    public void run()
    {
        char c;
        for(;;)
        {
            repaint();
            try
            {
                Thread.sleep(100);
            }
            catch(InterruptedException e){}
            c=msg.charAt(0);
            msg=msg.substring(1,msg.length());
            msg+=c;


            if(stop)
                break;
        }
    }
    public void init()
    {
        setBackground(Color.DARK_GRAY);
        setForeground(Color.WHITE);

    }
    public void start()
    {
        t=new Thread(this);
        stop=false;
        t.start();
    }

    public void paint(Graphics g)
    {
        g.drawString(msg,30,30);

    }
    public void stop()
    {
        stop=true;
        t=null;
        
    }
}

Any help is appreciated !!
Thank you in advance !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.