/* * MovieClip.java * Examples * * Created by Stuart MacKay on Sun Jun 13 2004. * Copyright (c) 2004 Flagstone Software Ltd. All rights reserved. * * This code is distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND Flagstone HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING * WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. */ package com.flagstone.translate.examples; import com.flagstone.transform.*; import com.flagstone.translate.*; import com.flagstone.transform.examples.*; import java.io.*; import java.util.*; /* This example shows how to use Translate to compile ActionScript to control a * movie clip. * * To run this example, type the following on a command line: * * java com.flagstone.translate.examples.MovieClip --file script [--resultDir path] * * where * * script is a file containing the ActionScript that will be executed to * control the movie clip. * * path is the directory where the Flash file generated by the example is * written to. * * IMPORTANT: When running the movie generated by the example in Internet Explorer, * you will need to click on the browser window so the Flash plug-in gets the keyboard * focus. */ public class MovieClip extends Example { public static void main(String[] args) { new MovieClip(args); } public MovieClip(String[] args) { super(args); int screenWidth = 8000; int screenHeight = 8000; int swfVersion = 5; String filename = getOption("file", ""); ASParser parser = new ASParser(); try { /* * The movie clip is the simplest possible - just a simple rectangle displayed * for a single frame. */ FSDefineShape rectangle = rectangle(movie.newIdentifier(), 2000, 2000, FSColorTable.red()); FSDefineMovieClip clip = new FSDefineMovieClip(movie.newIdentifier()); clip.add(new FSPlaceObject2(rectangle.getIdentifier(), 1, 0, 0)); clip.add(new FSShowFrame()); /* * Now generate the encoded array of FSClipEvent objects from the script and * add it to the object used to place the movie clip on the screen. * * The version number of Flash to encode to is required as the format of the * data structures holding the event information for a movie clip changed with * the releases of Flash 6 and Flash 7. */ String script = contentsOfFile(filename); byte[] events = parser.parse(script).encode(swfVersion); FSPlaceObject2 location = new FSPlaceObject2(clip.getIdentifier(), 1, events, "clip", screenWidth/2, screenHeight/2); /* * Put all the objects together in a movie. */ movie.setFrameRate(12.0f); movie.setFrameSize(new FSBounds(0, 0, screenWidth, screenHeight)); movie.add(new FSSetBackgroundColor(FSColorTable.lightblue())); movie.add(rectangle); movie.add(clip); movie.add(location); movie.add(new FSShowFrame()); writeFile("MovieClip.swf"); } catch (FileNotFoundException e) { System.err.println("Could not open file: " + filename); } catch (IOException e) { System.err.println("Could not read file: " + filename); } catch (ParseException e) { /* * If the parser detects an error in the script a ParseException * will be thrown. The parser can be used to report the error * and where it occurred in the script. */ System.err.println(parser.getError()); System.err.println("Line: " + parser.getLineNumber()); System.err.println(parser.getLine()); } } /* Defines a simple rectangle filled with a solid colour. * * @param identifier the unique identifier used to reference the shape definition. * @param width the width of the rectangle in twips. * @param height the height of the rectangle in twips. * @param color the colour used to fill the contents of the rectangle. */ private FSDefineShape rectangle(int identifier, int width, int height, FSColor color) { FSBounds bounds = new FSBounds(-width/2, -height/2, width/2, height/2); ArrayList lineStyles = new ArrayList(); ArrayList fillStyles = new ArrayList(); lineStyles.add(new FSSolidLine(20, FSColorTable.black())); fillStyles.add(new FSSolidFill(color)); ArrayList shapeRecords = new ArrayList(); shapeRecords.add(new FSShapeStyle(1, 1, 0, -width/2, -height/2)); shapeRecords.add(new FSLine(width, 0)); shapeRecords.add(new FSLine(0, height)); shapeRecords.add(new FSLine(-width, 0)); shapeRecords.add(new FSLine(0, -height)); return new FSDefineShape(identifier, bounds, fillStyles, lineStyles, new FSShape(shapeRecords)); } /* Returns the contents of the file as a single string. * * @param fileName the name of the file to read. * * @return a String containing the contents of the file. * * @throws FileNotFoundException if the file could not be found. * @throws IOException if an error occurred while reading the file. */ private String contentsOfFile(String fileName) throws FileNotFoundException, IOException { String script = ""; File aFile = new File(fileName); if (aFile.exists()) { byte[] fileIn = new byte[(int)aFile.length()]; FileInputStream fileContents = new FileInputStream(aFile); fileContents.read(fileIn); script = new String(fileIn); fileContents.close(); } else { throw new FileNotFoundException(); } return script; } }