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){}
}}}
私たちにあなたの宿題を依頼しないでください。あなたが持っている特定のプログラムの質問は歓迎されますが、あなたの課題はあなたをテストしたり教えたりすることになっています。 – Brody
ボタンやその他の方法をランダム化する方法を知りたいだけです。私はあなたが宿題をすることを望んでいない、ちょうどその質問に答える。 –
希望する機能は何ですか? – 4castle