Java 在 JPanel 上绘制 SVG 图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20664107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Draw SVG images on a JPanel
提问by Jochem Kuijpers
I want to draw SVG images, scaled, rotated, coloured and with an alpha layer onto a JPanel in my Java application. How can I achieve this? There may be multiple of these images overlapping.
我想在我的 Java 应用程序的 JPanel 上绘制 SVG 图像、缩放、旋转、着色并带有 alpha 层。我怎样才能做到这一点?可能有多个这些图像重叠。
I don't really know how to use SVG images in Java so please explain from the start, not only the rendering process :)
我真的不知道如何在 Java 中使用 SVG 图像,所以请从头开始解释,不仅仅是渲染过程:)
Thanks in advance!
提前致谢!
采纳答案by Dodd10x
Use Batik (http://xmlgraphics.apache.org/batik/) or SVGSalamander (https://svgsalamander.java.net/). I've used batik successfully before but I have not tried SVGSalamander.
使用蜡染 ( http://xmlgraphics.apache.org/batik/) 或 SVGSalamander ( https://svgsalamander.java.net/)。我以前成功使用过蜡染,但我还没有尝试过 SVGSalamander。
In batik, there is an SVG panel that will display the image for you and add keyboard/mouse shortcuts for zooming, panning, and rotating the image. You can disable these shortcuts and implement your own mechanisms, however.
在蜡染中,有一个 SVG 面板可以为您显示图像并添加用于缩放、平移和旋转图像的键盘/鼠标快捷键。但是,您可以禁用这些快捷方式并实现您自己的机制。
Also, with some work you can overlap the images.
此外,通过一些工作,您可以重叠图像。
Just be sure to read the FAQs.
请务必阅读常见问题解答。
回答by PaulProgrammer
You need to get the SVG image into a type that can be displayed by the JPanel -- I'm going to assume you already know how to use BufferedImage
to display e.g. a PNG, and that you don't need to edit the SVG, just display it.
您需要将 SVG 图像转换为可以由 JPanel 显示的类型——我假设您已经知道如何使用BufferedImage
来显示例如 PNG,并且您不需要编辑 SVG,只需显示它。
The key here is that Java doesn't have native support for SVG. You have to use a library like batikto load and convert the image to a displayable format.
这里的关键是 Java 没有对 SVG 的原生支持。您必须使用像蜡染这样的库来加载图像并将其转换为可显示的格式。
I stole this answer from http://bbgen.net/blog/2011/06/java-svg-to-bufferedimage/
我从http://bbgen.net/blog/2011/06/java-svg-to-bufferedimage/窃取了这个答案
Write a simple Transcoder
编写一个简单的转码器
class BufferedImageTranscoder extends ImageTranscoder
{
@Override
public BufferedImage createImage(int w, int h)
{
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
return bi;
}
@Override
public void writeImage(BufferedImage img, TranscoderOutput output)
{
this.img = img;
}
public BufferedImage getBufferedImage()
{
return img;
}
private BufferedImage img = null;
}
Using the Transcoder
使用转码器
public static BufferedImage loadImage(File svgFile, float width, float height)
{
BufferedImageTranscoder imageTranscoder = new BufferedImageTranscoder();
imageTranscoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, width);
imageTranscoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, height);
TranscoderInput input = new TranscoderInput(svgFile);
imageTranscoder.transcode(input, null);
return imageTranscoder.getBufferedImage();
}
Then, just display the rendered BufferedImage
on your JPanel as if it were a PNG or whatever.
然后,只需BufferedImage
在您的 JPanel 上显示渲染,就好像它是 PNG 或其他任何东西一样。