Dear Batik-Experts,
am having trouble displaying a whole svg file.
This file (3000*2000) is bigger than the gui with a fixed size(500,
300), so after I set the svg file to the canvas, I tried to scale the
file to fit in the gui, but somehow it doesn't work...
Could anyone please take a look at the sample codes below and tell me
why I failed or how I can do scale the file to fit the gui please?
best regards,
Cui
------------------------------------------------------------------------------------------------
package resizeText;
import java.awt.Dimension;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.util.SVGConstants;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Resizer extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private JSVGCanvas canvas;
private Document svgDoc;
private static final int ellipseX = 1500;
private static final int ellipseY = 1000;
private static final int canvasFixedWidth = 500;
private static final int canvasFixedHeight = 300;
public Resizer(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.canvas = new JSVGCanvas();
this.canvas.setDocumentState (JSVGCanvas.ALWAYS_DYNAMIC);
DOMImplementation dom =
SVGDOMImplementation.getDOMImplementation ();
svgDoc =
dom.createDocument(SVGConstants.SVG_NAMESPACE_URI,
SVGConstants.SVG_SVG_TAG, null);
this.initBall();
this.canvas.setDocument(svgDoc);
this.getContentPane().add(this.canvas, "Center");
double scale = Math.min(canvasFixedWidth/(double)(ellipseX*2),
canvasFixedHeight/(double)(ellipseY*2));
AffineTransform at = canvas.getRenderingTransform();
at.scale(scale, scale);
canvas.setRenderingTransform(at);
this.canvas.setMySize(new Dimension(canvasFixedWidth,
canvasFixedHeight));
this.pack();
this.setVisible(true);
}
private void initBall(){
Element ballE =
svgDoc.createElementNS(SVGConstants.SVG_NAMESPACE_URI,
SVGConstants.SVG_ELLIPSE_TAG);
svgDoc.getDocumentElement().appendChild(ballE);
ballE.setAttributeNS(null, SVGConstants.SVG_CX_ATTRIBUTE,
""+ellipseX);
ballE.setAttributeNS(null, SVGConstants.SVG_CY_ATTRIBUTE,
""+ellipseY);
ballE.setAttributeNS(null, SVGConstants.SVG_RX_ATTRIBUTE,
""+ellipseX);
ballE.setAttributeNS(null, SVGConstants.SVG_RY_ATTRIBUTE,
""+ellipseY);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Resizer();
}
});
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]