jeudi 24 novembre 2016

Randomize ArrayList and using it in another class [duplicate]

This question already has an answer here:

I have a class Reel where i populate ArrayList with pictures, the array needs to be randomized in the Reel class and used in my Gui class, how do i do this?

private ArrayList<Symbol>  images = new ArrayList<>();
    public Reel(){

        Icon seven = new ImageIcon((getClass().getResource("seven.png")));
        Icon bell = new ImageIcon((getClass().getResource("bell.png")));
        Icon melon = new ImageIcon((getClass().getResource("melon.png")));
        Icon plum = new ImageIcon((getClass().getResource("plum.png")));
        Icon lemon = new ImageIcon((getClass().getResource("lemon.png")));
        Icon cherry = new ImageIcon((getClass().getResource("cherry.png")));

        images.add(new Symbol(7,seven));
        images.add(new Symbol(6,bell));
        images.add(new Symbol(5,melon));
        images.add(new Symbol(4,plum));
        images.add(new Symbol(3,lemon));
        images.add(new Symbol(2,cherry));
    }

    public Symbol spin(){
        Random randomGenerator = new Random();
        int index = randomGenerator.nextInt(images.size());
        Symbol image = images.get(index);
        return image;
    }

    public ArrayList<Symbol> getList(){
        return images;
    }
}

    public class Gui extends JFrame {

    Reel reels = new Reel();

    //this is how i get the array in to my Gui Class
    ArrayList<Symbol> list = reels.getList();
}




Aucun commentaire:

Enregistrer un commentaire