2015-02-09 27 views
0

为什么第一个和第三个矩形不出现在下面的示例中?无法在Piccolo2D中拉伸零矩形?

看起来像长方形是一旦它有零大小被打破。

package tests.piccolo; 

import org.piccolo2d.extras.PFrame; 
import org.piccolo2d.nodes.PPath; 

public class Try_EmptyRectangle { 

    public static void main(String[] args) { 

     new PFrame() { 

      @Override 
      public void initialize() { 

       PPath rect1 = PPath.createRectangle(0, 0, 0, 0); 
       PPath rect2 = PPath.createRectangle(0, 100, 1, 1); 
       PPath rect3 = PPath.createRectangle(0, 200, 1, 1); 

       getCanvas().getLayer().addChild(rect1); 
       getCanvas().getLayer().addChild(rect2); 


       rect1.setWidth(50); 
       rect1.setHeight(50); 

       rect2.setWidth(50); 
       rect2.setHeight(50); 

       rect3.setWidth(0); 
       rect3.setHeight(0); 
       rect3.setWidth(50); 
       rect3.setHeight(50); 


      } 



     }; 

    } 

} 

回答

1

这看起来像一个错误。 PPath内部包装GeneralPathPPath.createRectangle(0, 0, 0, 0)GeneralPath初始化为零大小的矩形形状。然后更改PPath宽度/高度触发边界更改。 PPath覆盖internalUpdateBounds()以缩放路径以适合指定的边界。似乎有一个问题零大小的路径:

protected void internalUpdateBounds(final double x, final double y, final double width, final double height) { 
    final Rectangle2D pathBounds = path.getBounds2D(); 
    ... 
    final double scaleX; 
    if (adjustedWidth == 0 || pathBounds.getWidth() == 0) { 
      scaleX = 1; 
    } 
    ... 
    final double scaleY; 
    if (adjustedHeight == 0 || pathBounds.getHeight() == 0) { 
     scaleY = 1; 
    } 
    ... 
    TEMP_TRANSFORM.scale(scaleX, scaleY); 
    ... 
    path.transform(TEMP_TRANSFORM); 
} 

的scaleX的scaleY总是1.所以路径实际上从不进行缩放,并保持零大小。