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: ");
    }
  }
  public WebcamCaptureAndFadePanel(String saveDir, String layout) {

    System.out.println("Using " + saveDir + " as directory for the images.");
    saveDirectory = saveDir;

    getImages();
    images_used = new ArrayList<Integer>();
    images_lastadded = new ArrayList<Integer>();
    images_nevershown = new ArrayList<Integer>();

    Vector devices = (Vector) CaptureDeviceManager.getDeviceList(null).clone();
    Enumeration enumeration = devices.elements();
    System.out.println("- Available cameras -");
    ArrayList<String> names = new ArrayList<String>();
    while (enumeration.hasMoreElements()) {
      CaptureDeviceInfo cdi = (CaptureDeviceInfo) enumeration.nextElement();
      String name = cdi.getName();
      if (name.startsWith("vfw:")) {
        names.add(name);
        System.out.println(name);
      }
    }

    // String str1 = "vfw:Logitech USB Video Camera:0";
    // String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
    if (names.size() == 0) {
      JOptionPane.showMessageDialog(
          null,
          "Ingen kamera funnet. " + "Du må koble til et kamera for å kjøre programmet.",
          "Feil",
          JOptionPane.ERROR_MESSAGE);
      System.exit(0);
    } else if (names.size() > 1) {

      JOptionPane.showMessageDialog(
          null,
          "Fant mer enn 1 kamera. " + "Velger da:\n" + names.get(0),
          "Advarsel",
          JOptionPane.WARNING_MESSAGE);
    }

    String str2 = names.get(0);
    di = CaptureDeviceManager.getDevice(str2);
    ml = di.getLocator();

    try {
      player = Manager.createRealizedPlayer(ml);
      formatControl = (FormatControl) player.getControl("javax.media.control.FormatControl");

      /*
      Format[] formats = formatControl.getSupportedFormats();
      for (int i=0; i<formats.length; i++)
      	System.out.println(formats[i].toString());
      */

      player.start();
    } catch (javax.media.NoPlayerException e) {
      JOptionPane.showMessageDialog(
          null,
          "Klarer ikke å starte" + " programmet pga. feil med kamera. Sjekk at det er koblet til.",
          "IOException",
          JOptionPane.ERROR_MESSAGE);
      System.exit(0);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(0);
    }

    /*
     * Layout
     *
     * Add
     * - comp
     * - imagepanels
     */

    if (layout.equals("1024v2")) {
      layout1024v2();
    } else if (layout.equals("1280")) {
      layout1280();
    } else {
      layout1024();
    }

    // Capture Window
    if (captureWindow) {
      cw = new JFrame("Capture from webcam");
      cw.setAlwaysOnTop(true);
      cw.setSize(sizeCaptureWindow_x, sizeCaptureWindow_y);
      cw.addKeyListener(new captureWindowKeyListner());
      cw.setUndecorated(true);

      // Add webcam
      if ((comp = player.getVisualComponent()) != null) {
        cw.add(comp);
      }

      // Add panel to window and set location of window
      cw.setLocation(cwLocation_x, cwLocation_y);
    }

    // Text window
    cwText = new rotatedText("");

    /*
     * Timer for update
     */
    Timer thread = new Timer();
    thread.schedule(new frameUpdateTask(), 0, (1000 / fps));
  }