
class PixelSpriteList {
  private ArrayList sprites;

  public PixelSpriteList() {
    sprites = new ArrayList();
  }

  public void add( PixelSprite sprite, int layer ) {
    PixelSpriteEntry newEntry = new PixelSpriteEntry( sprite, layer );

    for ( int i=0; i<sprites.size(); i++ )
      if ( layer <= ((PixelSpriteEntry) sprites.get( i )).getLayer() ) {
        sprites.insert( i, newEntry );
        return;
      }

    // add last
    sprites.add( newEntry );
  }

  public boolean remove( PixelSprite sprite ) {
    for ( int i=0; i<sprites.size(); i++ )
      if ( ((PixelSpriteEntry) sprites.get( i )).getSprite() == sprite ) {
        sprites.remove( i );
        return true;
      }

    return false;
  }

  public void render( PixelPanel panel, double parentX, double parentY ) {
    for ( int i=0; i<sprites.size(); i++ )
      ((PixelSpriteEntry) sprites.get( i )).getSprite().render( panel, parentX, parentY );
  }
}

class PixelSpriteEntry {
  private PixelSprite sprite;
  private int layer;

  public PixelSpriteEntry( PixelSprite sprite, int layer ) {
    this.sprite = sprite;
    this.layer = layer;
  }

  public PixelSprite getSprite() {
    return sprite;
  }

  public int getLayer() {
    return layer;
  }
}
