2016-05-10 9 views
0

私はゲームを作るための学校プロジェクトを持っています。私はメモリゲームを作ろうとしていますが、画像を2度しか使用できません。ランダムにする必要があります。この特定のもののためにランダムを使用する方法を知らないでください。なぜなら、イメージはゲーム全体を通して一定のままでなければならず、ランダムは非常に難しいものです。ランダムボタンを作ることはできますか?

public class Juego extends JFrame implements ActionListener{ 

JButton boton1 = new JButton(); 
JButton boton2 = new JButton(); 
JButton boton3 = new JButton(); 
JButton boton4 = new JButton(); 
JButton boton5 = new JButton(); 
JButton boton6 = new JButton(); 


public Juego(){ 
FlowLayout lay = new FlowLayout(); this.setLayout(lay); this.setDefaultCloseOperation(EXIT_ON_CLOSE); 



this.setSize(1080, 1080); this.setTitle("Memoria"); 




this.setBackground(Color.black); 
try { 
Image img = ImageIO.read(getClass().getResource("/videojuegos/media/card-back.jpg")); 
boton1.setIcon(new ImageIcon(img)); 
boton2.setIcon(new ImageIcon(img)); 
boton3.setIcon(new ImageIcon(img)); 
boton4.setIcon(new ImageIcon(img)); 
boton5.setIcon(new ImageIcon(img)); 
boton6.setIcon(new ImageIcon(img)); 



} catch (IOException ex){} 
boton1.addActionListener(this); this.add(boton1);boton2.addActionListener(this);this.add(boton2);boton3.addActionListener(this);this.add(boton3);boton4.addActionListener(this);this.add(boton4);boton5.addActionListener(this);this.add(boton5);boton6.addActionListener(this)this.add(boton6); 






boton1.setActionCommand("boton1"); 


boton2.setActionCommand("boton2"); 



boton3.setActionCommand("boton3"); 



boton4.setActionCommand("boton4"); 



boton5.setActionCommand("boton5"); 



boton6.setActionCommand("boton6"); 





this.setVisible(true); 

} 

public void actionPerformed(ActionEvent e) { 






if ("boton1".equals(e.getActionCommand())){ 
    try{ 
Image img = ImageIO.read(getClass().getResource("/videojuegos/media/card-back.jpg")); 
Image img1 = ImageIO.read(getClass().getResource("/videojuegos/media/Fool.jpg")); 
Image img2 = ImageIO.read(getClass().getResource("/videojuegos/media/empress")); 
Image img3 = ImageIO.read(getClass().getResource("/videojuegos/media/lovers")); 
boton1.setIcon(new ImageIcon(img1)); 
} catch (IOException ex){} 

}}} 
+0

私たちにあなたの宿題を依頼しないでください。あなたが持っている特定のプログラムの質問は歓迎されますが、あなたの課題はあなたをテストしたり教えたりすることになっています。 – Brody

+0

ボタンやその他の方法をランダム化する方法を知りたいだけです。私はあなたが宿題をすることを望んでいない、ちょうどその質問に答える。 –

+0

希望する機能は何ですか? – 4castle

答えて

1

あなたは、画像のセットを保持して、ランダムな順序で別のArrayListに転送するRandomを使用するArrayListを使用することができます。たとえば:

Random rn = new Random(); 
ArrayList<Image> imageSet = importImages(); 
ArrayList<Image> randomizedSet = randomize(imageSet, rn); 

public static ArrayList<Image> importImages() { 
    ArrayList<Image> images = new ArrayList<>(); 
    // put some code here to add each image to images twice 
    return images; 
} 

public static ArrayList<Image> randomize(ArrayList<Image> imageSet, Random rn) { 
    ArrayList<Image> images = new ArrayList<>(); 
    while (!imageSet.isEmpty()) { 
     images.add(imageSet.remove(rn.nextInt(imageSet.size()))); 
    } 
    return images; 
} 

この例ではrandomize()方法はimageSetからランダムに一つの要素を削除し、imagesに追加し、imageSetまでそう残りの要素がありません。

関連する問題