Mi estimado, tu pregunta me puso la espina de hacer este juego tan llamativo, no me agrada mucho contestar por completo una duda incluyendo todo el código, pero dado que tu ya tenía algo empezado no creo que esté mal, además puedes tomar de mi código lo que te sirva, saludos.
Código Java:
Ver originalpackage main;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main {
private final static long serialVersionUID = 437634839L;
private static int difficulty = 1;
private static int follow = 0;
private static boolean doSimon = false;
private static boolean exit = false;
private static boolean doAction = false;
private static final ArrayList<Integer> says = new ArrayList<Integer>();
private static final ArrayList<JPanel> buts = new ArrayList<JPanel>();
public static void main
(String[] args
) { board.setSize(600, 200);
insertButtons(board);
board.setVisible(true);
simon();
actions();
doSimon = true;
doAction = true;
b.setEnabled(false);
}
});
exit = true;
}
});
while (!exit) {
if (doSimon) {
waiting(1000);
simonSays();
doSimon = false;
}
}
board.dispose();
}
private static void insertButtons
(final JFrame board
) {
buts.
get(0).
setBackground(Color.
BLUE); buts.get(0).setBounds(0, 0, 100, 100);
buts.get(0).setLayout(null);
buts.
get(1).
setBackground(Color.
GREEN); buts.get(1).setBounds(100, 0, 100, 100);
buts.get(1).setLayout(null);
buts.
get(2).
setBackground(Color.
CYAN); buts.get(2).setBounds(200, 0, 100, 100);
buts.get(2).setLayout(null);
buts.
get(3).
setBackground(Color.
BLACK); buts.get(3).setBounds(300, 0, 100, 100);
buts.get(3).setLayout(null);
buts.
get(4).
setBackground(Color.
ORANGE); buts.get(4).setBounds(400, 0, 100, 100);
buts.get(4).setLayout(null);
buts.
get(5).
setBackground(Color.
RED); buts.get(5).setBounds(500, 0, 100, 100);
buts.get(5).setLayout(null);
board.add(buts.get(0));
board.add(buts.get(1));
board.add(buts.get(2));
board.add(buts.get(3));
board.add(buts.get(4));
board.add(buts.get(5));
bot.setBounds(0, 100, 500, 35);
lab.setBounds(0, 135, 500, 35);
bot.add(b);
bot.add(s);
lab.add(l);
board.add(lab);
board.add(bot);
board.setLayout(null);
board.addWindowListener(exitListener());
}
private static void fine() {
follow = 0;
difficulty++;
simon();
l.setText("BIEN!");
}
private static void wrong() {
follow = 0;
l.setText("Perdiste... nivel máximo alcanzado:" + difficulty);
l.
setForeground(Color.
RED); doAction = false;
difficulty = 1;
simon();
b.setEnabled(true);
}
private static void simon() {
says.clear();
for (int i = 0; i < difficulty; i++) {
says.add(random.nextInt(6));
}
}
private static void simonSays() {
l.setText("Nivel:" + difficulty);
l.
setForeground(Color.
GRAY); doAction = false;
for (int i = 0; i < says.size(); i++) {
doClick(says.get(i));
waiting(200);
}
l.
setForeground(Color.
BLACK); doAction = true;
}
private static void doClick(final int index) {
final Color current
= buts.
get(index
).
getBackground(); buts.
get(index
).
setBackground(Color.
WHITE); waiting(300);
buts.get(index).setBackground(current);
}
private static void actions() {
addMouseListenerToPanel(buts.get(0), 0);
addMouseListenerToPanel(buts.get(1), 1);
addMouseListenerToPanel(buts.get(2), 2);
addMouseListenerToPanel(buts.get(3), 3);
addMouseListenerToPanel(buts.get(4), 4);
addMouseListenerToPanel(buts.get(5), 5);
}
private static void waiting(int time) {
try {
}
}
private static void addMouseListenerToPanel
(final JPanel jp,
final int index
) {
if (doAction) {
((JPanel) e.
getSource()).
setBackground(color
[0]); }
}
if (doAction) {
color
[0] = ((JPanel) e.
getSource()).
getBackground(); if (says.get(follow++).intValue() == index) {
if (says.size() <= follow) {
fine();
doSimon = true;
}
} else {
wrong();
}
}
}
}
}
}
};
jp.addMouseListener(listener);
}
}
exit = true;
}
exit = true;
}
}
}
}
}
};
}
}