Dynamically scale SVG image to the frame/window si

2019-08-28 14:19发布

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条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-28 15:13

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

查看更多
登录 后发表回答