Simon game. buttons wont flash... help!

This is a simple "Simon" memory game, produced in GUI...........

The main(); invokes the FIRE(); which generates random numbers , which in turns invokes one of four COLOR methods, red(), green(), yellow(), blue(); .... These methods flash one of the four buttons and are assigned their own values. when they are invoked, they pass that value to Pattern();

Pattern (); fills an array with the random values passed from the color methods, As well as compares users entry against the array and IF user entry is accurate then increase difficulty++

(Pattern method has poor logic in it. I would like tips on this.)

PROBLEM: it executes nearly perfect the first time the code cycles. After that the buttons no longer flash when the FIRE(); invokes COLOR(); methods.

THANKS IN ADVANCE FOR YOUR HELP.

import java.io.File;

import java.io.IOException;

import javax.sound.sampled.AudioFormat;

import javax.sound.sampled.AudioInputStream;

import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.DataLine;

import javax.sound.sampled.LineUnavailableException;

import javax.sound.sampled.SourceDataLine;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

publicclass Simonextends JFrameimplements MouseListener,ActionListener

{

//static int pat=1;

privatestaticfinalintEXTERNAL_BUFFER_SIZE = 128000;

static JButton but1 =new JButton("Green");

static JButton but2 =new JButton("Blue");

static JButton but3 =new JButton("Orange");

static JButton but4 =new JButton("Red");

static JPanel pane1 =new JPanel();

static JPanel pane2 =new JPanel();

static JPanel pane3 =new JPanel();

static JPanel pane4 =new JPanel();

static JPanel pane5 =new JPanel();

static JButton but5 =new JButton("Start");

//static JLabel lab1 = new JLabel("Simon Says");

Container con = getContentPane();

static File soundFile =new File("hiho.wav");

static File utopia =new File("utopia.wav");

static File cowbell =new File("cowbell.wav");

static File slap =new File("sound16.wav");

staticint G1=1,B2=2,Y3=3,R4=4;

staticint[] array =newint[256];// maximum value

staticint difficulty = 3;//initial value

staticint count;

staticint x=0;

staticboolean click=false;

public Simon()

{

super("Simon Says");

con.setLayout(new BorderLayout());

con.add(pane5,BorderLayout.CENTER);

con.add(pane4,BorderLayout.NORTH);

con.add(pane3,BorderLayout.SOUTH);

con.add(pane2,BorderLayout.EAST);

con.add(pane1,BorderLayout.WEST);

pane1.add(but1);

pane2.add(but2);

pane3.add(but3);

pane4.add(but4);

pane5.add(but5);

but1.addMouseListener(this);

but2.addMouseListener(this);

but3.addMouseListener(this);

but4.addMouseListener(this);

but1.addActionListener(this);

but2.addActionListener(this);

but3.addActionListener(this);

but4.addActionListener(this);

but5.addActionListener(this);

}

publicstaticvoid main(String[]args)

{

JFrame af =new Simon();

af.setSize(275,135);

///////////////////////////////////////////////////////////////////////

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

af.setLocation( (screenSize.width - af.getWidth())/2,

(screenSize.height - af.getHeight())/2 );

///////////////////////////////////////////////////////////////////

af.setVisible(true);

af.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

af.setResizable(false);

but1.setContentAreaFilled(false);

but1.setOpaque(true);//enabling button to changed with mouse events.

but2.setContentAreaFilled(false);

but2.setOpaque(true);

but3.setContentAreaFilled(false);

but3.setOpaque(true);

but4.setContentAreaFilled(false);

but4.setOpaque(true);

but1.setBackground(Color.darkGray);

but1.setForeground(Color.WHITE);

but2.setBackground(Color.darkGray);

but2.setForeground(Color.WHITE);

but3.setBackground(Color.darkGray);

but3.setForeground(Color.WHITE);

but4.setBackground(Color.darkGray);

but4.setForeground(Color.WHITE);

// everytime the pattern fires a button it invokes a method which

// recieves an int ... fills an array with digits. and before the int

// is assigned a subscript a variable is incremented so the array continues

// to cycle.

//store the last pattern in an array then, and do a check if they match generate new pattern.

FIRE();

}

publicstaticvoid FIRE()

{

for(x=0;x<difficulty;++x)

{

double rnd = Math.random()*10;

if (rnd><2.5)

{

play(soundFile);

green();

}

elseif (rnd>=2.5&&rnd<=5.0)

{

play(utopia);

blue();

}

elseif (rnd>5.0&&rnd<=7.5)

{

play(cowbell);

yellow();

}

else//if (rnd>7.5);

{

play(slap);

red();

}

}

if(x==difficulty)

count=0;

}

publicstaticvoid green()///////////////////////////////////////////////////////

{

but1.setBackground(Color.GREEN);

but1.setForeground(Color.WHITE);

try

{

Thread.sleep(200);

}

catch(InterruptedException e){}

but1.setBackground(Color.darkGray);

but1.setForeground(Color.WHITE);

try

{

Thread.sleep(200);

}

catch(InterruptedException e){}

Pattern(G1);

}

publicstaticvoid blue()////////////////////////////////////////////////////

{

but2.setBackground(Color.BLUE);

but2.setForeground(Color.WHITE);

try

{

Thread.sleep(200);

}

catch(InterruptedException e){}

but2.setBackground(Color.darkGray);

but2.setForeground(Color.WHITE);

try

{

Thread.sleep(200);

}

catch(InterruptedException e){}

Pattern(B2);

}

publicstaticvoid yellow()///////////////////////////////////////////////////////

{

but3.setBackground(Color.ORANGE);

but3.setForeground(Color.WHITE);

try

{

Thread.sleep(200);

}

catch(InterruptedException e){}

but3.setBackground(Color.darkGray);

but3.setForeground(Color.WHITE);

try

{

Thread.sleep(200);

}

catch(InterruptedException e){}

Pattern(Y3);

}

publicstaticvoid red()//////////////////////////////////////////////////////////

{

but4.setBackground(Color.RED);

but4.setForeground(Color.WHITE);

try

{

Thread.sleep(200);

}

catch(InterruptedException e){}

but4.setBackground(Color.darkGray);

but4.setForeground(Color.WHITE);

try

{

Thread.sleep(200);

}

catch(InterruptedException e){}

Pattern(R4);

}

publicvoid mousePressed(MouseEvent e)////////////////////////////////////////////////////////////////

{

//System.out.println(" Count Equals before Fire()"+count);

Object source = e.getSource();

if (source==but1)

{

click =true;

but1.setBackground(Color.GREEN);

but1.setForeground(Color.WHITE);

play(soundFile);

Pattern(G1);

}

if(source==but2)

{

click =true;

but2.setBackground(Color.BLUE);

but2.setForeground(Color.WHITE);

play(utopia);

Pattern(B2);

}

if (source==but3)

{

click =true;

but3.setBackground(Color.orange);

but3.setForeground(Color.WHITE);

play(cowbell);

Pattern(Y3);

}

if (source==but4)

{

click =true;

but4.setBackground(Color.RED);

but4.setForeground(Color.WHITE);

play(slap);

Pattern(R4);

}

}

publicvoid mouseReleased(MouseEvent e)/////////////////////////////////////////////////////////////

{

Object source = e.getSource();

if(source==but1)

{

but1.setBackground(Color.darkGray);

but1.setForeground(Color.WHITE);

}

elseif (source==but2)

{

but2.setBackground(Color.darkGray);

but2.setForeground(Color.WHITE);

}

elseif (source==but3)

{

but3.setBackground(Color.darkGray);

but3.setForeground(Color.WHITE);

}

elseif (source==but4)

{

but4.setBackground(Color.darkGray);

but4.setForeground(Color.WHITE);

}

}

publicstaticvoid Pattern(int A)/////////////////////////////////////////////////////////////

{

if (x<difficulty)

{

array[count]=A;

System.out.println("Randomly generated array position "+count+"'s value is, "+A);

++count;

}

if (x==difficulty)

{

System.out.println(".....User entry at array position "+count+"'s value is, "+A);

if (array[count]==A&&count==difficulty-1)//if user entry is correct and the array has reached

NEXT();

//array.length then invoke NEXT();

if (A!=array[count]/*&&count<difficulty*/&&count!=0)//poor logic: at anytime if array[count] is not equal to user entry, LOSE();

LOSE();

if (click==true)

count++;

}

click =false;

}

publicstaticvoid LOSE()

{

System.out.println("YOU LOSE");

System.exit(0);

}

publicstaticvoid NEXT()

{

//String[] s = null;

System.out.println("NEXT LEVEL");

++difficulty;

count=0;

x=0;

// Simon.main((s = new String[] {"N"}));

FIRE();

}

publicstaticvoid play(File soundFile)////////////////////////////////// AUDIO PLAYER

{

AudioInputStreamaudioInputStream =null;

try

{

audioInputStream = AudioSystem.getAudioInputStream(soundFile);

}

catch (Exception e)

{

e.printStackTrace();

System.exit(1);

}

AudioFormataudioFormat = audioInputStream.getFormat();

SourceDataLineline =null;

DataLine.Infoinfo =new DataLine.Info(SourceDataLine.class,audioFormat);

try

{

line = (SourceDataLine) AudioSystem.getLine(info);

line.open(audioFormat);

}

catch (LineUnavailableException e)

{

e.printStackTrace();

System.exit(1);

}

catch (Exception e)

{

e.printStackTrace();

System.exit(1);

}

line.start();

intnBytesRead = 0;

byte[]abData =newbyte[EXTERNAL_BUFFER_SIZE];

while (nBytesRead != -1)

{

try

{

nBytesRead = audioInputStream.read(abData, 0, abData.length);

}

catch (IOException e)

{

e.printStackTrace();

}

if (nBytesRead >= 0)

{

intnBytesWritten = line.write(abData, 0, nBytesRead);

}

}

//line.drain();

// line.close();

//System.exit(0);

}

privatevoid printUsageAndExit()

{

out("SimpleAudioPlayer: usage:");

out("\tjava SimpleAudioPlayer <soundfile>");

System.exit(1);

}

privatevoid out(String strMessage)

{

System.out.println(strMessage);

}

publicvoid actionPerformed(ActionEvent e)//OVERRIDDEN INTERFACE METHODS

{}

publicvoid mouseClicked(MouseEvent e)

{}

publicvoid mouseEntered(MouseEvent e)

{}

publicvoid mouseExited(MouseEvent e)

{}

}

[22952 byte] By [oasisfleetinga] at [2007-11-27 6:15:50]
# 1

In the future, Swing related questions should be posted in the Swing forum.

You don't use the Thread.sleep(...) method in the GUI Event Thread, it prevents the GUI from responding to events and repainting itself.

Use a Swing Timer to schedule the frequency of whatever you are doing.

camickra at 2007-7-12 17:26:55 > top of Java-index,Java Essentials,Java Programming...