Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How do you make an array of image icons and then call them?

843789Mar 24 2009 — edited Mar 24 2009
How do you make an array of image icons and then call them, i have searched all over the internet for making an array of icons, but i have
found nothing. Below is my attempt at making an array of icons, but i cant seem to make it work. Basically, i want the image to match the value of the roll of the dice (rollVal)
Any help would be greatly appreciated, some code or link to tuturial, ect.



/** DiceRoller.java
 * 
 * Roll, print, Gui
 *
 */
 
import javax.swing.*;
	
public class DiceRoller extends JFrame
{
		
 	private ImageIcon[] image  ; 
 	public String[] images = { "empty", "dice1.jpg",
			"dice2.jpg", "dice3.jpg", "dice4.jpg",
			"dice5.jpg", "dice6.jpg" }; 
	public Dice die;
	private int rollVal;
	public int rollNum;
	private JLabel j1;
	public DiceRoller(){
		j1= new JLabel("");
	 
	  die =new Dice();
	  int rollVal; 
	  rollVal = die.roll();	
	  image = new  ImageIcon[images.length];
	 
     for(int i = 0; i < images.length; i++){
     
      image[i] = new ImageIcon(images);
}

if (image!=null){
j1.setIcon(image[rollVal]);
}

System.out.println("Roll = "+die.roll());

}


}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 21 2009
Added on Mar 24 2009
6 comments
324 views