문제

I'm creating a Graphic Editor and to make shapes hierachies I'm using Composite Pattern, the question is how to resize the children of a Composite structure?

I have the following code where tempGroup is the composite object that groups a selection of objects to resize, the "resize tool" resizes the shape from the corners of the bounding box.

            Point p = e.getPoint();
            BoundBox r = tempGroup.getBoundBox();
            int dx = p.x - r.x;
            int dy = p.y - r.y;
            int width = 0;
            int height = 0;

            if (controlPoint.equals("NW")) {
                width = r.width - dx;
                height = r.height - dy;

                tempGroup.setRectBoundBox(new Rectangle(r.x + dx, r.y + dy, width, height));
            } else if (controlPoint.equals("NE")) {
                width = dx;
                height = r.height - dy;
                tempGroup.setRectBoundBox(new Rectangle(r.x, r.y + dy, width, height));
            } else if (controlPoint.equals("SW")) {
                width = r.width - dx;
                height = dy;
                tempGroup.setRectBoundBox(new Rectangle(r.x + dx, r.y, width, height));
            } else if (controlPoint.equals("SE")) {
                width = dx;
                height = dy;
                tempGroup.setRectBoundBox(new Rectangle(r.x, r.y, width, height));
            }

setRectBoundBox method in the superclass uses Rectangle.setRect to set the new BoundingBox and in the Composite class I've tried to adjust proportionally the way the children are resized but it doesn't seems to work, any idea to implement correctly the setRectBoundBox in the Composite class?.

This is the super class method:

public void setRectBoundBox(Rectangle r){
    bbox.setRect(r);
}

And this is what I've tried so far to implement children resize:

public void setRectBoundBox(Rectangle r) {

    Rectangle prev = (Rectangle) bbox.clone();

    super.setRectBoundBox(r);

    Rectangle aft = (Rectangle) bbox.clone();

    for (ShapeIf sh : children) {
        Rectangle chBBox = sh.getBoundBox();
        Rectangle t = new Rectangle(chBBox.x * aft.x / prev.x, chBBox.y
                * aft.y / prev.y, chBBox.width * aft.width / prev.width,
                chBBox.height * aft.height / prev.height);

        sh.setRectBoundBox(t);
    }

}
도움이 되었습니까?

해결책 2

It looks like the formula itself is having problem.

You calculate the X coordinate by oldChild.x * newParent.x/oldParent.x which doesn't seems right.

instead, the formula should be

(oldChild.x - oldParent.x)/(newChild.x - newParent.x) 
     = oldParent.width/newParent.width

(old and new displacement of child's x from parent's x, should be proportional to the change of width)

which means

newChild.x = newParent.width* (oldChild.x - oldParent.x) / oldParent.width
             + newParent.x

다른 팁

I am considering that you want the design to place your logic, so in that case what i thing is you can Try Using decorators and Composites Together, They will have common Parent class i.e Decorator will support the Component interface for adding, removing, re sizing. And for the Shapes You can use Composite.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top