gpt4 book ai didi

java - 将 Java2D 保存到 SWF (flash)

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:13:02 26 4
gpt4 key购买 nike

我正在尝试将 Java2D 中绘制的 vector 图像的输出保存到 SWF 文件中。有很多很棒的库可以将 java2D 输出保存为 SVG (BATIK) 和 PDF(itext) 之类的东西,但我找不到一个用于 SWF 的库。有什么想法吗?

最佳答案

我刚得到一个使用 SpriteGraphics2D 的例子来自 Adobe's Flex 3 的对象.仅供引用...Flex 3 现在是开源的。

(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.

我通过查看这两个类发现了这一点 CubicCurveTest.javaSpriteTranscoder.java .

运行此示例所需的唯一两个 jar 是 swfutils.jar 和 batik-awt-util.jar,它们可以是 downloaded here .

这是我的示例代码...

     // 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);

关于java - 将 Java2D 保存到 SWF (flash),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/267001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com