首先看在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取邊界,然後控制點方向纔不會亂。