HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Java & Macromedia, a perfect match"
Vol. 6, Issue 2, p. 18

	

Listing 1 CustomClassLoader.java

/**
* this class is a customized class loader which loads a named class 
and puts into a Hashtable for future reference
*/
import java.io.*;
import java.util.*;

public class CustomClassLoader extends ClassLoader
{

     Hashtable classdefs; //Hashtable for storing the reference 
of the loaded class(s)

      public CustomClassLoader()
     {
classdefs=new Hashtable();
     }

/**
* this method loads a named java class file and returns it as a Class object
*/
         public Class loadClass(String className, boolean flag)
        {

try
            {
     if(flag)
    {
resolveClass(findSystemClass(className));

//putting the class name into the hashtable
classdefs.put(className, findSystemClass(className));
         }

}catch(Exception e)
{
System.out.println("Error in loading the 
class  : "+e.toString());
}

//returning the class by picking up from the hashtable
return (Class)classdefs.get(className);
}

}

Listing 2 CustomStub.java

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.io.*;
/**
* This is a custom class providing the required AppletStub and 
AppletContext environment to the
* movie applet. This is a very minimal implementation of the 
AppletStub and AppletContext interfaces with
* only the methods implemented as they are required by the movie applet.

*  The getCodeBase() , getImage(0 and getParameter() methods are used 
by the movie applet.
*  So they are implemented accordingly.
*/

public class CustomStub implements AppletStub, AppletContext, Enumeration
{

private URL codeBase;
private URL documentBase;
private String mName=null;
/* The constructor accepts the name of the movie because from the 
getParameter() method we have to return
* the movieName.djr as a string which is used by the movie applet to 
display the movie.
*/
public CustomStub(String movieName)
{
mName=movieName;
try
{
codeBase=new 
URL("file:/"+System.getProperty("user.dir")+"/");
documentBase=codeBase;
}catch(Exception e)
{

}
}

public void appletResize(int w, int h){ }

public AppletContext getAppletContext()
{
return (AppletContext)this;
}

public URL getDocumentBase()
{
return documentBase;
}

public URL getCodeBase()
{

return codeBase;
}

public String getParameter(String param)
{
return mName+".djr";

}

public boolean isActive()
{
return true;
}

//AppletContext methods

public Applet getApplet(String name)
{
return null;
}

public Enumeration getApplets()
{
return (Enumeration)this;
}

public AudioClip getAudioClip(URL url)
{
return null;
}

public Image getImage(URL url)
{
return Toolkit.getDefaultToolkit().getImage(url);

}

public void showDocument(URL url){ }
public void showDocument(URL url, String target){ }
public void showStatus(String status){ }

//Enumeration methods

public boolean hasMoreElements()
{
return false;
}

public Object nextElement()
{
return null;
}
}

Listing 3 MovieLoader.java

/**
This class loads a Director movie applet and displays in a java Frame component
*/

import java.applet.*;
import java.awt.*;
import javax.swing.*;

public class MovieLoader extends JFrame
{

CustomClassLoader loader=null;
Class theClass=null;
Applet theApplet=null;
CustomStub stub=null;

public MovieLoader()
{
super("The sample Director movie");

try
{
//setting the size of the frame to 400X400
this.setSize(400,400);

//setting the background color of the frame to white
this.setBackground(Color.white);

//making the frame visible
this.show();

}catch(Exception e)
{
System.out.println(e.toString());
}


}

/**
* this method loads a named movie applet

*/
public void loadMovie(String movieName)
{
try
{

//initializing a CustomClassLoader object
loader=new CustomClassLoader();

//loading a named movie applet class through the 
CustomClassLoader  object
    theClass=loader.loadClass(movieName,true);

//casting the loaded class to an Applet class
theApplet=(Applet)theClass.newInstance();

//initializing the CustomStub class with the name of 
the movie applet
stub=new CustomStub(movieName);

//providing the applet a CustomStub object
theApplet.setStub(stub);

//setting the width and height of the applet
theApplet.setSize(400,400);

//adding the applet to the frame
this.getContentPane().add(theApplet);
}catch(Exception e){ System.out.println(e.toString());}

//calling the life cycle methods of the applet

theApplet.init();
theApplet.start();
validate();

}

public static void main(String args[])
{

new MovieLoader().loadMovie(args[0]);
}
}

Listing 4 NumberGenerator.java

class NumberGenerator
{

public NumberGenerator()
{

}

public int generateNumber()
{
java.util.Random random = new java.util.Random();
int i = random.nextInt(5)+1;
return i;
}
}

Listing 5 sample.java

import java.applet.Applet;
import java.awt.image.*;
import java.awt.*;
import java.util.*;
import java.net.*;

public class sample extends DirectorMovie
{

NumberGenerator generator = null;
int number = 0;
int usedNumber=0;

static public sample __m; //global movie variable declaration

public sample()
{

__m = this;

generator = new NumberGenerator();

}


/**
* --- Movie  Script Translations ---

*/

public void uberHandleAnEvent (Member member, int eventCode)
{

if ( member.castNumber == 1 && member.memberNumber == 8 ) 
//anslation of script: castMember8
{
   switch( eventCode )
  {
       case DirectorMovie.__MouseUp: // mouseUp
      {
        //finding if there is a number already 
displayed and then moving it out of stage
if(usedNumber != 0)
{
       __m.getSprite( usedNumber ).setLoc(new 
LPoint(  2000,2000 ).asPoint());
}

//obtaining a generated number from the 
NumberGenerator class
number=generator.generateNumber();

//  set the location of the sprite 
corresponding to the generated number
// to point(250,100)
__m.getSprite( number ).setLoc(new LPoint( 
250, 100 ).asPoint());

usedNumber = number;
break;
}
       default:    break;
      }
}
}
}




  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.