A simple clock display that should work with any version of Java. 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.
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.
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"); } }
Please also visit my home page.
Email me at morris_hirsch@brown.edu
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: