/* * ShowImage.java * Examples * * Created by Stuart MacKay on Fri Jul 25 2003. * Copyright (c) 2001-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.transform.util.examples; import com.flagstone.transform.examples.Example; import com.flagstone.transform.*; import com.flagstone.transform.util.*; import java.io.*; /* * This example shows how image can be displayed using the FSImageConstructor. * * To run this example, type the following on a command line: * * java com.flagstone.transform.util.examples.ShowImage \ * --file image-file [--resultDir path] * where * * image-file is the path to a file containing either BMP, PNG or JPEG image. * * resultDir is the directory where the Flash file generated by the example is * written to. * * The name of the Flash file generated is the same as the image file with the * file extension replaced with a '.swf' suffix. */ public class ShowImage extends Example { private FSImageConstructor imageGenerator = null; private FSMovie movie = new FSMovie(); public static void main(String[] args) { new ShowImage(args); } public ShowImage(String[] args) { super(args); String imageFile = getOption("file", ""); try { /* Create the FSImageConstructor object used to generate the object * containing the image definition. Initialize it using the name of * the image file passed on the command line when the exanple was run. */ imageGenerator = new FSImageConstructor(imageFile); /* Create the name of the Flash file generated using the name of the * font file - adding a '.swf' suffix. */ int first = imageFile.lastIndexOf(File.separatorChar); int last = imageFile.lastIndexOf('.'); String fileOut = resultDir + File.separator + imageFile.substring(first, last) + ".swf"; createMovie(imageFile); movie.encodeToFile(fileOut); } catch (Exception e) { /* Several exceptions could be thrown to get to this point. FSImageConstructor * will throw a FileNotFoundException, IOException or DataFormatException * if the image format is not support or if an error occurs while * loading and decoding the image data. * * The FSMovie will throw a FileNotFoundException or IOException if * there is a problem generating the Flash file. * * The exceptions are all caught in a single handler to make the code more * readable. */ System.err.println("Cannot create movie for the image: " + imageFile); } } public void createMovie(String filename) { int imageId = movie.newIdentifier(); int shapeId = movie.newIdentifier(); int xOrigin = imageGenerator.getWidth()/2; int yOrigin = imageGenerator.getHeight()/2; // Generate the image defintion FSDefineObject image = imageGenerator.defineImage(imageId); /* * All images must be displayed as a bitmap fill inside a shape. The * FSImageConstructor class generates the shape enclosing the image. * If no border is required then the line style may be set to null. */ FSDefineShape3 shape = imageGenerator.defineEnclosingShape(shapeId, imageId, -xOrigin, -yOrigin, new FSSolidLine(20, FSColorTable.black())); /* * Add all the objects together to create the movie. */ movie.setFrameRate(1.0f); movie.setFrameSize(shape.getBounds()); movie.add(new FSSetBackgroundColor(FSColorTable.lightblue())); movie.add(image); movie.add(shape); movie.add(new FSPlaceObject2(shapeId, 1, 0, 0)); movie.add(new FSShowFrame()); } }