01: /*
02: * ViewBoxPosition.java
03: *
04: * Created on 31 January 2007, 11:13
05: *
06: * To change this template, choose Tools | Template Manager
07: * and open the template in the editor.
08: */
09:
10: package com.xoetrope.svg;
11:
12: import com.kitfox.svg.SVGRoot;
13: import com.kitfox.svg.animation.AnimationElement;
14: import com.xoetrope.carousel.build.BuildProperties;
15: import com.xoetrope.svg.XSvgImageMap;
16: import java.awt.geom.Point2D;
17:
18: /**
19: *
20: *
21: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
22: * the GNU Public License (GPL), please see license.txt for more details. If
23: * you make commercial use of this software you must purchase a commercial
24: * license from Xoetrope.</p>
25: * <p> $Revision: 1.2 $</p>
26: */
27: public class XViewBoxModifier {
28: protected XSvgImageMap imageMap;
29: protected XPointSystem pointSystem;
30:
31: /**
32: * Creates a new instance of ViewBoxPosition
33: * @param imageMap the <CODE>XSvgImageMap</CODE> instance used by this class.
34: */
35: public XViewBoxModifier(XSvgImageMap imageMap) {
36: this .imageMap = imageMap;
37: pointSystem = new XPointSystem(imageMap);
38: }
39:
40: /**
41: * Sets the viewboxs centre position on the map.
42: * @param x <CODE>double</CODE> specifying the x coordinate of the view box's centre position.
43: * @param y <CODE>double</CODE> specifying the y coordinate of the view box's centre position.
44: */
45: public void setPosition(double x, double y) {
46: SVGRoot root = imageMap.getSvgDiagram().getRoot();
47: double[] attr = root.getPresAbsolute("viewBox").getDoubleList();
48:
49: Point2D centre = pointSystem.glassPaneToSvg(x, y);
50:
51: double newX = (centre.getX() - ((attr[2] + attr[0]) / 2.0));
52: double newY = (centre.getY() - ((attr[3] + attr[1]) / 2.0));
53:
54: String viewBox = newX + " " + newY + " " + attr[2] + " "
55: + attr[3];
56:
57: try {
58: root.setAttribute("viewBox", AnimationElement.AT_XML,
59: viewBox);
60: root.build(); // rebuild root of svg image
61: imageMap.resetBuffer();
62: imageMap.repaint();
63: } catch (Exception ex) {
64: if (BuildProperties.DEBUG)
65: ex.printStackTrace();
66: }
67: }
68:
69: /**
70: * Sets the size of the view box.
71: * @param w <CODE>double</CODE> specifying the new width of the viewBox.
72: * @param h <CODE>double</CODE> specifying the new width of the viewBox.
73: */
74: public void setSize(double w, double h) {
75: SVGRoot root = imageMap.getSvgDiagram().getRoot();
76: double[] attr = root.getPresAbsolute("viewBox").getDoubleList();
77:
78: String viewBox = attr[0] + " " + attr[1] + " " + w + " " + h;
79:
80: try {
81: root.setAttribute("viewBox", AnimationElement.AT_XML,
82: viewBox);
83: root.build(); // rebuild root of svg image
84: imageMap.resetBuffer();
85: imageMap.repaint();
86: } catch (Exception ex) {
87: if (BuildProperties.DEBUG)
88: ex.printStackTrace();
89: }
90: }
91: }
|