mardi 10 mai 2016

Can i make a random button?

I have a school project to make a game, I´m trying to make a memory game, but can only use the image twice, and need it to be random, here is what I've got so far. Don´t know how to use random for this specific thing, since the images while have to remain constant throughout the game and random makes that very difficult Sorry for the format, first post.

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){} 

}}}




Aucun commentaire:

Enregistrer un commentaire