Comp210 Lab 14: HTML and Applets

Table of Contents

  • Your Web Page
  • Events
  • Applets
  • Applets are Java applications that can be run by a web browser. First we'll make your web page; then we'll write an applet.

    Your Web Page

    Create a directory public_html in your home directory. (Make sure it is publically accessible.) Copy ~comp210/tutorials/lab13/sample-index.html to your public_html/index.html. These two names are special; this is what a browser looks at if somebody tries to view http://www.owlnet.rice.edu/~username. Also, copy a gif file with an interesting-sounding name from ~comp210/Gifs to public_html/some.gif.

    Now, go view your home page! You can edit your new index.html file at your leisure.

    Design and layout of your web page is easy to write, but difficult to write well. As you browse other people's pages, pay attention to what makes those pages good or bad. (Does your eye get lost in clutter, or does it glide over the information, recognizing how the page is arranged w/o even being aware of it?)

    Some ways to learn about HTML include

    Events

    Programs you've written so far always started from some function (like main()), and proceeded at their own discretion. When they wanted input, they called some input routine (like (read)). In contrast, applets (as well as most programs using a graphical interface) tend to be event-driven.

    An event is some happening outside of the program's control, which the program may be interested in. A few examples are

    Having a Being event-driven means

    In Java, there is a class Applet, which has methods (with names like mouseDown) for various groups of events. When an event occurs, Java repsonds by calling the appropriate method of Applet (such as mouseDown or ), passing arguments to the method which contain details of the event (such as which mouse button or key was pressed down). To make your own applet, just create a class which extends Applet, and then override the methods of the events you'd like to handle.

    public class MyZerothApplet extends Applet {
      public boolean mouseDown( Event e, int x, int y ) {
        // Hey!  This is my very own code, being run when a mouse button is clicked!
        // x and y are the coordinates of the click, and e contains
        // further details of the event (e.g. which mouse button).
        System.out.println( "Saw a click-down at (" + x + ", " + y ")." );
    
        // i've handled the event, so return "true, nobody else needs to
        // worry about doing anything more with the click".
        return true;
        }
      }
    

    Putting Applets into Your Web Page

    In order to run applets from inside netscape, you must:
    1. You need to get a certain file in your ~/.netscape directory.
          owl%   ln -s  ~comp210/.netscape/java_301  ~/.netscape
      
      ln -s is conceptually similar to cp, but it just makes a symbolic link--a small pointer to the real file. (In the MacOS, symbolic links are called "aliases".) Good thing, since the real file is 7Meg of java libraries!
    2. Quit your current netscape, and instead run netscape3.01 (which is coming from ~comp210/bin, by the way, and it not guaranteed to always be there for you).
    3. Inside netscape3.01, from the Options menu select "Network Preferences", thumb to the "Languages" tab, and click the "Enable Java" button.
    A later post will contain instructions on how to convert your trivia game (hw12) so you can put it on your web page!

    Example Applets

    Suppose you have a Java class FirstApplet.class, and you want that applet to run when your web page is fired up. You simply include, in your web page, an html tag something like <applet code="FirstApplet.class" width=150 height=100> The Java programming examples shown here are from the book Java in a Nutshell , by David Flanagan, published by O'Reilly & Associates. The examples were written by David Flanagan, and are Copyright (c) 1996 by O'Reilly and Associates. You may study, use, and modify these examples for any purpose, but note that they are provided with NO WARRANTY express or implied.

    Example 4-1

    FirstApplet -- the simplest possible applet; displays "Hello World".
    view source code  run the applet

    Example 4-2

    SecondApplet -- a fancier version of Hello World.
    view source code  run the applet

    Example 4-3

    Scribble -- a simple applet with user interaction. Click and scribble in the window. This applet cannot refresh itself.
    view source code  run the applet

    Example 4-4

    EventTester -- play with the mouse and keyboard in this window. It will tell you what events are generated.
    view source code  run the applet

    Example 4-5

    ColorScribble -- the scribble applet in color. The colors are specified by tags in the HTML file, and are read by the applet code.
    view source code  run the applet

    Example 4-6

    ClearableScribble -- we add a Clear button to the basic Scribble applet.
    view source code  run the applet

    Example 4-7

    Imagemap -- an example of a simple client-side imagemap implmented in Java.
    view source code  run the applet

    Example 4-8

    Animator -- simple animation in Java.
    view source code  run the applet

    Example 4-10

    StandaloneScribble -- an applet converted to run as a standalone Java application.
    view source code
    Back to Comp 210 Home