Dynamically scale SVG image to the frame/window size

坚强是说给别人听的谎言 提交于 2019-12-12 02:46:05

问题


I am using SVGSalamander. My code loads a svg image and sets it as background of a JDesktopPane.

File f = new File("awesome_tiger.svg");
SVGUniverse svgUniverse = new SVGUniverse();
try {
  SVGDiagram diagram = svgUniverse.getDiagram(svgUniverse.loadSVG(f.toURL()));
  try {
    diagram.render(g);
  }
  catch(Exception ex) {System.out.println(ex);}}
catch (Exception ex2) {System.out.println(ex2);}

How can I achieve, that the image fills the window/frame completely and resizes with it?

Seems SVGSalamander has the method isScaleToFit() but how can I use it?

I use for antiAlias this: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); (this is how it is written in the SVGIcon and SVGPanel classes)

Edit: solved it

AffineTransform at = new AffineTransform();
at.setToScale(jdpPane.getWidth()/diagram.getWidth(), jdpPane.getWidth()/diagram.getWidth());
g.transform(at);
diagram.render(g);

scales it proportionally


回答1:


We can use an AffineTransform and the setToScale method

File f = new File("awesome_tiger.svg");
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
SVGUniverse svgUniverse = new SVGUniverse();
try {
  SVGDiagram diagram = svgUniverse.getDiagram(svgUniverse.loadSVG(f.toURI().toURL()));
  try {
    AffineTransform at = new AffineTransform();
    at.setToScale(jdpPane.getWidth()/diagram.getWidth(), jdpPane.getWidth()/diagram.getWidth());
    g.transform(at);
    diagram.render(g);
  }
  catch(Exception e2) {System.out.println(e2);}}
catch (Exception ex) {System.out.println(ex);}

this is the code with which I am overriding the paint() method



来源:https://stackoverflow.com/questions/17170430/dynamically-scale-svg-image-to-the-frame-window-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!