Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2007

Tip: Looking for answers? Try searching our database.

Applets doesn't close

Thread view: 
Ravi - 29 Mar 2007 19:04 GMT
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.
Tom Hawtin - 29 Mar 2007 20:00 GMT
>         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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.