Applets VS Applications

 

 

When you set out to design a program in Java you need to decide if you want the program to run as an application (local hard drive) or as an applet (in a browser).   In this tutorial we will design applets with the purpose of the content being viewed or used on the Internet.   We will assume that you have downloaded Java Development Kit (JDK) version 3.1 and that you are using JCreator version 1.52 as your Integrated Development Environment (IDE).

 

The first step to developing a multimedia applet is to set up an appropriate workspace using JCreator.  It is critical to keep your Workspace name, Class name, Java source file name and HTML file the same if you want to use the AppletViewer application.   The AppletViewer application saves time because you will not need to load the applet in the browser every time you want to test your output.   In the illustration below, you will see how to set up an applet Project workspace :

 

 

 

 

 

Be sure to select a directory where you want to save your Java source and HTML file.  I’ve created a workspace “Test” and you will note that the Test.java and Test.html have been automatically set up in the workspace.

 

 

 

Now that you have set up the project correctly you can use the AppletViewer application to view the output.  In the remaining parts of this tutorial we will examine how the “Easter” applet was developed and demonstrate the use of multimedia objects in Java.

 

 

 

Next, you will need to download the “Easter” lab Java source code from the website and cut and paste this code into a workspace that was created in the same manner as “Test”.  When you have completed this you should have a workspace that looks like the illustration below.   Next, you must copy the image files and Sun audio file from the “easter.zip” file to the Easter folder or workspace prior to compiling the project.

 

 

 

 

 

 

 

 

 

 

When you compile and execute the “Easter” lab you will see an image file of an egg move around the applet canvas followed by an instance of an audio file that makes the sound of a chicken.  This will occur once only because we have yet to add thread code to enable multi-tasking or animation.   In addition, when you complile the java source file (.java ) successfully the JDK will generate an Java executable file (.class) which will be the file that the browser interprets to display the output.

 

Next, let’s take some time to examine the source code for the Easter Lab:

 

import java.awt.*;

import java.applet.*;

 

// had to include all applet libraries for sound to work

 

public class Easter extends Applet

           

            {

           

            private Image easteregg;

            private AudioClip chicken;

           

           

            int y;  // forced to make y global when 2 loops involved

            int x;

           

            // both the spaceship and train variables had to be declared as global

            // because they involve the browser or for VB people the Form

           

            public void init()

            {

                        easteregg = getImage(getDocumentBase(),"egg1.gif");

                        chicken = getAudioClip(getDocumentBase(), "CHICKEN.AU");

    }

   

   

    public void paint (Graphics g)

    {

           

    for(y=0;y<200;++y)

            {

            g.drawImage(easteregg,1,y,this);

            chicken.play();

                       

    }

      

    for(x=0;x<400;++x)

            {

            g.drawImage(easteregg,x,200,this);

            chicken.play();

           

    }

   

    for(y=200;y>0;--y)

            {

            g.drawImage(easteregg,400,y,this);

            chicken.play();

                       

    }

   

    for(x=400;x>0;--x)

            {

            g.drawImage(easteregg,x,1,this);

            chicken.play();

    }

    g.setColor(Color.red);

    g.setFont(new Font("Arial", Font.PLAIN, 30));

    g.drawString("Happy Easter",200,100);

 

}          

 

} // end of Easter class

 

 

 

In the code above, you will note that an applet begins with the “public void init()” code rather than the “public static void main (String args[ ])” that we use for applications.  Also, you will see that Java uses a variety of functions to initialize and display data types.  The Internet provides many good tutorials to cover these topics and I will leave that discovery to you.  Also, you will note how an applet displays output to the canvas or “this” on the browser by using the “public void paint (Graphics g)” method.

 

Because we are displaying the output to a browser or an external object it is required in Java that we set up variables with a “global” scope.  For those of you that program using VB this is similar to when we need to send data to another form in a multi-form project.  Also, you will recognize the use of the “this” variable if you have done any Javascript programming.  For those of you that program in C, you will note that Java requires libraries for specific programming tasks.  In the “Easter” lab, I had to import the applet (java.applet.*) and abstract windows toolkit (java.awt.*) libraries to complete the lab. 

 

 

In order to display the “Happy Easter” message on the canvas I had to instantiate an instance of the Font method and send the appropriate parameters (ex. Arial", Font.PLAIN, 30) to this encapsulated class to get the desired results.  Java is an Object-Oriented Programming (OOP) language and it has a plethora of methods and classes that you can harness to do just about anything.

 

 

Viewing the Applet

 

There are two ways to view the output of an applet, namely the AppletViewer application or a browser (MSIE or Netscape).   When the students are developing their applets it is much easier to view output using the AppletViewer.   For example, let’s look at the “Easter” lab output with the AppletViewer as it is presented in JCreator.  In the illustration below, you will note the icons for compiling and executing a project.  The icon that executes the applet source code invokes the AppletViewer from the JDK.

 

 

 

 

 

 

 

When you execute the project it will appear in a small window that simulates a browser.  When you have the applet performing as you desire then you can test it in a browser.  You will use the browser to open the HTML file that is in the workspace.  It is critical that all media files are in the same folder as the workspace otherwise they will not load.   In the illustration below you can see how MSIE is used to display an applet.  Be sure that the HTML file includes the exact name of the class file that you are trying to interpret:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

If you have followed the above steps accurately then you should see the output above in your browser