assume an image called "die_no_3.jpg", how does one display this image within an applet if a randomly generated number yields the integer 3?
thanks
Printable View
assume an image called "die_no_3.jpg", how does one display this image within an applet if a randomly generated number yields the integer 3?
thanks
Perhaps store all of the images within an Image[]. Then use the random number as the index.
The random numbers would have to be in the range of the array of course. 0-array.length -1
Each time i refresh firefox i get a new image. :)
Code:import java.io.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class AppletEx extends JApplet{
private Image image;
private String[] filenames;
private File imgdir;
private int dirlength;
private Random r;
private int imageindex;
public void init(){
imgdir = new File("C:\\Pics");
filenames = imgdir.list();
dirlength = filenames.length;
r = new Random();
imageindex = r.nextInt(dirlength);
image = Toolkit.getDefaultToolkit().getImage(imgdir.getPath() + "//" + filenames[imageindex]);
}
public void paint(Graphics gfx){
gfx.drawImage(image,50,50,this);
}
}
Out of topic, but shouldn't it be PIV and not PIIII in your sig? ;)Quote:
Originally Posted by Dilenger4
Sombody beat you to it. :lol:
http://www.vbforums.com/showthread.php?t=334369
Soon i will have a PIIIIIIIIIIIIIIIIIIII. Then i will take over the world!