Results 1 to 4 of 4

Thread: strange code

  1. #1
    tghn2b is offline Member
    Join Date
    Jan 2008
    Posts
    31
    Rep Power
    0

    Default strange code

    well not really strange, but but it is when I got up to speed faster with C/C++

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.util.Date;
    import java.text.DateFormat;
    				/** An Applet to display the current time */
    public class TimeDate extends Applet implements Runnable {
     				  /** A Thread to run the timer */
       protected Thread timerThread;		   /** The date object */
       Date date = new Date();				   /** The date format */
       protected DateFormat format = DateFormat.getTimeInstance();
    
       			/* Applet Lifestyle Methods */
       public void start() {
          timerThread = new Thread(this, "Clock");
          timerThread.start();
       }
    
       public void stop() {
          if (timerThread == null)
             return;
          timerThread = null;
       }
    
      	 /** Show the time, and wait a while. */
       public void run() {
          while (timerThread != null) {
             repaint();  					 // request a redraw
             try {
                timerThread.sleep(1000);
             } catch (InterruptedException e)		{ /* do nothing*/ }
          }
       } // end of run()
    
      								 /** Display the time. */
       public void paint(Graphics g) {
            Font type = new Font("Monospaced", Font.BOLD, 30);
            g.setFont(type);
    int test =5/2;
          date.setTime(System.currentTimeMillis());
          g.drawString(format.format(date), 10, 100);
      
    }
    } // end of TimeDate class

    In this code
    start() stop() run() and paint() functions never seem to be called up yet the code runs OK

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The methods (this being Java requires that there are no "functions", just methods) will be called behind the scenes by the JVM.

  3. #3
    tghn2b is offline Member
    Join Date
    Jan 2008
    Posts
    31
    Rep Power
    0

    Default

    Thank you Fubarable for mentioning the JVM now it is clear.

    I remember reading something about it in my text book (I have misplaced at the moment)

    But I found this via google at sun web site

    Life Cycle of an Applet: Basically, there are four methods in the Applet class on which any applet is built.
    # init: This method is intended for whatever initialization is needed for your applet. It is called after the param attributes of the applet tag.
    # start: This method is automatically called after init method. It is also called whenever user returns to the page containing the applet after visiting other pages.
    # stop: This method is automatically called whenever the user moves away from the page containing applets. You can use this method to stop an animation.
    # destroy: This method is only called when the browser shuts down normally.

  4. #4
    tghn2b is offline Member
    Join Date
    Jan 2008
    Posts
    31
    Rep Power
    0

    Default

    This leave just one question now, how do I put [solved] in the subject line? or is there a button called solved I have trouble reading
    Last edited by tghn2b; 12-23-2008 at 12:53 AM. Reason: more

Similar Threads

  1. AffinedTransform strange behaviour
    By Echilon in forum AWT / Swing
    Replies: 3
    Last Post: 12-11-2008, 10:58 AM
  2. strange Error message
    By little_polarbear in forum New To Java
    Replies: 4
    Last Post: 08-26-2008, 12:45 AM
  3. Strange problem
    By Tamir in forum Eclipse
    Replies: 1
    Last Post: 08-18-2008, 10:44 PM
  4. What is the answer to this strange problem:
    By willemjav in forum New To Java
    Replies: 5
    Last Post: 08-11-2008, 10:31 PM
  5. Strange behaviour in swing
    By cbalu in forum AWT / Swing
    Replies: 1
    Last Post: 05-23-2008, 10:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •