• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using arrays to randomly generate images? (applet)

 
Khair Bahir
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to randomly generate a few images from a directory of images, but I have no idea how to do it.

This is what I have without the random image generation component:



I've heard that using a random number generator and/or array can display randomly selected images. Can anyone help me understand how that would work? Thanks.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replace your twelve Image references with one Image[]:
You can use java.util.Random or Math.random() for generating 4 random indexes for the array.
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob wrote:You can use java.util.Random or Math.random() for generating 4 random indexes for the array.

Or Collections#shuffle()
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In combination with Arrays.asList then, of course. Or the Image[] could be replaced by a List<Image>.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic