Sample Code
Flash files can be built from scratch by simply constructing instances of objects that represent the respective Flash data structure and adding them to an FSMovie object in the order they will be executed by the Flash player.
FSMovie movie = new FSMovie(); movie.setFrameSize(new FSBounds(0, 0, 400, 400)); movie.setFrameRate(1.0); movie.add(new FSSetBackgroundColor(FSColor.lightblue())); movie.add(new FSDefineShape(shapeId, ....)); movie.add(new FSPlaceObject2(shapeId, layer, x, y)); movie.add(new FSShowFrame());
Complex animations are created in exactly the same way: defining the objects to be displayed then adding, updating or removing them from the display list.
The movie is then encoded to transform the objects into the binary representation that will be decoded and executed by the Flash Player:
try {
movie.encodeToFile(filename);
}
catch (Exception e) {
... Process Exception ...
}
Transform SWF can also be used to parse existing Flash files and create objects for each of the data structures decoded from the file.
try {
FSMovie movie = new FSMovie(filename);
}
catch (Exception e) {
... Process Exception ...
}
The objects may be inspected and their attributes changed accordingly allowing Flash files to be used as templates to deliver customised presentations.
More detailed examples are available to get you started with the Transform SWF framework and how to use the concepts supported in Flash.
High Level Examples
The classes in the com.flagstone.transform.util package of the framework are designed to make it easy to load images, sounds and fonts stored in files and generate the objects required to display them in a movie.
FSImageConstructor generator = new FSImageConstructor("image.png");
// Generate the object containing the shape definition.
FSDefineObject image = generator.defineImage(imageId);
// All images are displayed as a bitmap fill inside a shape.
FSDefineShape3 shape = generator.defineEnclosingShape(shapeId, imageId,
-xOrigin, -yOrigin, new FSSolidLine(borderWidth, borderColor));
// Add all the objects together to display the image.
movie.add(image);
movie.add(shape);
movie.add(new FSPlaceObject2(shapeId, layer, x, y));
movie.add(new FSShowFrame());
Sounds can be played in response to events, such as a button being clicked:
FSSoundConstructor generator = new FSSoundConstructor("beep.wav");
//Add the sound definition and start playing it immediately.
movie.add(generator.defineSound(soundId));
movie.add(new FSStartSound(new FSSound(soundId, FSSound.Start)));
The FSSoundConstructor class can also be used to stream sounds allowing a sound track to be easily added to a movie:
FSSoundConstructor generator = new FSSoundConstructor("soundtrack.mp3");
// Generate the header that describes the streaming sound.
movie.add(generator.streamHeader(samplesPerBlock));
//Play a block of sound for each frame shown.
for (int i=0; i<numberOfBlocks; i++)
{
movie.add(generator.streamBlock(i, samplesPerBlock));
movie.add(new FSShowFrame());
}
Displaying text is greatly simplified using the FSTextConstructor. The class may be used to generate simple strings using TrueType, Flash or AWT font files. Support for blocks of text and also Unicode strings is included.
FSTextConstructor generator = new FSTextConstructor(fontId, "Arial.ttf"); String message = "Display this string."; FSDefineText2 text = generator.defineText(textId, message, size, color); FSDefineFont2 font = generator.defineFont(); movie.add(font); movie.add(text); movie.add(new FSPlaceObject2(textId, layer, x , y)); movie.add(new FSShowFrame());
The FSShapeConstructor class adds a powerful tool for drawing arbitrary paths and complex shapes. A full range of drawing commands is supported including quadratic and Bezier curves. The class also supports commands for drawing simple geometric shapes.
FSShapeConstructor path = new FSShapeConstructor(); /* * The line and fill styles used to render the shapes should be * specified before drawing starts. */ path.add(new FSSolidLine(borderWidth, borderColor)); path.add(new FSSolidFill(fillColor)); /* * Draw a rectangle with the origin at the centre of the shape. */ newPath(); setStyle(0, 0); move(xOrigin-width/2, yOrigin-height/2); rline(width, 0); rline(0, height); rline(-width, 0); rline(0, -height); closePath(); movie.add(path.defineShape(identifier));
The simplified examples shown are to illustrate how each of the classes in the framework are used. Detailed, working code is included on the Examples page.