Skip to main content.

Web Based Programming Tutorials

Homepage | Forum - Join the forum to discuss anything related to programming! | Programming Resources

Java Unleashed

Java Unleashed-ch33.htm Previous Page TOC Index Next Page

Chapter 33

Multimedia and Java

Java’s multimedia capabilities are currently primitive and sometimes inadequate or nonexistent. Nevertheless, Java is positioned as an important technology for Internet-based multimedia, particularly for distributing multimedia content on demand. Furthermore, as bandwidth increases—both within corporate intranets and throughout the Internet—and Java matures and is more widely supported, its importance will grow. Sun has already promised more extensive multimedia features in future releases of Java.

This chapter presents the big picture of multimedia and how it relates to Java. You can think of this chapter as your tour guide through the world of Java multimedia. Some topics are covered here in detail, while other topics have already been covered in prior chapters. In the case of topics being covered in prior chapters, you will see clear references to those chapters in case you happened upon this chapter first. Still other topics are on the horizon and can only be covered in a general sense. Nevertheless, you should finish this chapter with a solid understanding of what type of support Java currently has for multimedia, and where it’s headed in the future.

Because Java has some competition in bringing multimedia to the Web, this chapter also briefly discusses alternatives to Java. In particular, the Macromedia Director movie player, Shockwave, is covered. Macromedia Director is a popular multimedia tool that enables multimedia developers to create interactive movies. More information on Macromedia Director is available at

Bandwidth Limitations

Before getting into multimedia as it affects Java, it’s important to look into a major limitation in dealing with multimedia on the Web: bandwidth. Bandwidth refers to the amount of information that can be sent over a network connection at a time. The multimedia potential of any Web-based media application is directly inhibited by the bandwidth of the connection. Most users across the public Internet access the Net at between 9600 and 28,800 bps.

A variety of emerging technologies in the near future will aid in providing a bandwidth that enables the delivery of multimedia applications in real time. In the meantime, you must consider all the available techniques to minimize file size and ensure that your applications are compelling. In other words, no matter how cool your multimedia content is, if it takes 10 minutes to transfer to an end user’s machine, it’s basically worthless. So, you should use all the tools at your disposal to deal with the bandwidth limitation as it exists today.


Although more mature tools exist for creating and viewing multimedia on the Web, Java is very important even now for distributed multimedia applications on the Internet. It will be increasingly important both as its support for multimedia grows, and as bandwidth increases on the Web. Java’s main contribution will be a robust environment delivering all code necessary to run multimedia content on demand by the user. This eliminates the growing problem of the plethora of browser and plug-in providers, with each provider having multiple versions and proprietary MIME types. Java will help simplify this by requiring the client to have only the necessary Java interpreter downloaded and configured.

In addition, Java also enables lower-level data handling and network access, thus raising the level of site interactivity in terms of interaction with back-end data. Presentations using Java, unlike those based on some other technologies, need not be as stand-alone.

Java Multimedia Features

Although primitive, Java currently has multimedia features, which include the following:

These multimedia features are provided in the standard Java API. Sun also provides an animation class for creating simple animations in addition to the multimedia support in the API classes.


Java provides a wide range of graphics support in its standard API. Java graphics features can be broken down roughly into two groups: primitive drawing routines and bitmapped image routines.

Primitive Drawing

Lest you think I’m referring to etchings on a cave wall, primitive drawing simply refers to the most basic drawing functions such as lines and points. Java provides its primitive drawing support through the Graphics class. The Java Graphics class provides support for many types of primitive drawing functions, including lines, ovals, arcs, and polygons, to name a few. The Graphics class was covered in detail in Chapter 23, “The Applet Package and Graphics.”

Building Your Own Graphics Class Using Primitives

Because the Java technology is still immature, even simple tasks such as drawing a simple grid could require creating a class with several methods. In the following example, we create GridDraw.class to draw a grid using primitives.

The first thing we do is to import java.applet.Applet and the java.awt.*. Then, the class GridDraw is created as an extension of Panel.class. Once all variables are initialized, the GridDraw method sets the layout using BorderLayout and sets background color to white.

Public method setGrid is created as void because no result is returned. This method can then be invoked by any other class that creates an instance of type GridDraw. This method receives values to determine where to start drawing (xstart, ystart), desired width and height (width, height), and number of rows and columns (numrows, numcols).

The paint( ) method is invoked to draw a rectangle starting at (xstart, ystart) with a width specified by width and a height specified by height. The grid is created by drawing horizontal and vertical lines at every height/numrows and width/numcols interval throughout the loop.

The last method, Dimension preferredSize() specifies a default size for the grid, and is set to 250¥250. This method listing can be seen in Listing 33.1.

import java.applet.Applet;

import java.awt.*;

public class GridDraw extends Panel{

      int i,j;

      public int xstart=0, ystart=0, width=0, height=0, numrows=0, numcols=0;

      GridDraw() {

           setLayout(new BorderLayout());



      public void setGrid(int x, int y, int w, int h, int rows, int cols){








      public void paint(Graphics g){


           g.drawRect(xstart, ystart, width, height);

           for (int i=1; i<numrows; i++){

             g.drawLine(xstart, ystart+(i*height/numrows),            Âxstart+width,ystart+(i*height/numrows));


           for (int j=1; j<numcols; j++){

              g.drawLine(xstart+(j*width/numcols), ystart, xstart+(j*width/             Ânumcols), ystart+height);



      public Dimension preferredSize(){

           return (new Dimension(250,250));



Using this class to draw a 5¥5 grid in a 100 pixel by 100 pixel box is fairly simple, as shown in Listing 33.2.

import java.applet.Applet;

import java.awt.*;

public class GridExample extends Applet {

    GridDraw gridDraw;

    public void init() {




        add(gridDraw = new GridDraw());




An HTML file to view this, called GridExample.html (see Listing 33.3), can then be created.

<title>Grid Example</title>


<applet code=GridExample width=250 height=250>



The applet, when run as shown, will appear as shown in Figure 33.1.

Figure FIGURE 33.1.

Output from GridExample class.

In the near future, many development tools will enable the user to quickly create graphical classes without much work. But until these tools become readily available, simple classes such as GridDraw can be coded fairly easily. The object-oriented nature of Java enables such classes to be used over and over.

Bitmapped Images

Although primitive graphics are useful for drawing graphs and charts, most realistic graphics require the use of bitmapped images. Bitmapped images are composed of pixels of different colors and can be created directly from scanned photographs and artwork. The majority of graphics viewed on the Web is in the form of bitmapped images. Java provides support for bitmapped images in a variety of ways.

The most fundamental support for bitmapped images in Java is the Image class, which represents an image in memory. The Graphics class also provides support for drawing images using the Image class. Furthermore, there is a whole set of classes for manipulating and working with images. This set of classes consists of an image filter framework, which enables you to perform transformations on images, such as brightening, sharpening, and scaling.

The Java programming aspects of bitmapped images were also covered in Chapter 23, “The Applet Package and Graphics,” and Chapter 25, “Animation Programming.”


The current release of Java provides fairly limited support for audio. However, within this support is the capability to play multiple, digital waveform audio sounds. This section examines how Java handles audio and is illustrated with some examples.

Audio Classes

The current release of Java supports audio through classes that interface with the audio hardware. The standard Java API actually only contains one class for working with audio, AudioClip, which is located in the java.applet package. The AudioClip class represents a single piece of digital waveform audio.

There are also a variety of undocumented, unsupported audio classes in the package. These audio classes will more than likely form the basis for future, more extensive audio features in future releases of Java.

The standard, and currently only supported, format for audio data in Java is Sun’s AU format. This format is nice because the audio files are usually very small. Of course, the trade-off is that the quality of sound is usually somewhat lacking.

Example: HelloWorldAudio

For illustrative purposes, let’s extend the familiar HelloWorld applet with audio. You saw this applet way back in Chapter 1, “Java Makes Executable Content Possible.” This example assumes creation of an audio file named that has been placed in a directory called audio. Listing 33.4 contains HelloWorldAudio applet, which plays the audio file

import java.applet.*;

import java.awt.*;

public class HelloWorldAudio extends HelloWorld {

AudioClip audio;

  public void init () {


    audio = getAudioClip(getCodeBase(),”audio/”);


  public void start() {;



The class HelloWorldAudio is an extension of the HelloWorld class. It is declared public so that it is visible to the Java runtime environment. The AudioClip object is initialized in the init method by calling the getAudioClip method. This method creates an AudioClip object in memory from an audio file. The start() method then is used to play the audio stream with a simple call to the play method.

Now, let’s create an HTML file to play HelloWorldAudio.class and call it HelloWorldAudio.html (see Listing 33.5).



<title>Hello World Audio</title>


<BODY><P>”Hello World Audio” </P>

<applet code=HelloWorldAudio.class width=100 height=80> </applet>




At the core of the Java architecture is the concept of multithreading. Multithreading is basically the capability of the Java environment to support multiple paths of execution. More practically speaking, it means you can have multiple sections of code or even multiple programs running at the same time. The details of multithreading and how threads are handled in Java were covered in Chapter 15, “Threads and Multithreading.”

In this chapter, however, you are interested in how multithreading impacts multimedia in Java. Multithreading actually plays a vital role in multimedia because it is essential in providing a means for animations to execute at a fast speed without monopolizing the processor.

Consider the situation of playing an animation at 12 frames per second. In this case, the Java applet displaying the animation must update and redraw its graphical output 12 times every second, or once every 83 milliseconds. For an applet to maintain this kind of frame rate, it has to constantly keep working at updating and redrawing the animation frames. If this work was handled in an environment without threads, the animation code would basically take over the processor. However, in a multithreaded environment like Java, you can assign the animation its own thread and it will coexist comfortably in the runtime system. The animation still requires as much processor overhead as before, but the Java runtime system makes sure other threads get a shot at the processor, too.

The Media Tracker

The Java media tracker is another unique feature of Java that aids in supporting multimedia on the Web. The Java media tracker is a class that provides a mechanism for keeping up with when multimedia content has been successfully transferred over a network connection.

The need for the media tracker goes back to the issue of bandwidth discussed earlier in this chapter. Because it sometimes takes a considerable amount of time to transfer multimedia content over an Internet connection, Java multimedia applets desperately need to know if and when the content is available to use. The media tracker solves this problem by providing Java applets a means to track when multimedia objects have finished loading.

The media tracker was used in a practical sample applet in Chapter 25, “Animation Programming.”

Future of Java and Multimedia

Probably much more important than the present state of Java multimedia is what the future holds. There are so many emerging multimedia technologies related to or based on Java that it is difficult to see which ones will brave the storm and which will just fade away. One thing is for certain: The future of Java and multimedia holds both a lot of uncertainty and a lot ofpromise.


In this section, you’ll take a close look at one of the more promising multimedia technologies that promises to provide some degree of Java support: Macromedia’s Shockwave technology.

In short, Shockwave is a new technology developed by Macromedia that allows Macromedia Director movies to be embedded in Web pages. Shockwave is distributed in the form of a Web browser plug-in. Currently, Shockwave is supported by Netscape Navigator 2.0 for Windows 3.1, Windows 95, and Macintosh. It is expected to soon be supported by other browsers.

To understand the impact a technology like Shockwave could have on the Web, you must understand what Macromedia Director itself is all about. Macromedia Director is a leading multimedia development tool, originally developed for the Macintosh and later released for Windows 3.1, that provides a powerful movie paradigm and graphical approach for creating interactive multimedia presentations. Multimedia developers use Director to control the choreography and coordination among images, sound, video clips, and other media types to create a complete multimedia movie. The approach is similar in theory to a director coordinating actors, props, and a soundtrack when creating a motion picture.

Creating a Shockwave Movie

The process of creating a Shockwave movie begins by first creating the movie using Director as if it were a standard Director movie. Once the movie is working well under Director, it is processed by Afterburner, which is a tool distributed with Shockwave that compresses the movie into a form more suitable for the Web.

The resulting compressed movie is then embedded in a Web page as multimedia content. In Netscape Navigator 2.0, a Shockwave movie is embedded like this:

<EMBED SRC=”path/file_name.ext” WIDTH=n HEIGHT=n TEXTFOCUS=focus>

The WIDTH and HEIGHT arguments specify the width and height of the movie window, in pixels, and TEXTFOCUS tells the Shockwave plug-in when to respond to input from the keyboard. Possible TEXTFOCUS settings include focus, onMouse, onStart, and never.

If the user’s browser doesn’t support the Shockwave plug-in, the NOEMBED tag enables you to substitute a JPEG or GIF image in place of the movie, like this:

<NOEMBED> <IMG SRC=”path/file_name.ext”> </NOEMBED>

Shockwave and Java

Deciding when to use new technologies such as Shockwave, as opposed to developing your own classes in Java, is often difficult. There are no hard rules governing when to use certain technologies over others. All you can hope for is a future integration and convergence of technologies, which would allow more options for multimedia developers. For example, Macromedia has shown a serious interest in Java. In fact, they have even promised some degree of Java support in future releases of Shockwave. What this will mean to the Java multimedia programmer isn’t yet clear, but there is certainly the potential for a leveraging of high-level multimedia development tools with custom Java code.

Getting Shockwave Information

To get the latest information regarding Shockwave, check out Macromedia’s Web site at You can also take a look at the Director Web at, which is a good place to go for examples of Shockwave movies and technical details showing how commands are used.


As a multimedia technology, Java is still in its infancy. Although it currently supports some interesting multimedia features such as graphics, imaging, basic sound, and media tracking, it is still lacking in many ways. This situation really is to be expected, because the Web community in general has yet to figure out the best approach to take in handling distributed multimedia. There are many problems to be solved before any technology can successfully deliver a widely distributed multimedia solution. However, even in its young state, Java is quickly positioning itself as the driving multimedia technology for the Web.

Java, however, isn’t alone. There are other technologies aiming to solve many of the multimedia problems facing the Web as well. One of these technologies, Macromedia Shockwave, which is based on the popular Macromedia Director multimedia authoring tool, actually shows signs of supporting some degree of integration with Java. This mixture of Java and new third-party technologies will more than likely pave the way for the future of multimedia on the Web. Either way, there doesn’t appear to be much doubt that Java will play a key role in the evolution of multimedia on the Web.

Previous Page TOC Index Next Page