【发布时间】:2019-02-24 15:05:49
【问题描述】:
我正在尝试在我的摇摆应用程序上显示 SVG 图像, 因为我没有 SVG 文件,但我将路径转换为有效的 svg 文档的路径:
private static Document buildSVGDocument(Color svgColor, /*double svgWidth, double svgHeight,*/ String svgPath) {
DOMImplementation svgDocumentImplementation = SVGDOMImplementation.getDOMImplementation();
Document svgDocument = svgDocumentImplementation.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null);
Element svgDocumentElement = svgDocument.getDocumentElement();
//svgDocumentElement.setAttribute("height", String.valueOf(svgHeight));
//svgDocumentElement.setAttribute("width", String.valueOf(svgWidth));
Element svgDocumentPath = svgDocument.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "path");
svgDocumentPath.setAttribute("style", String.format("fill:rgb(%s, %s, %s);", svgColor.getRed(), svgColor.getGreen(), svgColor.getBlue()));
svgDocumentPath.setAttribute("d", svgPath);
svgDocumentElement.appendChild(svgDocumentPath);
return svgDocument;
}
然后我在蜡染画布上显示 SVG 文档:
JSVGCanvas panel = new JSVGCanvas();
panel.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
panel.setDisableInteractions(true);
panel.setDocument(buildSVGDocument(/*etc*/));
container.add(panel, BorderLayout.WEST);
现在我的问题是:如何将 svg 调整为面板大小以保持纵横比?
【问题讨论】:
标签: java swing svg scale batik