public static void randomizeImage() { File dir = new File("images"); File[] files = dir.listFiles(); int rnd = (int)(Math.random() * files.length) + 1; try { myPicture = ImageIO.read(new File(files[rnd])); } catch (IOException e) { e.printStackTrace(); System.out.println("corgi" + rnd + ".jpg"); return; } addImage(myPicture); frame.validate(); frame.repaint(); }