listview don't load images

edited March 2016 in Library Questions

Hi, the below code don't load images in listview. Why? Thanks in advice

import controlP5.*;
import java.util.*;

ControlP5 cp5;

void setup() {
  size(1024, 768, P3D);
  cp5 = new ControlP5(this);

  MenuListDetail d = new MenuListDetail(cp5, "detail", 200, 600);
  d.setPosition(400, 40);
  int n_files = 4;  

  for (int j=0; j<n_files; j++) {
    PImage det;
    try {
      det = loadImage("unicorn.png");
    } catch (Exception e) {
      println("Error on load image detail");
      det = createImage(200, 200, RGB);      
    }    
    d.addItem(makeImg("test", det));    
  }  
}

Map<String, Object> makeImg(String theName, PImage theImg) {
  Map g = new HashMap<String, Object>();
  g.put("name", theName);
  g.put("image", theImg);  
  return g;
}

void detail(int i) {
  println("Some menu event from item with index " + i);
}

public void controlEvent(ControlEvent theEvent) {
  if (theEvent.isFrom("detail")) {
    Map g = ((MenuListDetail) theEvent.getController()).getItem(int(theEvent.getValue()));
    println("Menu event from item: " + g);
  }
}

void draw() {
  background(40);    
}

class MenuListDetail extends Controller<MenuListDetail> {
  float pos, npos;
  int itemHeight = 200;
  int scrollerLength = 40;
  List< Map<String, Object>> items = new ArrayList< Map<String, Object>>();
  PGraphics menu;
  boolean updateMenu;

  MenuListDetail(ControlP5 c, String theName, int theWidth, int theHeight) {
    super(c, theName, 0, 0, theWidth, theHeight);
    c.register( this );
    menu = createGraphics(getWidth(), getHeight());

    setView(new ControllerView<MenuListDetail>() {

      public void display(PGraphics pg, MenuListDetail t) {
        if (updateMenu) {
          updateMenu();
        }
        if (inside()) {
          menu.beginDraw();
          int len = -(itemHeight * items.size()) + getHeight();
          int ty = int(map(pos, len, 0, getHeight() - scrollerLength - 2, 2));          
          menu.fill(255);
          menu.rect(getWidth()-4, ty, 4, scrollerLength);          
          menu.endDraw();
        }
        pg.image(menu, 0, 0);
      }
    }
    );    
    updateMenu();    
  }

  void updateMenu() {
    int len = -(itemHeight * items.size()) + getHeight();
    npos = constrain(npos, len, 0);
    pos += (npos - pos) * 0.1;
    menu.beginDraw();
    menu.noStroke();
    menu.background(255, 64);
    menu.textFont(cp5.getFont().getFont());
    menu.pushMatrix();
    menu.translate(0, int(pos));
    menu.pushMatrix();

    int i0 = PApplet.max(0, int(map(-pos, 0, itemHeight * items.size(), 0, items.size())));
    int range = ceil((float(getHeight())/float(itemHeight))+1);
    int i1 = PApplet.min(items.size(), i0 + range);

    menu.translate(0, i0*itemHeight);

    for (int i=i0; i<i1; i++) {
      Map g = items.get(i);
      menu.fill(255, 100);
      menu.rect(0, 0, getWidth(), itemHeight-1);      
      menu.fill(255);      
      menu.text(g.get("name").toString(), 10, 20);

      PImage test = (PImage)g.get("image");

      test.resize(200, 200);
      menu.image(test, 400, 40, 200, 200);        //x, y, width, height

      menu.translate(0, itemHeight);
    }
    menu.popMatrix();
    menu.popMatrix();
    menu.endDraw();
    updateMenu = abs(npos-pos)>0.01 ? true:false;
  }

  public void onClick() {
    if (getPointer().x()>getWidth()-10) {
      npos= -map(getPointer().y(), 0, getHeight(), 0, items.size()*itemHeight);      
      updateMenu = true;
    } else {
      int len = itemHeight * items.size();
      println("len: " + len);
      int index = int(map(getPointer().y() - pos, 0, len, 0, items.size()));      
      setValue(index);      
    }
  }

  public void onMove() {
  }

  public void onDrag() {
    npos += getPointer().dy() * 2;
    updateMenu = true;
  } 

  public void onScroll(int n) {
    npos += (n * 4);
    updateMenu = true;
  }

  void addItem(Map<String, Object> m) {
    items.add(m);
    updateMenu = true;
  }

  Map<String,Object> getItem(int theIndex) {
    return items.get(theIndex);
  }

}
Sign In or Register to comment.