Applets doesn't close

  • Follow


Here is the code of an applet to show a face happy and sad at
intervals, I made.

import java.awt.*;
import java.applet.*;
//<applet code=Smiley width=r500 height=500>
public class Smiley extends Applet {
        boolean active=true;
        String msg = "Ravi";
        public void paint(Graphics g) {
                while(true)
                {
                g.drawString(msg,50,50);
                g.drawOval(100,100,220,200);
                g.drawOval(150,165,10,10);
                g.drawOval(250,165,10,10);
                if (active)
                {
                        g.clearRect(185,190,59,59);
                        g.drawArc(185,190,50,50,-30,-120);
                        active = false;
                }
                else
                {
                        g.clearRect(185,190,55,59);
                        g.drawArc(185,230,50,50,30,120);
                        active = true;
                }
                try {
                        Thread.sleep(1000);
                } catch (InterruptedException e) { }
                }
        }
        public void stop() {
                msg = "Java is everywhere";
        }
}

The problem is that applet refuses to close when I click on the close
button present on the title bar of the window.
I use jdk1.6 under Debian GNU/Linux.

0
Reply ra.ravi.rav (123) 3/29/2007 6:04:23 PM

Ravi wrote:
>         public void paint(Graphics g) {
>                 while(true)
>                 {

>                 }
>         }

> The problem is that applet refuses to close when I click on the close
> button present on the title bar of the window.

AWT processes events on a single thread (the Event Dispatch Thread or 
EDT). By not exiting from handling the repaint event, the close window 
events never have an opportunity to get through.

The easiest solution is to take the while loop and the sleep out of the 
paint. Use javax.swing.Timer (*not* java.util.Timer) to periodically 
call repaint on your component.

Tom Hawtin
0
Reply Tom 3/29/2007 7:00:13 PM


1 Replies
104 Views

(page loaded in 0.035 seconds)

Similiar Articles:













7/17/2012 5:59:47 AM


Reply: