!7

sclass LockMatrix > DynModule {
  transient int n;
  transient BitSet data;
  transient JComponent canvas;
  
  visualize {
    canvas = dynamicallyPaintedComponent(voidfunc(Graphics2D g, int w, int h) {
      int rows = ifloor(sqrt(n));
      if (rows == 0) fillRect(g, 0, 0, w, h, Color.white);
      int cols = (n+rows-1)/rows;
      L<Rect> tiles = tileRects(w, h, cols, rows);
      _print("n=" + n + ", rows=" + rows + ", first tile=" + first(tiles));
      for i over tiles:
        fillRect(g, tiles.get(i), main.contains(data, i) ? Color.red : Color.black);
    });
    awtEvery(canvas, 1000, r updateMe);
    ret canvas;
  }
  
  void update {
    new BitSet bs;
    L l = dm_listModules();
    for i over l:
      if (isLocked((Lock) getOpt(l.get(i), 'lock))) {
        print("Locked: " + l.get(i));
        bs.set(i);
      }
    setFields(n := l(l), data := bs);
    repaint(canvas);
  }
}