/*
 *  ChangeColour.java
 *  Cookbook
 *
 *  Copyright (c) 2001-2009 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.
 */

import com.flagstone.transform.*;

import java.util.*;

/*
 * This example shows how to change the colour of a shape:
 *
 * To run this example, type the following on a command line:
 *
 *     java -cp cookbook.jar ChangeColour file-out
 *
 * where
 *
 *     file-out is the path where the file will be written. If no output file 
 *     is specified then a file named after the example will be written to the 
 *     current directory.
*/
public class ChangeColour
{
	public static void main(String[] args)
	{
        try
        {
        	String out = args.length == 0 ? "ChangeColour.swf" : args[0];
        	ChangeColour example = new ChangeColour();         
            FSMovie movie = new FSMovie();            
            example.createMovie(movie);
            movie.encodeToFile(out);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        }
	}
    
	void createMovie(FSMovie movie)
    {
        int xLower = 0;
        int yLower = 0;
        int xUpper = 8000;
        int yUpper = 8000;
        
        float framesPerSecond = 1.0f;

		movie.setFrameRate(framesPerSecond);
        movie.setFrameSize(new FSBounds(xLower, yLower, xUpper, yUpper));
        movie.add(new FSSetBackgroundColor(FSColorTable.lightblue()));
        
        FSDefineShape rectangle = createRectangle(movie);
        movie.add(rectangle);

        movie.add(new FSPlaceObject2(rectangle.getIdentifier(), 1, 4000, 4000));
        movie.add(new FSShowFrame());
                       
        /*
         * Change the colour of the rectangle. Here the terms are added to the
         * colour defined for the solid fill style of the rectangle.
         */
        movie.add(new FSPlaceObject2(1, new FSColorTransform(-255, 255, 0)));
        movie.add(new FSShowFrame());
 
        /*
         * Change the colour of the rectangle by changing the saturation of the colours
         * by specifying a multiplier for each channel. The alpha channel is omitted
         * so the transparency is not changed.
         */
        movie.add(new FSPlaceObject2(1, new FSColorTransform(0.7f, 1.0f, 1.0f)));
        movie.add(new FSShowFrame());
        
        /*
         * Both the add and multiply transforms can be performed in a single step.
         */
        movie.add(new FSPlaceObject2(1, new FSColorTransform(0.9f, 1.0f, 1.0f, 0, 128, 128)));
        movie.add(new FSShowFrame());
    }
	
	private FSDefineShape createRectangle(FSMovie movie)
	{
        int identifier = movie.newIdentifier();
        
        int width = 4000;
        int height = 4000;
        
        FSBounds bounds = new FSBounds(-2000, -2000, 2000, 2000);
        
        ArrayList lineStyles = new ArrayList();
        ArrayList fillStyles = new ArrayList();
        
        lineStyles.add(new FSSolidLine(20, FSColorTable.black()));
        fillStyles.add(new FSSolidFill(FSColorTable.red()));
        
        /* 
         * Create the outline of the shape.
         */
        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));
	}
}