首先看在SVGElementsManager中的两个函数
public Rectangle2D getSensitiveBounds(Element shape) {
Rectangle2D bounds = null;
if (shape != null) {
BridgeContext ctxt = handle.getCanvas().getBridgeContext();
if (ctxt != null) {
GraphicsNode gnode = null;
try {
gnode = ctxt.getGraphicsNode(shape);
} catch (Exception e) {
}
;
if (gnode != null) {
try {
AffineTransform transform = gnode.getTransform();
bounds = gnode.getSensitiveBounds();
if (transform != null) {
bounds = transform.createTransformedShape(bounds).getBounds2D();
}
} catch (Exception e) {
}
}
}
}
return bounds;
}
public Rectangle2D getSensitiveBoundsWithoutTF(Element shape) {
Rectangle2D bounds = null;
if (shape != null) {
BridgeContext ctxt = handle.getCanvas().getBridgeContext();
if (ctxt != null) {
GraphicsNode gnode = null;
try {
gnode = ctxt.getGraphicsNode(shape);
} catch (Exception e) {
}
;
if (gnode != null) {
try {
bounds = gnode.getSensitiveBounds();
} catch (Exception e) {
}
}
}
}
return bounds;
}
简单说就是getSensitiveBounds函数是将仿射变换也叠加到坐标产生的边界。
看下面的矩形元素
<rect x="50" width="152" height="79" y="100" style="fill:none;stroke:#000000;stroke-width:1.0" />
用这两个函数得到的都是java.awt.geom.Rectangle2D$Float[x=50,y=100,w=152.0,h=79]
当矩形元素为下面
<rect x="50" width="152" height="79" y="100" style="fill:none;stroke:#000000;stroke-width:1.0" transform="rotate(41.47143 126.0 139.5)"/>
用getSensitiveBounds函数得到的是
[x=42.189758556672416,y=58.86669987156454,w=167.62048288665517,h=161.2666002568709]
用getSensitiveBoundsWithoutTF函数得到的是
java.awt.geom.Rectangle2D$Float[x=50,y=100,w=152.0,h=79]
如下图所示:
黑色边线代表的是<rect x="50" width="152" height="79" y="100" style="fill:none;stroke:#000000;stroke-width:1.0" transform="rotate(41.47143 126.0 139.5)"/>这个矩形元素,蓝色边线代表是getSensitiveBoundsWithoutTF返回的边界,紫色边线代表的是getSensitiveBounds返回的边界。
下面在讲绘图中如何得到控制点的边界;
先以单个元素为例。使用getSensitiveBoundsWithoutTF返回一个矩形边界area。假设有八个控制点,则根据area可以得到蓝色虚线边界的八个控制点,而我们进行操作时,实际要得到的是如下在黑色矩形上的控制点。这时就要对每个方向对应的控制点再做仿射变换:
控制点的计算逻辑如下,point为该方向关联的点。当元素的transform属性中有matrix时,要用getSensitiveBounds取边界,然后控制点方向才不会乱。