samedi 19 novembre 2016

Composite pattern - Arraylist of an Arraylist of shapes

I am supposed to draw squares using composite pattern. I have a method like this:

public class CompositeIcon{
private List<Icon> icons = new ArrayList<Icon>();

    public int getIconWidth() {
        for(Icon ic : icons){
            width += ic.getIconWidth();
        }
        return width;
    }
}

I want to set the width of the local variable of CompositeIcon, to be equal the total width of all the icons in my ArrayList. It's just not working. The width that i get is far bigger than the actual width of all the icons in the ArrayList. Note that inside the ArrayList there is another ArrayList of icons:

public class CompositeIconTest{

    CompositeIcon ic = new CompositeIcon();
    CompositeIcon ic2 = new CompositeIcon();
    ic.add(new SquareIcon());
    ic.add(new SquareIcon());
    ic2.add(new SquareIcon());
    ic2.add(ic);
}

Aucun commentaire:

Enregistrer un commentaire