Java Network Features: Intro

Most people assume that Java is a vehicle written for web browsers, i.e. to 'spice up' web pages. While we know by know that that is not true at all, what is true that Java is a language written with the Internet in mind. Therefore, Java has a lot of network support built in, and you can make use of it in a variety of ways.

We will start our 'network explorations' by looking at the classes in the java.applet package, in particular at the java.applet.AppletContext class:

public abstract Applet getApplet(String name);
public abstract Enumeration getApplets();
public abstract AudioClip getAudioClip(URL  url);
public abstract Image getImage(URL  url);
public abstract void showDocument(URL  url);
public abstract void showDocument(URL  url, String  target);
public abstract void showStatus(String  status);

An AppletContext, usually, provides a hook to the current web browser used. Therefore, using this class we should be able to load images, audio clips, display messages in the status bar of our web browser, and display other web documents in the web browser. Here, for example is a little applet that displays a message in the browser's status bar. Of course, if you are not using a web browser (but, say, the appletviewer) to look at this applet, nothing may happen (but also no error will occur).

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

public class NetExample extends Applet
{
   private AppletContext browser = null;
   private Button showStatus    = new Button("Show Status");

   public void init()
   {
      Panel panel = new Panel();
      panel.setLayout(new GridLayout(1,2));
      panel.add(showStatus);

      setLayout(new BorderLayout());
      add("South", panel);

      browser = getAppletContext();
  }

   public boolean action(Event e, Object o)
   {
      if (e.target == showStatus)
         browser.showStatus("Here is something for your status line ...");
      return true;
  }
}

Next, we would like to control our web browser from within the currently running applet. The 'showDocument' seems appropriate, but we need to make up a URL first. Looking up the URL class, we find, as the most important facts:

public final class java.net.URL extends java.lang.Object
{
        // Constructors
    public URL(String  spec);
        // Selected Methods
    public final InputStream openStream();
}

Also, this class is part of the java.net package (and hence must be imported from there). Finally, when constructing a URL from a string, one must catch a MalFormedURLException exception. Here is the code to switch the web browser to a new location, added to the previous code. Again, if an appletviewer rather than a web browser is used, this will not work.

import java.applet.*;
import java.awt.*;
import java.net.URL;

public class NetExample extends Applet
{
   private AppletContext browser = null;
   private URL location = null;

   private Button showURL       = new Button("URL");
   private Button showTargetURL = new Button("URL + target");
   private Button showStatus    = new Button("Show Status");

   public void init()
   {
      Panel panel = new Panel();
      panel.setLayout(new GridLayout(2,2));
      panel.add(showURL);
      panel.add(showTargetURL);
      panel.add(showStatus);

      setLayout(new BorderLayout());
      add("South", panel);

      browser = getAppletContext();

      try
      {
         location = new URL("http://www.shu.edu/");
      }
      catch(Exception e)
      {
         System.err.println("Invalid URL ");
      }
   }

   public boolean action(Event e, Object o)
   {
      if (e.target == showStatus)
         browser.showStatus("Oops, looks like you're getting the hang of it!");
      else if (e.target == showURL)
         browser.showDocument(location);
      else if (e.target == showTargetURL)
         browser.showDocument(location, "_new");

      return true;
   }
}

Note that the 'showDocument' method can take an additional String parameter indicated whether a new window should be opened, or whether to put a web page into a named frame.

Next, let's try to load an audio clip and play it at the press of a button. First, notice that the appropriate message will be the getAudioClick method of the AppletContext. That message returns an AudioClip, for which the API says the following:

public  interface  java.applet.AudioClip
{
        // Methods
    public abstract void loop();
    public abstract void play();
    public abstract void stop();
}

Therefore, to add an audio clip to our applet, we need a new field to contain the clip, set a URL to the audio clip on the net, load the clip, then play the clip at will. Here's some same code to accomplish this:

import java.applet.*;
import java.awt.*;
import java.net.URL;

public class NetExample extends Applet
{
   private AppletContext browser = null;
   private URL location = null;
   private URL audioLocation = null;
   private AudioClip audio = null;
   private boolean audioLoaded = false;

   private Button showURL       = new Button("URL");
   private Button showTargetURL = new Button("URL + target");
   private Button showStatus    = new Button("Show Status");
   private Button showAudio     = new Button("Show Audio");

   public void init()
   {
      Panel panel = new Panel();
      panel.setLayout(new GridLayout(2,2));
      panel.add(showURL);
      panel.add(showTargetURL);
      panel.add(showStatus);
      panel.add(showAudio);

      setLayout(new BorderLayout());
      add("South", panel);

      browser = getAppletContext();

      try
      {
         location = new URL("http://www.shu.edu/");
         audioLocation = new URL("file:///c|/temp/ha.au");
      }
      catch(Exception e)
      {
         System.err.println("Invalid URL ");
      }
   }

   public boolean action(Event e, Object o)
   {
      if (e.target == showStatus)
         browser.showStatus("Loading page now ...");
      else if (e.target == showURL)
         browser.showDocument(location);
      else if (e.target == showTargetURL)
         browser.showDocument(location, "_new");
      else if (e.target == showAudio)
      {
         if (!audioLoaded)
         {
            audio = browser.getAudioClip(audioLocation);
            audioLoaded = true;
            audio.play();
         }
         else
            audio.play();
      }
      return true;
   }
}

Finally, let's also try to load and display an image. The 'getImage' method of the AppletContext seems to be do that. We have already seen how to display an image in the onscreen drawing area, so we simply need to figure out how to work the logic of everything. Here is a sample:

import java.applet.*;
import java.awt.*;
import java.net.URL;

public class NetExample extends Applet
{
   private AppletContext browser = null;
   private URL location = null, imageLocation = null, audioLocation = null;
   private Image image = null;
   private AudioClip audio = null;
   private boolean imageLoaded = false, audioLoaded = false;

   private Button showURL       = new Button("URL"),
                  showTargetURL = new Button("URL + target"),
                  showStatus    = new Button("Show Status"),
                  showImage     = new Button("Show Image"),
                  showAudio     = new Button("Show Audio");

   public void init()
   {
      Panel panel = new Panel();
      panel.setLayout(new GridLayout(3,2));
      panel.add(showURL);     panel.add(showTargetURL);
      panel.add(showStatus);  panel.add(showImage);
      panel.add(showAudio);

      setLayout(new BorderLayout()); add("South", panel);
      browser = getAppletContext();

      try
      {
         location      = new URL("http://www.shu.edu/");
         imageLocation = new URL("file:///c|/temp/frac.gif");
         audioLocation = new URL("file:///c|/temp/ha.au");
      }
      catch(Exception e)
      {
         System.err.println("Invalid URL ");
      }
   }

   public boolean action(Event e, Object o)
   {
      if (e.target == showStatus)
         browser.showStatus("Loading page now ...");
      else if (e.target == showURL)
         browser.showDocument(location);
      else if (e.target == showTargetURL)
         browser.showDocument(location, "_new");
      else if (e.target == showAudio)
      {
         if (!audioLoaded)
         {
            audio = browser.getAudioClip(audioLocation);
            audioLoaded = true;
            audio.play();
         }
         else
            audio.play();
      }
      else if (e.target == showImage)
      {
         if (!imageLoaded)
         {
            image = browser.getImage(imageLocation);
            imageLoaded = true;
            repaint();
         }
         else
            repaint();
      }
      return true;
   }

   public void paint(Graphics g)
   {
      if (imageLoaded)
         g.drawImage(image,0,0,null);
   }
}

Our applet can now load sound and images, keep the user informed about what's happening by using the browser's status line for messages, and bring up additional web pages in the browser at will. Using these samples, it should be easy to improve on our current applets to include sound and graphics, and perhaps use the web browser for a nicely formated help screen.

However, you may have noticed that the image does not necessarily display when we press the first time on the 'Show Image' button. We will discuss these issues, and others, during the next lecture.