|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Product Reviews Java & Macromedia, a perfect match
Java & Macromedia, a perfect match
By: Samudra Gupta
Feb. 1, 2001 12:00 AM
How many times have we pulled out our hair trying to find a proper way to deploy high-end graphics and animation over the Web? The answer is perhaps a bit embarrassing. In DHTML concepts, with the help of JavaScript and layered components, we could render interactivity with graphics and produce some animation effects, but those were far from what we desired and what existing multimedia packages could offer for PC-based games and animation programs. By the time Java came into the picture it offered graphics-handling features, which perhaps put a ray of hope into the developer's world. Despite the fact that Java could handle graphics, it was to a limited degree and had inherent problems with graphics rendering, such as flickering, and frequent repaint problems. It was (should I say is?) a head-breaking task to write extra code to avoid those problems. Moreover, in a browser environment Java applets took a fairly long time to download and display heavy graphics, calling into question their potential as a solution. Then Macromedia came up with award-winning products such as Flash, and developers could at last find a real solution to the problem. The Macromedia product families used vector-based graphics and framed animation and successfully produced animation in a compressed format called Shockwave, which had considerably smaller files and took less time to download into the client browser. So with all their rich features, Macromedia products immediately became the most popular way to present graphics and animation over Internet browsers.
Director: The Choice
We can view Director movies in one of three ways:
Director then offered cross-platform compatibility through its internal conversion engine, called Xtra, that transformed Director movies into Java applets, although Java applets aren't yet as smooth as their Shockwave equivalents. They work, however, and platform independence was achieved, making it useful for those with something other than Windows or Macintosh. The quality of graphics and the animation in the applets were dependent on the size and resolution of the images used to render the movie. Everything is apparently set and done for a platform-independent, high-end graphics and animation deployment framework. But human beings are never satisfied, and we're still frustrated. The possibility of transforming Director movies to Java applets tempted us to apply the same rich functionality of graphics and animation outside the domain of the browser and, more important, in a platform-independent manner. Unfortunately, Macromedia didn't offer a ready-made, one-shot solution for this through Director. They offered projectors that are .exe files to run the movies as a stand-alone, but they're not platform independent. In reality it's not too difficult to work out a solution to the problem. In fact, the bottom line is that we have to find a mechanism through which we can run the Director movie applets, loading them into a container such as Java Frame or a Java Window. Interestingly, Director allows us to produce source code of the movie applet along with a few other helper .class files to run it. We can manipulate the applet source file depending on our need and even embed our own objects that may in turn be responsible for activities such as database access, complex networking, or even RMI. In the end what we have is a powerful mechanism to build a truly platform-independent graphics and animation framework.
Creating the First Applet
When we want to customize the applet on our own, we must save the movie by selecting the option "Save as Source." Although things seem to be straightforward (indeed they are), it's unwise to assume at an early stage that everything within a Director movie can be converted to an applet. In fact, there are limitations (we'll discuss them in due course). But to be optimistic, we can reasonably say that a Director movie, with simpler event handling and animation features, can more or less be converted to an applet. Before covering the applet issues, it's worth discussing Director terminology and looking into Lingo, the scripting language of Director and the basic architecture of the conversion of Director movies into Java applets. Some Useful Director Terminology
Lingo is Director's scripting language and a 4GL language with a lot of power. It offers:
As mentioned, the Director movie is a kind of framed animation, and each sprite on the stage is assigned several behaviors. We can control these behaviors with the help of Lingo and, loosely speaking, that's the key to Director movie animation. It's possible to write two kinds of scripts with Lingo. A frame script controls the behavior of the movie when it reaches a particular frame. A behavior script controls the sprite's behavior, such as dancing and movement. By attaching custom scripts to the sprites and cast members, we can achieve a high level of framed animation. It's recommended that the developer consult a thorough tutorial on Lingo before developing complicated movies in Director.
How the Movie Works as an Applet
Every running applet has exactly one instance of movie- Name.class, which is the first object created when the applet runs. This object reads the media file, creates the score and cast data structures, then begins to play the movie. According to the movie's tempo (the number of frames covered in a second), this object periodically advances the frame counter, dispatches frame events to active sprites and their behaviors, and redraws the stage. As the applet receives mouse and key events, this object dispatches the events to movie, sprite, and cast member scripts. The DirectorMovie class's public API supports Lingo commands that manipulate the entire movie and access to sprite and cast member objects.
Standard Java Classes
Core Classes
DirectorMovie (extends Applet)
Member
A Member object contains the data and functions required to load, draw, and use a single cast member. The Member class handles all the built-in cast member types - shape, sound, bitmap, field, transition, and script. We can create custom subclasses of Member. The Member class's public API primarily provides get-and-set access to Lingo-visible properties, such as getWidth() and setText(String). The DirectorMovie.getMember() functions fetch an existing member object given its name or number. Use the Sprite.getMember() function to fetch the cast member attached to a particular sprite.
Sprite
A sprite object contains the data and functions required to animate a single sprite, including stepping and interpreting the score and dispatching events to behaviors. The sprite class handles all built-in sprite types, and we can create custom subclasses of sprites. The sprite class's public API primarily provides get-and-set access to Lingo-visible properties, such as getInk() or setLocH(). We can use the DirectorMovie.getSprite() function to fetch a sprite object by number and manipulate the sprite's properties by the public API of the sprite class. The Behavior.__s property is a reference to the sprite object that a particular behavior is attached to.
Behavior
When the export Xtra for Java translates scripts to Java, it tries to determine whether a variable or parameter's data type is one of Java's built-in data types: int, String, float, or Boolean. If the Xtra can determine that the variable or parameter is one of Java's built-in data types, it takes the same data type in Java. Sometimes the Xtra can't determine the data type. For example, several set statements assign a variable different data types if the type isn't a built-in Java data type. In these situations the Xtra gives the variable or parameter the Java type LVal, or "Lingo value." An LVal object can be a null, integer, string, double, sprite, member, symbol, linear list, property list, rect, or point data type. Various subclasses of LVal handle the possible types of data that an LVal object can hold. The player creates and destroys Lingo values as required. After Java creates a Lingo value object, the object can't change its type.
These classes have no public API. The Xtra includes them in the player when they're required.
Classes Generated by the Export Xtra for Java
Following are some known tasks that custom Java can't do in the Director player for Java. JavaBeans can't easily be embedded because they're drawn differently than Director cast members and sprites. To embed the bean we have to rewrite the bean's source as a class that inherits from the member and sprite. AWT components and functionality can't be integrated because they're drawn differently than Director draws. Director's rich compositing model uses a custom Java engine that doesn't mesh with the AWT drawing model. Java's other inherent limitations, including security restrictions and lack of direct connection with the operating system, also limit how much we can customize Java in an applet. Not all the events that Lingo is capable of handling are converted to the applet. For example, a doubleClick event can't be directly converted to an applet. Many animation and movie features included in Director can't be converted to Java applets.
The Framework
We'll create the applet, provide a stub and context for it, place it in a container to be displayed, and execute its actions and properties. This is possible only if the applet is the kind of component that can be placed inside a container. If we look into the Java class hierarchy, we see that the applet class inherits from the panel class that in turn is inherited from the component class (see Figure 1). An applet also inherits the properties of a component, and it should be possible to place it inside a standard Java container, such as a frame or window, as simply as any other graphics component, like Button or TextField. So far it's as simple as that. But applets are a special type of component in the sense that they're not static like AWT components, but possess their own life cycles to execute. At the same time they can also act as a container that can be a placeholder for other AWT components. In a nutshell, to design and implement a framework for playing the Director movie applets as stand-alone applications we need to achieve the following:
The CustomClassLoader class extends from the java.lang.ClassLoader, an abstract class that contains an abstract method called loadClass (String className, Boolean resolve) (see Listing 1). If the resolve flag is true, the method should call the resolveClass() method of the resulting Class object. Our CustomClassLoader class implements this method and returns the Class object specified in the variable className. Following is the code snippet doing the required job:
. . . . The advantage of putting the resolved class into a hashtable is that the same class doesn't get loaded twice; if it's already been loaded, it's fetched from the hashtable and returned as the class object. The CustomStub class implements both AppletStub and AppletContext interfaces defined in the java.applet package and provides the custom implementation of the methods defined within those two interfaces (see Listing 2). In our program the CustomStub constructor expects movieName to be passed because the movie applet queries the getParameter(ShockwaveMediaFile) about the .djr file to be loaded. This is normally passed via the PARAM tag of the APPLET tag in the HTML file. To return a proper .djr file name, the CustomStub needs to know the name of the movie, which it does via the constructor. The next step is to implement a movieLoader class that's also a container and can contain an applet and attach the necessary CustomStub object to the applet (see Listing 3). This class essentially makes a call to the loadClass() method of the CustomClassLoader class, passing the name of the movie applet. It's important to note that loading the applet doesn't mean the applet is executing its life cycle. We have to explicitly call the life cycle methods of the applet (init(), start(), destroy()) as needed and provide it a CustomStub object to see it perform the actions defined within them. The following lines of the movieLoader class do precisely that.
CustomClassLoader loader=new CustomClassLoader(); The overall proposed architecture is presented in Figure 2.
Live Example
The Movie
The NumberGenerator Class
Creating the Movie Applet
On mouseDown me
set the location of Sprite(1) to point(250,100). The script typically handles a mouseDown event on the button and brings any particular sprite to the determined location (250,100). If we don't attach any script to the button before converting it to the Java applet, the button sprite won't be configured to register any event through the Java code. This occurs because all the behaviors of the sprites are registered inside the behavior class. Each member of the movie has to be notified in a way that it's capable of handling an event on it. This is according to Macromedia's model of handling the movies as Java applets. Having done that, if we save the movie as the source Java file (from the "Save as Java" option), we'd see the sections of the code shown below. The movie class (in this case I've named the movie as Sample) extends the DirectorMovie class, which in turn extends the Applet class (see Listing 5).
public class sample extends DirectorMovie Following is an event-handling method that typically holds the translation of the movie scripts:
// --- Movie Script Translations --- The event handling contains the Java equivalent translation of the Lingo script we attached to the button. We need to modify this code to our specific needs to show the generated number sprite on the stage.
Adding Our Own Objects
NumberGenerator generator= null; Then we initialize the object in the constructor of the sample.java. generator=new NumberGenerator(); Now we need to write the code to obtain the number by invoking the generateNumber() method of the NumberGenerator class. We do this in the event-handling portion of the sample.java and bring the corresponding sprite to the predetermined point (250,100) location (see Listing 5). Each time we click the button, a number is generated and shows the relevant sprite. But remember we also have to remove any previous sprite present on the stage. There are two ways to do this:
Setting the Classpath
Running the Application
java movieLoader sample and the movie appears (see Figure 3). We can generate and see the number by clicking on the button on the stage.
Conclusion
Resources
Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||