问题
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