Random Image on Refresh (FROM Folder not set number)

Hi,

I want to know how I can write a Java program for Random Image Rotation. Now I have looked online, spent time researching, however I cannot find exactly what I need.

I need a Random Image Script that will Display an image on my webpage (which when clicked on, links to the image). The image will change on Refresh. I want the script to be able to pull the random images from a folder "Images" and to randomly pick an image in the folder and display it. The images can be named whatever they want to be, and there is no limit/restrictions on numbers. all are .jpg though.

Anyone able to help me out? Since every bit of Java Random image rotation tutorials/scripts I find online are all where you have to either A: List all your images in the file, or B: Name every image the same way.

Thanks!

[828 byte] By [QuinnMala] at [2007-11-27 9:47:26]
# 1

I assume that you want to display images from the server (also that you're talking about java, not javascript, which isn't the same thing). Here's a link I found that looks like it could work, although it uses some cgi for the server interaction part: http://www.webdeveloper.com/java/java_jj_read_write.html I don't know if it's possible to read all the files in a directory with cgi or not.

hunter9000a at 2007-7-12 23:59:53 > top of Java-index,Java Essentials,Java Programming...
# 2

right, I do mean Java, not JavaScript.

and yes, all of the images will be in a folder called "images" on the server.

The issue I have though is I would know how to do this task in PHP or ASP, however neither is available on the server. Nor is Pearl/CGI or anything because I can't add anything new to the server.

QuinnMala at 2007-7-12 23:59:53 > top of Java-index,Java Essentials,Java Programming...
# 3
I'm just throwing a guess out here, but couldn't you make a properties file setup like:1=imagewhatever.jpg2=differentimage.gifetc...and then just use ResourceBundle.getKeys() and like..randomize on the enumartion returned?
GAndersona at 2007-7-12 23:59:53 > top of Java-index,Java Essentials,Java Programming...
# 4

> I'm just throwing a guess out here, but couldn't you

> make a properties file setup like:

> 1=imagewhatever.jpg

> 2=differentimage.gif

> etc...

>

> and then just use ResourceBundle.getKeys() and

> like..randomize on the enumartion returned?

That's the usual way I think, but he's referring to dynamically loading the contents of the folder without having a listing.

So far the answer I've found is that without some server-side process to either return individual images or return you a listing, you'll need to make a static listing of the directory contents available.

hunter9000a at 2007-7-12 23:59:53 > top of Java-index,Java Essentials,Java Programming...
# 5

ah...I thought he meant having the images named like: 1.jpg, 2.jpg, 3.jpg (I've done that with PHP before...does get a little annoying having to name the images like that)

Umm...how about like...get a directory listing (I believe File.list()) and with that string array, pick a random number based on it's length then use that?

File dir = new File("./images");

String[] images = dir.list();

// Pick a random number between 0 and images.length and use that for the index

GAndersona at 2007-7-12 23:59:53 > top of Java-index,Java Essentials,Java Programming...