import java.io.*; import com.anotherbigidea.flash.*; import com.anotherbigidea.flash.interfaces.*; import com.anotherbigidea.flash.writers.*; import com.anotherbigidea.flash.structs.*; /** * An example of writing directly to the SWF file format interfaces. * Create a movie consisting of 4 instances of a Movie Clip containing * a rotating red square. * * Compare this to RedSquare.java (This was RedSquare2.java) */ public class Orbit13 { public static final int sc = 200; public static Rect outline2 = new Rect(-14*sc, -14*sc, 14*sc, 14*sc); public static SWFShape planet(SWFTagTypes swf, int id, double x, double y, double r, Color c, int n) throws IOException { SWFShape s = swf.tagDefineShape(id, outline2); s.defineFillStyle(c); s.setFillStyle1(1); s.defineLineStyle(20, new Color(0,0,0)); s.setLineStyle(1); int lastX = (int) (x + sc*r); int lastY = (int) y; s.move(lastX, lastY); for ( int i = 1; i <= 2*n; i++ ) { double angle = i * Math.PI/n; int thisX =(int)(x + sc*r * Math.cos(angle)); int thisY =(int)(y + sc*r * Math.sin(angle)); //s.line(thisX, thisY); // Maybe not delta's but relative to first point? s.line(thisX - lastX, thisY - lastY); lastX = thisX; lastY = thisY; } s.done(); return s; } public static void axes(SWFTagTypes swf, int id, int b) throws IOException { SWFShape s = swf.tagDefineShape(id, outline2); s.defineLineStyle(10, new Color(0,0,0)); s.setLineStyle(1); s.move(-b,0); s.line(2*b,0); s.move(0,-b); s.line(0,2*b); s.done(); } public static void orbit(SWFTagTypes swf, int id, int r) throws IOException { SWFShape s = swf.tagDefineShape(id, outline2); s.defineLineStyle(20, new Color(0,255,255)); s.setLineStyle(1); int n = 16; double x = 0; double y = 0; int lastX = (int) (x + sc*r); int lastY = (int) y; s.move(lastX, lastY); for ( int i = 1; i <= 2*n; i++ ) { double angle = i * Math.PI/n; int thisX =(int)(x + sc*r * Math.cos(angle)); int thisY =(int)(y - sc*r * Math.sin(angle)); //s.line(thisX, thisY); // Maybe not delta's but relative to first point? s.line(thisX - lastX, thisY - lastY); lastX = thisX; lastY = thisY; } s.done(); } public static void orbit2(SWFTagTypes swf, int id, int r) throws IOException { SWFShape s = swf.tagDefineShape(id, outline2); s.defineLineStyle(20, new Color(0,0,0)); s.setLineStyle(1); double x = 0; double y = 0; int n = 64; int lastX = (int) (x + sc*r + sc); int lastY = (int) y; int thisX; int thisY; s.move(lastX, lastY); for ( int i = 1; i <= 2*n; i++ ) { double angle = i * Math.PI/n; thisX =(int)(x + sc*r * Math.cos(angle) + sc*Math.cos(12*angle)); thisY =(int)(y - sc*r * Math.sin(angle) - sc*Math.sin(12*angle)); // Maybe not delta's but relative to first point? s.line(thisX - lastX, thisY - lastY); lastX = thisX; lastY = thisY; } s.done(); } /** /** * First arg is output filename */ public static void main( String[] args ) throws IOException { SWFWriter writer = new SWFWriter( args[0] ); SWFTagTypes swf = new TagWriter( writer ); swf.header( 5, //Flash version -1, //unknown length 550 * SWFConstants.TWIPS, //width in twips 400 * SWFConstants.TWIPS, //height in twips 12, //frames per sec -1 ); //unknown frame count swf.tagSetBackgroundColor( new Color(255,255,255) ); axes(swf, 1, 2000); orbit(swf, 2, 12); orbit2(swf, 3, 12); planet(swf, 4, 0, 0, .3, new Color(255, 255, 150), 16); // moon planet(swf, 5, 0, 0, .5, new Color(0, 255, 255), 16); //planet planet(swf, 6, 0, 0, 1, new Color(255, 255, 0), 16); //sun //--define a sprite (movie clip) SWFTagTypes sprite = swf.tagDefineSprite( 20 ); //id =20 int r = 12*sc; Matrix matrix = new Matrix( 1, 1, 0, 0, r, 0); //--place the shape in the first frame of the sprite sprite.tagPlaceObject2(false, -1, 1, 1, new Matrix(), null, -1, null,0); sprite.tagPlaceObject2(false, -1, 2, 2, new Matrix(), null, -1, null,0); sprite.tagPlaceObject2(false, -1, 3, 3, new Matrix(), null, -1, null,0); sprite.tagPlaceObject2(false, -1, 5, 5, matrix, null, -1, null, 0); sprite.tagPlaceObject2(false, -1, 6, 6, new Matrix(), null, -1, null,0); Matrix matrix2 = new Matrix( 1, 1, 0, 0, 13*r/12, 0); sprite.tagPlaceObject2(false, -1, 4, 4, matrix2, null, -1, null, 0); sprite.tagShowFrame(); //end the first frame int n = 64; //--Rotate the square (using degrees for clarity) for( int i = 1; i < 2*n; i++ ) { //--Convert degrees to radians double radians = i * Math.PI / n; //--Create a rotation matrix double sin = Math.sin( radians ); double cos = Math.cos( radians ); double sin2 = Math.sin( 12*radians ); double cos2 = Math.cos( 12*radians ); //Matrix matrix = new Matrix( cos, cos, sin, -sin, 0, 0 ); matrix = new Matrix(1, 1, 0, 0, (int)(r * cos), -(int)(r * sin)); matrix2 = new Matrix(1, 1, 0, 0, (int)(r * cos + sc*cos2), -(int)(r * sin + sc*sin2)); //--Alter the square using the transformation matrix sprite.tagPlaceObject2(true, -1, 5, -1, matrix, null, -1, null, 0); sprite.tagPlaceObject2(true, -1, 4, -1, matrix2, null, -1, null, 0); sprite.tagShowFrame(); } //--End the sprite timeline sprite.tagEnd(); //--changed was Place 4 instances of the sprite (at depths 1,2,3,4) swf.tagPlaceObject2( false, -1, 1, 20, new Matrix(4500,4500), null, -1, null, 0 ); //--End the first frame of the main timeline swf.tagShowFrame(); //--End the main timeline swf.tagEnd(); } }