开发者

Why isnt Rectangle2D.createUnion() working for me?

im trying to implement a getBounds() method, but i cant get union to work; i must not understand how union works. my code is the following:

public Rectangle2D getBounds2D() {
    Rectangle2D rec= new Rectangle2D开发者_运维知识库.Double();
    Rectangle2D temp;
    for(int i=0; i<shapes.size(); i++){
        temp = new Rectangle2D.Double(shapes.get(i).getBounds2D().getX(),shapes.get(i).getBounds2D().getY(),shapes.get(i).getBounds2D().getWidth(), shapes.get(i).getBounds2D().getHeight());
        rec.createUnion(temp);

    }
    return rec;
}

the shapes variable is an arraylist of Shapes.

i use temp to create a rectangle using the bounds from each shape in the arraylist

Ive used getbounds().getWidth/Height on temp to see if it were returning wierd numbers but the numbers look fine. When i call rec.getBounds.getWidth/Height i get 0.0 for both. From this, im assuming that im not using union() correctly. does anyone have any insight as to what i can to? thanks!


If you are not sure how to use the method then why is your code so complex to test the method? Why are you looping through and array? How will you verify the results?

Start with something simple. Try to use the method with just two rectangles and with hardcoded values for each Rectangle. Then you can easily verify the results. If it doesn't work then you have a complete program to post.

Something like:

public class Test
{
    public static void main(String args[]) throws Exception
    {
        Rectangle a = new Rectangle(5, 5, 30, 30);
        Rectangle b = new Rectangle(10, 10, 50, 50);
        a = a.union(b);
        System.out.println(a);
        System.out.println(b);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜