Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Simon game. buttons wont flash... help!

807606Jun 3 2007 — edited Jun 3 2007
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.*;
 
    public class Simon extends JFrame implements MouseListener,ActionListener
   {
      //static int pat=1;
      private static final int	EXTERNAL_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");
   
      static int G1=1,B2=2,Y3=3,R4=4;
      static int[] array = new int[256]; // maximum value
   	
      static int difficulty = 3; //initial value
      static int count;
      static int x=0;
      static boolean 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);
      
      }
   	
       public static void 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();
      }
       
       public static void FIRE()
      {
         
         for(x=0;x<difficulty;++x)
         {      
            double rnd = Math.random()*10;
            if (rnd<2.5)
            {
               play(soundFile);    
               green();
            }  
            else if (rnd>=2.5&&rnd<=5.0)
            {  
               play(utopia);
               blue();
            }
            else if (rnd>5.0&&rnd<=7.5)
            {
               play(cowbell);
               yellow();
            }  
            else //if (rnd>7.5);
            {
               play(slap);
               red();
            }
         
         }
         if(x==difficulty)
            count=0;
        
        
      }
       public static void 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);
      }
       public static void 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);  
      }
       public static void 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);  
      }
       public static void 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);
      	  
      }
       public void 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);
         }

      }
       public void mouseReleased(MouseEvent e)/////////////////////////////////////////////////////////////
      {
      
         Object source = e.getSource();
         if(source==but1)
         {
            but1.setBackground(Color.darkGray);
            but1.setForeground(Color.WHITE);  
         }
         else if (source==but2)
         {
            but2.setBackground(Color.darkGray);
            but2.setForeground(Color.WHITE);
         }
         else if (source==but3)
         {
            but3.setBackground(Color.darkGray);
            but3.setForeground(Color.WHITE);
         }
         else if (source==but4)
         {
            but4.setBackground(Color.darkGray);
            but4.setForeground(Color.WHITE);
         }
          
      }
   
       public static void 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;	            
         
      }
       public static void LOSE()
      {
         System.out.println("YOU LOSE");
         System.exit(0);
      }
       public static  void NEXT()
      {
	//	String[] s = null;
         System.out.println("NEXT LEVEL");
         ++difficulty;
         count=0;
         x=0;
       
		 // Simon.main((s = new String[] {"N"}));
         FIRE();
      }
       public static void play(File soundFile)////////////////////////////////// AUDIO PLAYER
      {
         AudioInputStream	audioInputStream = null;
         try
         {
            audioInputStream = AudioSystem.getAudioInputStream(soundFile);
         }
             catch (Exception e)
            {
               e.printStackTrace();
               System.exit(1);
            }
      
         AudioFormat	audioFormat = audioInputStream.getFormat();
         SourceDataLine	line = null;
         DataLine.Info	info = 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();
         int	nBytesRead = 0;
         byte[]	abData = new byte[EXTERNAL_BUFFER_SIZE];
         while (nBytesRead != -1)
         {
            try
            {
               nBytesRead = audioInputStream.read(abData, 0, abData.length);
            }
                catch (IOException e)
               {
                  e.printStackTrace();
               }
            if (nBytesRead >= 0)
            {
               int	nBytesWritten = line.write(abData, 0, nBytesRead);
            }
         }
      //       line.drain();
      //          line.close();
         //System.exit(0);
      }
       private void printUsageAndExit()
      {
         out("SimpleAudioPlayer: usage:");
         out("\tjava SimpleAudioPlayer <soundfile>");
         System.exit(1);
      }
       private void out(String strMessage)
      {
         System.out.println(strMessage);
      }
       public void actionPerformed(ActionEvent e)//OVERRIDDEN INTERFACE METHODS
      {}
       public void mouseClicked(MouseEvent e)
      {}
       public void mouseEntered(MouseEvent e)
      {}  
       public void mouseExited(MouseEvent e)
      {}  
   }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 1 2007
Added on Jun 3 2007
1 comment
359 views