Hello Demo

A simple clock display that should work with any version of Java. You cannot run Java applets SimpleClock.java

/** Every second update a time display. */

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

public class SimpleClock extends Applet implements Runnable {

  Thread runner = null;

/** To run as an application,
 * construct it and show it in a frame.
 * This must be a static (class not instance) method,
 * because the instance won't exist until we construct it here.
 * When run as an applet,
 * the browser provides the frame,
 * and calls the constructor and init methods. */

  public static void main (String []argv) {

      SimpleClock cd = new SimpleClock ();

      Frame ff = new Frame ("SimpleClock");
      ff.setBounds (100, 100, 200, 50);

      ff.add (cd);
      cd.init ();
      cd.start ();

      ff.show ();
  }

  public void init () {
      setForeground (Color.blue);
      setBackground (Color.white);
  }

/* The ''runner'' thread runs this method */

  public void run () {
      while (true) {
          repaint ();
          try { Thread.sleep (1000); }
          catch (Exception e) {}
      }
  }

  public void start () {
      if (null == runner) {
	  runner = new Thread (this);
	  runner.start ();
      }
  }

  public void stop () {
      if (null != runner) {
	  runner.stop ();
	  runner = null;
      }
  }

  public void paint (Graphics g) {
      Date now = new Date ();
      g.drawString (now.toString (), 0, 15);
  }

}

This very simple applet is written twice, in Java 1.0.2 and in Java 1.1.x styles, to see what version (if any) your browser can run. Neither of them really does anything, they just show what version is supported. Many of the examples in this directory require Java 1.1.x support, but some of them are also available in the older Java 1.0.2 style.

This appletshows you can run 1.0 Java.
You cannot run Java applets

import java.applet.Applet;

import java.awt.*;
import java.awt.event.*;

/** Simplest++ Applet */

public class Hello2Demo extends Applet
{

    private TextArea textarea;

/** To run as an application,
 * construct it and show it in a frame.
 * This must be a static (class not instance) method,
 * because the instance won't exist until we construct it here. */

  public static void main (String []argv) {

      Frame ff = new Frame ("Hello2Demo");
      ff.setBounds (100, 100, 500, 300);

      Hello2Demo cd = new Hello2Demo ();
      ff.add (cd);

      cd.init ();

      ff.pack ();
      ff.show ();

      cd.invalidate ();
      cd.validate ();
  }

    public Hello2Demo () {
	setLayout (new FlowLayout ());

          add (new Button ("Start"));

          add (new Button ("Stop"));

          add (new Checkbox ("Help"));

        add (textarea = new TextArea (12,50));
    }

    public void init () {
        System.out.println ("Hello2Demo init");

//  awt 1.1 added append...
	textarea.append ("Hello2Demo\n");
	textarea.append ("ignores all buttons\n");
    }

}

This appletshows you have 1.1 Java.
You cannot run Java applets

import java.applet.Applet;

import java.awt.*;
import java.awt.event.*;

/** Simplest Applet -- get this working first! */

public class HelloDemo extends Applet
{

    private TextArea textarea;

/** To run as an application,
 * construct it and show it in a frame.
 * This must be a static (class not instance) method,
 * because the instance won't exist until we construct it here. */

  public static void main (String []argv) {

      Frame ff = new Frame ("HelloDemo");
      ff.setBounds (100, 100, 500, 300);

      HelloDemo cd = new HelloDemo ();
      ff.add (cd);
      cd.init ();

      ff.pack ();
      ff.show ();

      cd.invalidate ();
      cd.validate ();
  }

    public HelloDemo () {
	setLayout (new FlowLayout ());

          add (new Button ("Start"));

          add (new Button ("Stop"));

          add (new Checkbox ("Help"));

        add (textarea = new TextArea (12,50));
    }

    public void init () {
    System.out.println ("init");
//	textarea.append ("HelloDemo\n");
//	textarea.append ("ignores all buttons\n");
    }

}

Directory to Other Java..

Please also visit my home page.

Email me at morris_hirsch@brown.edu

Legalities

This software is provided free of charge with no support. Email me at morris_hirsch@brown.edu if you do have a problem, and I will try to help, but I cannot promise to.

Except for all sections which are identified as the work, and copyright, of others, this work is Copyright (C) 1998 by Morris Hirsch. All rights reserved, except as granted here.

Redistribution and use in source and binary forms, with or without modification, for any purpose, are permitted, except as may be restricted by original copyright holders, provided that the following conditions are met:

DISCLAIMER

This software is provided free of charge with no support.
This software is provided in source format; You are advised to examine it and understand it, before putting it to use.
This software uses only algorithms and coding techniques that are available to the public in the open literature. You are however, advised to make your own determination of legality, for your intended use.
This software is provided by the Author and Contributors ``AS IS'' and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose, are disclaimed.
In no event shall the Author or Contributors, or their Agents, be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
1