我正在尝试将Java2D中矢量图像drawin的输出保存到SWF文件中.有很多用于保存java2D输出的库,如SVG(BATIK)和PDF(itext),但我找不到一个用于SWF.有任何想法吗?
我刚刚得到一个使用Adobe Flex 3中的SpriteGraphics2D对象的示例.仅供参考...... Flex 3现在是开源的.
(来自SpriteGraphics2D javadoc) SpriteGraphics2D是Java2D的Graphics2D API的SWF特定实现.对此类的调用将转换为可用于构造SWF Sprite的TagList.
我通过查看CubicCurveTest.java和SpriteTranscoder.java这两个类来解决这个问题.
运行此示例所需的两个罐子是swfutils.jar和batik-awt-util.jar,可以在这里下载.
这是我的示例代码......
// 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);