tags:

views:

1166

answers:

8

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?

A: 

Flash generally uses SVG as its native format. You could export to SVG and then write a trivial Flash program to embed and display the image. But why use Flash at all? Most browsers are capable of displaying SVG nowadays.

Edit: I don't know why I was downvoted for a clearly correct answer. I can understand not being upvoted for not having the best answer (I fully acknowledge that this soltuion isn't ideal), but I really don't think it deserves a downvote. You can consult the section in the middle of this page about embedding SVG in Flex programs. For those of you who aren't familiar, Flex is an adobe toolkit/library that generates SWF programs that run in the normal Flash player. Many (all?) of the normal Flash libraries are available to it. It uses ActionScript. For you doubters, here's a snippet showing exactly how you do it, copied from a Flex program I wrote. It should go inside an MXML file.

<mx:Script><![CDATA[

[Embed(source="../images/down.svg")]
[Bindable]
protected var drillDownImage:Class;

]]></mx:Script>

<mx:HBox width="50%" horizontalAlign="right">
    <mx:Image id="drillDownButton" source="{drillDownImage}" height="20" width="20" click="drillDown();" />
</mx:HBox>

Now all you need to do is wrap that in an appropriate MXML file, maybe import some controls packages for the HBox and Image tags, and you're good to go. The example in the link I provided should be sufficient for you to figure it out.

rmeador
I got one of your down votes taken away. My flash developer (who only does flash, not flex) was convinced that there was no way to do direct display of svg in flash. It looks like flex can convert (I think via batik actually) svg into flash but it is not the native swf format.
wburke
+2  A: 

Mm, I know some libraries outputting Flash content, like or Ming, or even haXe, a language which can be "translated" into JavaScript, Flash or PHP code... But I know no Java library.

Searching a bit (I am curious), I found a commercial Java Graph Library, probably closed source, a Flash player in Java, libraries to manipulate ActionScript source code or bytecode... Ah, the latter points to JavaSWF2 which is supposed to be able to generate SWF. I found also a DrawSWF which uses... JavaSWF2 library as back-end!

PS.: Also found Transform SWF. Looks promising.

PhiLho
A: 

Thanks for the responses guys. I've seen most of these products but haven't been able to get the right angle yet. If flash can import and display SVG then I can use Batik to write out SVG and send it down to the flash client.

We have a web app servicing a heavy flash front end. Our app used to run all on a local network but is being rebuilt to run from a datacenter. I am trying to replace shipping large rendered JPGs with a vector format that will be quicker to download and will render correctly in flash. Currently we are dynamically generating jpgs, but we can also generate svg. It would be nice to draw directly to swf (Batik lets you draw directly to a custom Java2D graphics context that generates SVG from the primitive drawing commands) and skip the svg step but I'll take what I can get.

wburke
A: 

As far as I know flash does not use SVG at all. I'm not at all convinced that the answer mentioning that is correct. (But I can't just add this as a comment as I wanted to as I don't have enough points...)

John Burton
A: 

John,

I just talked with our head flash developer and he agrees with you. That leaves me back where I started.

wburke
I updated my answer to show an example of what I was talking about. You may want to consider getting a new lead flash developer.
rmeador
+1  A: 

This probably isn't what you are looking for but I'll throw it in as a suggestion -

Have you considered making your java output actionscript to draw the vectors using the flash drawing API and compile it to a .SWF file using the free flex as3 compiler? If you don't use the flex library it doesn't include any of it. Your java code would just output the drawing lines surrounded by a template for the actionscript code.

This makes Test.swf that draws a square and compiles down to 610 bytes for me.

If you need to convert on the fly then this would be slow but for preconverted drawings it would be simple.

package{
  import flash.display.Sprite;

  public class Test extends Sprite{
    public function Test (){

      // Start of lines generated by your java
      graphics.lineStyle(1, 0, 1);
      graphics.lineTo(100, 0);
      graphics.lineTo(100, 100);
      graphics.lineTo(0, 100);
      graphics.lineTo(0, 0);
      // End of lines generated by your java
    }
  }
}
John Burton
+8  A: 

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);
delux247
This looks really good! You'd need to check the license though, there seems to be an obligation to publish the source but that *may* be just to changes you make to the library. I've not read it in detail.
John Burton
A: 

Check out Transform SWF. The API takes some time to get used to, but it comes with some really helpful examples to get you started. I've used it myself and it work very well.

My advice would be to skip SVG altogether as a way to go from Java2D to SWF. The SVG that Batik produces behaves very badly with the solutions I tried to import SVG into Flash.

Rich Apodaca