File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
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: 19543
    
  16

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: 9990
    
    7

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: 19543
    
  16

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)
 
Similar Threads
weird error when my jsp page uses a javabean for help
ArrayList advice needed
Exception in thread "main"
weird error when using a java bean with my jsp
Help With 5 card stud program