All pastes #9050 Raw Edit

Miscellany

public text v1 · immutable
#9050 ·published 2005-04-07 20:43 UTC
rendered paste body
/**
 *am trying to convert the APPLET from:
 *http://java.sun.com/docs/books/tutorial/essential/threads/clock.html
 *I could NOT get the applet code to function, plus this is good practice for me anyway
 *do NOT understand threads yet...
 *
 *get NO errors!, this compiles, and shows the STATIC time, but does not turn numbers like the demonstration
 *at above link don't know what am doing wrong
 *

 */

package clock2;

import java.awt.*;
import java.awt.Graphics.*;
import java.util.*;
import java.text.DateFormat;
import javax.swing.*;

public class Clock2 extends JFrame implements Runnable {
    private Thread clockThread = null;
    
    
    public Clock2() { 
        super ("Clock Threading");
        
        setSize(150,150);
        setVisible( true );
        show();
    }
    
   public void paint(Graphics g) {
       super.paint( g );
     
       
       //get the time and convert it to a date
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        //format it and display it
        DateFormat dateFormatter =
				DateFormat.getTimeInstance();
        g.setColor( new Color( 255, 0,0 ) );
        
        g.drawString(dateFormatter.format(date), 44, 85);
  
      
    }

   
   
   
    public void start() {
        if (clockThread == null) {
            clockThread = new Thread(this, "Clock");
            clockThread.start();
        }
    }
    
    
    
    
    public void run() {
        Thread myThread = Thread.currentThread();
        while (clockThread == myThread) {
            repaint();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                //the VM doesn’t want us to sleep anymore,
                //so get back to work
            }
        }
    }
    
    
     public static void main(String[] args) {
    
    Clock2 clocker = new Clock2();
    
    clocker.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    
    
}
    
  
    
   
}//END MAIN CLASS