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());
  }
    public WebcamCaptureAndFadeImagePanel(int grid_x, int grid_y, int size_x, int size_y) {

      // Set to object
      this.grid_x = grid_x;
      this.grid_y = grid_y;

      this.size_x = size_x;
      this.size_y = size_y;

      // init tables
      imagenum_now = new int[this.grid_x][this.grid_y];
      imagenum_next = new int[this.grid_x][this.grid_y];
      imagenum_now2 = new Image[this.grid_x][this.grid_y];
      imagenum_next2 = new Image[this.grid_x][this.grid_y];
      fade = new float[this.grid_x][this.grid_y];
      wait = new int[this.grid_x][this.grid_y];
      redborder = new int[this.grid_x][this.grid_y];

      for (int i = 0; i < imagenum_now.length; i++) {
        for (int j = 0; j < imagenum_now[i].length; j++) {
          imagenum_now[i][j] = getRandomImageNum();
          images_used.add((Integer) imagenum_now[i][j]);
          imagenum_next[i][j] = getRandomImageNum();
          images_used.add((Integer) imagenum_next[i][j]);

          fade[i][j] = 0.1f * i * j;
          redborder[i][j] = 0;

          wait[i][j] = (int) (number_of_frames_showimage * Math.random());

          imagenum_now2[i][j] = getImage(imagenum_now[i][j]);
          imagenum_next2[i][j] = getImage(imagenum_next[i][j]);
        }
      }

      setPreferredSize(new Dimension(this.grid_x * size_x, this.grid_y * size_y));
    }