Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Java2D to SWF (flash)

Tags:

java

flash

I'm trying to save the output of an vector image drawin in Java2D to an SWF file. There are great libraries for saving java2D output as things like SVG (BATIK) and PDF(itext) but I can't find one for SWF. Any ideas?

like image 327
wburke Avatar asked Nov 05 '08 22:11

wburke


People also ask

How do I save a SWF file?

Export SWF files Open the Animate document to export, or select the frame or image to export in the current document. Select File > Export > Export Movie or File > Export > Export Image. Enter a name for the output file. Select the file format and click Save.

Does SWF file have audio?

SWF is a format designed to store vector graphics and animation, it may contain sound, video, text and other data. Such files are widely used for creating animation, games and playing video and audio on web pages.


1 Answers

I just got an example to work using the SpriteGraphics2D object from Adobe's Flex 3. FYI... Flex 3 is now open source.

(from SpriteGraphics2D javadoc) SpriteGraphics2D is a SWF specific implementation of Java2D's Graphics2D API. Calls to this class are converted into a TagList that can be used to construct a SWF Sprite.

I figured this out by looking at these two classes CubicCurveTest.java and SpriteTranscoder.java.

The only two jars needed to run this example are swfutils.jar and batik-awt-util.jar which can be downloaded here.

Here is my example code...

     // Create the SpriteGraphics2D object
     SpriteGraphics2D g = new SpriteGraphics2D(100, 100);

     // Draw on to the graphics object
     Font font = new Font("Serif", Font.PLAIN, 16);
     g.setFont(font);         
     g.drawString("Test swf", 30, 30);         
     g.draw(new Line2D.Double(5, 5, 50, 60));
     g.draw(new Line2D.Double(50, 60, 150, 40));
     g.draw(new Line2D.Double(150, 40, 160, 10));

     // Create a new empty movie
     Movie m = new Movie();
     m.version = 7;
     m.bgcolor = new SetBackgroundColor(SwfUtils.colorToInt(255, 255, 255));
     m.framerate = 12;
     m.frames = new ArrayList(1);
     m.frames.add(new Frame());
     m.size = new Rect(11000, 8000);

     // Get the DefineSprite from the graphics object
     DefineSprite tag = g.defineSprite("swf-test");

     // Place the DefineSprite on the first frame
     Frame frame1 = (Frame) m.frames.get(0);
     Matrix mt = new Matrix(0, 0);
     frame1.controlTags.add(new PlaceObject(mt, tag, 1, null));

     TagEncoder tagEncoder = new TagEncoder();
     MovieEncoder movieEncoder = new MovieEncoder(tagEncoder);
     movieEncoder.export(m);

     //Write to file
     FileOutputStream fos = new FileOutputStream(new File("/test.swf"));
     tagEncoder.writeTo(fos);
like image 59
delux247 Avatar answered Nov 08 '22 10:11

delux247