0

I was wondering how to create a moving banner using Java Applets without threads. I got it this far but the program doesn't repaint.

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

/*<applet code="bt" height=300 width=300></applet>*/

public class bt extends Applet{
    String msg="This banner is soo cool!!!";
    String hm;
    char c;

    public void init(){
        setBackground(Color.cyan);
        setForeground(Color.red);
    }

    public String bann(String mg){
        c=mg.charAt(0);
        hm=mg.substring(1,mg.length());
        hm+=c;
        repaint();

        return hm;
    }

    public void paint(Graphics g){

        try{
            for( ; ; ){
                g.drawString(msg,100,100);
                Thread.sleep(2000);
                msg=bann(msg);
            }
        } catch(InterruptedException e) {}
    }
} 

Moreover, i can't close the window by just clicking exit. I have to force Quit it. It would be nice if someone could explain why.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"Creating a moving banner using a Java Applet without threads" Why "without threads"? It is the correct way to do this. The way the code is currently approaching it will block the EDT & is set to fail. General advice: 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and .. – Andrew Thompson Jun 04 '17 at 02:24
  • .. [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/moving-to-a-plugin-free-web). 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. 4) Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. – Andrew Thompson Jun 04 '17 at 02:24

0 Answers0