!7

static double zoom = 0.5;

sS imageID = #1007256;
sS rects = [[ [Rect(h=415, w=392, x=104, y=168), Rect(h=420, w=393, x=552, y=168), Rect(h=414, w=392, x=1008, y=168), Rect(h=416, w=392, x=1448, y=168), Rect(h=416, w=400, x=112, y=640), Rect(h=416, w=392, x=560, y=640), Rect(h=424, w=400, x=1000, y=632), Rect(h=421, w=393, x=1447, y=632), Rect(h=419, w=400, x=112, y=1112), Rect(h=416, w=392, x=560, y=1112), Rect(h=416, w=392, x=1000, y=1112), Rect(h=424, w=394, x=1446, y=1104), /* the gray one: Rect(h=422, w=393, x=1447, y=1576),*/ Rect(h=420, w=400, x=96, y=1584), Rect(h=420, w=400, x=552, y=1583), Rect(h=416, w=392, x=1000, y=1584)] ]];

static Map<Int, S> names = littreemap(
  1, "happy",
  2, "sad",
  3, "extremely happy",
  4, "happy with tongue",
  5, "whaaat",
  6, "crying",
  7, "shocked",
  8, "worried",
  9, "winking",
  10, "closed eyes",
  11, "uuuhm",
  12, "super-lol",
  13, "oooh",
  14, "angry",
  15, "disappointed"
);

static ImageSurface is;
static JCheckBox cbAnimate;
static int idx;

p-subst {
  BufferedImage bi = loadImage2(imageID);
  L<Rect> theRects = cast unstructure(rects);
  final Pt size = ceilScalePt(rectsMaxSize(theRects), zoom);
  repeat with sleep 4 {
    bool first = is == null;
    if (first || isChecked(cbAnimate)) {
      Rect r = random(theRects);
      idx = theRects.indexOf(r);
      is = showZoomedImage_centered(is, clipBufferedImage(bi, r), zoom);
      setFrameTitle(is, "Emotion " + (idx + 1) + "/" + l(theRects) + " - " + or2(names.get(idx+1), "?"));
      if (first) awt {
        moveToTopRightCorner(frameInnerSize(size, getFrame(is)));
        addToWindow(is, withMargin(cbAnimate = jCheckBox("Cycle", true)));
        hideConsole();
      }
    }
  }
}