public int getRandomImageNum() {
    if (images.size() <= images_used.size()) return -1;
    else {
      // Calculate, based on random, if we try to get from the new images
      boolean getFromNewImages;
      if (Math.random() * 100 < percentage_of_new_images) {
        getFromNewImages = true;
      } else {
        getFromNewImages = false;
      }

      int i;
      int j = 0;
      int tries = 0;
      while (true && tries < randomImageNum_maxTries) {
        // Always try the last added pictures
        if (images_nevershown.size() > 0
            && tries < (int) (randomImageNum_maxTries / 4) // Only use 1/4 of the tries here
        ) {
          i = images_nevershown.get(0);
          tries++;
        } else if (getFromNewImages
            && images_lastadded.size() > 0
            && tries < (int) (randomImageNum_maxTries / 2) // Only use 1/2 of the tries here
        ) {
          j = (int) (images_lastadded.size() * Math.random());
          i = images_lastadded.get(j);
          tries++;
        } else {
          // Random from the rest of the pictures
          i = (int) (Math.random() * images.size());
          tries++;
        }
        if (!images_used.contains((Integer) i)) return i;
      }

      System.out.println("Max tries in randomImageNum");
      return -1;
    }

    // Original:
    // return (int)(Math.random() * images.size());
  }
  @Override
  public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == 67) // C
    {
      if (captureWindow) {
        this.openCaptureWindow();
      } else {
        this.captureImage();
      }
    } else if (e.getKeyCode() == 27) // Escape
    {
      System.out.println("Escape pressed, exiting");
      System.exit(0);
    } else if (e.getKeyCode() == 84) // t
    {
      // Testing purpose
      for (int i = 0; i < images.size(); i++) {
        System.out.println("image " + i + ", used? " + images_used.contains((Integer) i));
      }

    } else if (e.getKeyCode() == 89) // y
    {
      // Testing purpose
      System.out.println("getRandomImageNum() = " + getRandomImageNum());
    } else if (e.getKeyCode() == 73) // i
    {
      // Testing purpose
      System.out.println("LAST ADDED");
      for (int i = 0; i < images_lastadded.size(); i++) {
        System.out.println(
            i
                + " - image "
                + images_lastadded.get(i)
                + ", used? "
                + images_used.contains((Integer) images_lastadded.get(i)));
      }
    } else if (e.getKeyCode() == 85) // u
    {
      // Testing purpose
      for (int i = 0; i < imagepanels.length; i++) {
        for (int j = 0; j < imagepanels[i].imagenum_now.length; j++) {
          for (int j2 = 0; j2 < imagepanels[i].imagenum_now[j].length; j2++) {
            String print1;
            if (imagepanels[i].imagenum_now[j][j2] < 10)
              print1 = "  " + imagepanels[i].imagenum_now[j][j2];
            else if (imagepanels[i].imagenum_now[j][j2] < 100)
              print1 = " " + imagepanels[i].imagenum_now[j][j2];
            else print1 = "" + imagepanels[i].imagenum_now[j][j2];
            String print2;
            if (imagepanels[i].imagenum_next[j][j2] < 10)
              print2 = "  " + imagepanels[i].imagenum_next[j][j2];
            else if (imagepanels[i].imagenum_next[j][j2] < 100)
              print2 = " " + imagepanels[i].imagenum_next[j][j2];
            else print2 = "" + imagepanels[i].imagenum_next[j][j2];

            System.out.println(
                "imagepanels["
                    + i
                    + "]."
                    + "imagenum_now["
                    + j
                    + "]["
                    + j2
                    + "] = "
                    + print1
                    + ", next = "
                    + print2);
          }
        }
      }
    } else {
      displayInfo(e, "KEY TYPED: ");
    }
  }