[5433] in java-interest

home help back first fref pref prev next nref lref last post

First try at an applet

daemon@ATHENA.MIT.EDU (Mike Tindell)
Thu Feb 8 16:51:44 1996

From: "Mike Tindell" <jmt@penne.engr.sgi.com>
Date: Thu, 8 Feb 1996 12:32:07 -0800
To: java-interest-digest@java.sun.com

Hello.  I was playing around with the Irix Java environment and wrote a demon
dialer program.  I compiled and ran it in Netscape 2.0b6 for Irix and got a
runtime exception that appeared to be a mangled variable name.  Would someone
please compile and run my silly program on Windows and let me know if there is
a problem in my code or a bug in the compiler?  Thanks for any help in advance.

______________________________________________________________________________

//
//      A Java demon-dialer for Web browsers.
//

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

public class DemonDialer extends Applet {

        // Will have a container local so multiple dialer threads can
        // be running at once.

        Button button;
        DDPanel panel;
        Dimension originalSize;

        public void init () {
                button = new Button ("DemonDialer");
                add (button);
                panel = new DDPanel ();
                add (panel);
                panel.hide ();
        }

        public boolean action (Event event, Object what) {
                if (event.target == button) {

                        // This is a temporary hack, until I learn more.
                        originalSize = size ();

                        button.hide ();
                        resize (panel.size ());
                        panel.show ();
                        repaint ();
                }
                else if (event.target == panel) {
                        DDThread thread = new DDThread ((String) event.arg,
                                                        getAppletContext ());
                        // Debug code.
                        if (thread == null)
                                System.out.println ("Oops!  No new thread.");
                        else
                                System.out.println ("Created new thread.");

                        thread.run ();

                        // Need to set this up with a timeout parameter.
                        try
                                thread.join (10 * 1000);
                        catch (InterruptedException oops)
                                System.out.println ("Oops!  Failed join.  " +
                                                oops.getMessage () +
                                                " - end exception message.");

                        System.out.println ("Back from demon dialer thread.");

                        panel.hide ();
                        button.show ();

                        // Need to be smarter here.
                        resize (originalSize);
                        repaint ();
                }
                else
                        System.out.println ("Got bogus action.");

                return true;
        }
}

class DDThread extends Thread {
        URL url;
        AppletContext context;

        public DDThread (String urlString, AppletContext appletContext) {
                try
                        url = new URL (urlString);
                catch (IOException oops)
                        System.out.println ("Oops!  Failed creating URL.  " +
                                                oops.getMessage () +
                                                " - end exception message.");

                // Debug code
                if (url == null)
                        System.out.println ("Oops!  Failed opening URL.  " +
                                                "Null URL returned.");
                else
                        System.out.println ("URL is " + url.toString ());

                context = appletContext;
        }

        public void run () {
                URLConnection connection = null;

                try
                        connection = url.openConnection ();
                catch (IOException oops)

                        // while no connection, really want to try again
                        // until timeout; for now, print debug message.

                        System.out.println ("Oops!  Failed opening URL.  " +
                                                oops.getMessage () +
                                                " - end exception message.");

                // Debug code.
                if (connection == null)
                        System.out.println ("Oops!  Failed opening URL.  " +
                                                "Null connection returned.");
                else
                        System.out.println ("Connection is open.");

                // When connect notify user; if desired, display page.
                // For now just do it.

                context.showDocument (url);
        }
}

class DDPanel extends Panel {
        TextField text;

        public DDPanel () {
                text = new TextField (30);
                add (text);
                add (new Button ("Go to"));

                // Need to be smarter here...
                resize (300, 80);
        }

        public boolean action (Event event, Object what) {
                event.target = this;
                event.arg = text.getText ();

                return false;
        }
}
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post