aspose file tools*
The moose likes Beginning Java and the fly likes Using arrays to randomly generate images? (applet) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using arrays to randomly generate images? (applet)" Watch "Using arrays to randomly generate images? (applet)" New topic
Author

Using arrays to randomly generate images? (applet)

Khair Bahir
Greenhorn

Joined: Aug 01, 2011
Posts: 14
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

Joined: Oct 27, 2005
Posts: 19779
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10532
    
    9

Rob wrote:You can use java.util.Random or Math.random() for generating 4 random indexes for the array.

Or Collections#shuffle()


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19779
    
  20

In combination with Arrays.asList then, of course. Or the Image[] could be replaced by a List<Image>.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using arrays to randomly generate images? (applet)