Results 1 to 5 of 5

Thread: JOptionPane

  1. #1
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default JOptionPane

    I'm working on the game of life and everything is basically done but the option pane. I need the optionpane to state when the cells stop shifting to state that a steady state has been reached. I have the area I used for this commented out. If some one can help me with this I'll really appreciate it.
    Java Code:
    //************************************************************************************
    //TheGameOfLife.java							                  Frazho/Yazigi/Parker
    //This is the GUI of John Conway's The Game of Life.  All game calculations are sent 
    //over to GameCode.java
    //************************************************************************************
    
    //imports all functions needed for the GUI to work
    import java.awt.*;
    import java.awt.Color;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.Timer;
    import javax.swing.JFrame;
    
    public class TheGameOfLife extends JPanel {
        
        //declares all the variables needed in this program
        private static int generation = 0;
        private static JFrame frame;
        private static JComboBox preset;
        private static JPanel background, top, middle, bottom, counter;
        private static JButton start, stop, clear, next;
        private static Timer t;
        public static JLabel gen;
        private static JButton[][] cell;
        public static boolean done;
        
        public static void main(String[] args) {
        
        //the frame of the program is declared	
    	frame = new JFrame("Game Of Life");
    	frame.setPreferredSize(new Dimension (375, 500));
        frame.setSize(new Dimension(375, 500));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //the background of the entire program is declared here.
        background = new JPanel();
        background.setPreferredSize(new Dimension(370, 490));
        background.setSize(370, 490);
        frame.add(background);
        
        //the top panel of the program is declared here.
        top = new JPanel();
        top.setPreferredSize(new Dimension(400, 50));
        top.setSize(new Dimension(400, 50));
        background.add(top);
        
        //the list of selections for the JCombobox are listed here
       	String[] selection = {"Make a Selection", "10 Cell Row", "Glider", "Single Exploder"};
       	
       	//the JCombobox is declared here and added to the top panel
        preset = new JComboBox(selection);
        preset.setPreferredSize(new Dimension(130, 30));
        preset.setSize(new Dimension(130, 30));
        preset.setBackground(Color.cyan);
        preset.addActionListener(new ComboBoxListener());
        top.add(preset);
    
    	//the start button is declared here
        start = new JButton("Start");
        start.setPreferredSize(new Dimension(70, 30));
        start.setSize(new Dimension(70, 30));
        start.setBackground(Color.green);
        start.addActionListener(new StartListener());
        top.add(start);
        //the stop button is declared here
        stop = new JButton("Stop");
        stop.setPreferredSize(new Dimension(70, 30));
        stop.setSize(new Dimension(70, 30));
        stop.setBackground(Color.red);
        stop.addActionListener(new StopListener());
        top.add(stop);
        //the clear button is declared here
        clear = new JButton("Clear");
        clear.setPreferredSize(new Dimension(70, 30));
        clear.setSize(new Dimension(70, 30));
        clear.setBackground(Color.yellow);
        clear.addActionListener(new ClearListener());
        top.add(clear);
        //the middle panel is declared here
        middle = new JPanel();
        middle.setPreferredSize(new Dimension(360, 350));
        middle.setSize(new Dimension(360, 350));
        background.add(middle);
        //the bottom panel is declared here
        bottom = new JPanel();
        bottom.setPreferredSize(new Dimension(400, 50));
        bottom.setSize(new Dimension(400, 50));
        background.add(bottom);
        //the next generation button is declared here
        next = new JButton("Next Generation");
        next.setPreferredSize(new Dimension(150, 30));
        next.setSize(new Dimension(150, 30));
        next.setBackground(Color.green);
        next.addActionListener(new NextListener());
        bottom.add(next);
        //the background for the counter is declared here
        counter = new JPanel();
        counter.setPreferredSize(new Dimension(40, 30));
        counter.setSize(new Dimension(40, 30));
        counter.setBackground(Color.white);
        bottom.add(counter);
        //the display for the counter is declared here
        gen = new JLabel("" + generation);
        counter.add(gen);
        //the cells are declared here.
        //the array is a 22 by 22
        cell = new JButton[22][22];
        CellListener listener2 = new CellListener();
        //this for loop, loops for each button adding each one to the jpanel "middle"
        for(int number = 0; number <= 21; number++)
        {
        	for(int number2 = 0; number2 <= 21; number2++)
        	{
        		cell[number][number2] = new JButton();
        		cell[number][number2].setPreferredSize(new Dimension(11, 10));
        		cell[number][number2].setSize(new Dimension(11, 10));
        		cell[number][number2].setBackground(Color.white);
        		cell[number][number2].addActionListener(listener2);
        		middle.add(cell[number][number2]);
        	}
        }
        frame.pack();
        frame.setVisible(true);
    	//declares the timer and the rate its class is called
        t = new Timer(250, new TimerListener());
        //this makes sure that the array in GameCode is declared and initialized at zero
        done = GameCode.initialize();
        }
    	//-----------------------------------------------------------------------------------
    	//This timer class calls the GameCode.gamecalculations everytime it fires.  The GUI is 
    	//updated by the matrixtogui class.  The boolean done is just a placeholder for the
    	//program to return to the spot it left off at.
    	//------------------------------------------------------------------------------------
    	public static class TimerListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			int value;
    			done = GameCode.gamecalculation();
    			for(int number = 0; number <= 21; number++)
    			{
    				for(int number2 = 0; number2 <= 21; number2++)
    				{
    					value = GameCode.matrixtogui(number, number2);
    						if(value == 1)
    						{
    							cell[number][number2].setBackground(Color.blue);
    						}
    						else
    						{
      							cell[number][number2].setBackground(Color.white);
    						}
    						
    				}
    			}
    			//increments the generation counter by one
            	generation++;
            	//updates the generation text
    			gen.setText("" + generation);
    //			if(done == true)
    //			{
    //				JOptionPane.showMessageDialog(Game Over, "Society Has Reached a Steady State");
    //				t.stop();
    //			}
    		}
    	}
    	//the start listener just turns the timer on
    	public static class StartListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			t.start();
    		}
    	}
    	//the stop listener just turns the timer off
    	public static class StopListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			t.stop();
    		}
    	}
    	//the clear listener stops the timer and clears the array in GameCode and the button
    	//array in the game of life.
    	public static class ClearListener extends JPanel implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			t.stop();
    			boolean done = GameCode.initialize();
     			for(int number = 0; number <= 21; number++)
    			{
    				for(int number2 = 0; number2 <= 21; number2++)
    				{
    					int value = GameCode.matrixtogui(number, number2);
    						if(value == 1)
    						{
    							cell[number][number2].setBackground(Color.blue);
    						}
    						else
    						{
    							cell[number][number2].setBackground(Color.white);
    						}
    					}
    				}
    			generation = 0;
    			gen.setText("" + generation);
    			preset.setSelectedIndex(0);
    			}
    		}
    	//goes through one single loop in GameCode.gamecalculations and increments the
    	//generation value by one.
    	public static class NextListener extends JPanel implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    		done = GameCode.gamecalculation();
    		 	for(int number = 0; number <= 21; number++)
    			{
    				for(int number2 = 0; number2 <= 21; number2++)
    				{
    					int value = GameCode.matrixtogui(number, number2);
    						if(value == 1)
    						{
    							cell[number][number2].setBackground(Color.blue);
    						}
    						else
    						{
    							cell[number][number2].setBackground(Color.white);
    						}
    					}
    				}
    		generation++;
    		gen.setText("" + generation);
    		}
    	}
    	//takes the result from the combobox and displays the preset on the game board
    	public static class ComboBoxListener extends JPanel implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    		if(preset.getSelectedIndex() == 1)
    		{
    			System.out.println("works");
    			done = GameCode.initialize();
    			done = GameCode.preset(1);
    			for(int number = 0; number <= 21; number++)
    			{
    				for(int number2 = 0; number2 <= 21; number2++)
    				{
    					int value = GameCode.matrixtogui(number, number2);
    						if(value == 1)
    						{
    							cell[number][number2].setBackground(Color.blue);
    						}
    						else
    						{
    							cell[number][number2].setBackground(Color.white);
    						}
    					}
    				}
    			generation = 0;
    			gen.setText("" + generation);
    		}
    		if(preset.getSelectedIndex() == 2)
    		{
    		 	done = GameCode.initialize();
    		 	done = GameCode.preset(2);
    		 	for(int number = 0; number <= 21; number++)
    			{
    				for(int number2 = 0; number2 <= 21; number2++)
    				{
    					int value = GameCode.matrixtogui(number, number2);
    						if(value == 1)
    						{
    							cell[number][number2].setBackground(Color.blue);
    						}
    						else
    						{
    							cell[number][number2].setBackground(Color.white);
    						}
    					}
    				}
    			generation = 0;
    			gen.setText("" + generation);
    		}	
    		if(preset.getSelectedIndex() == 3)
    		{
    			done = GameCode.initialize();
    			done = GameCode.preset(3);
    			for(int number = 0; number <= 21; number++)
    			{
    				for(int number2 = 0; number2 <= 21; number2++)
    				{
    					int value = GameCode.matrixtogui(number, number2);
    						if(value == 1)
    						{
    							cell[number][number2].setBackground(Color.blue);
    						}
    						else
    						{
    							cell[number][number2].setBackground(Color.white);
    						}
    					}
    				}
    			generation = 0;
    			gen.setText("" + generation);
    		}
    			
    		}
    	}
    	//allows the user to click on the array of buttons and "create cells"
    	public static class CellListener extends JPanel implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    
    			int number3 = 0, number4 = 0;
    			//looks for which cell was clicked and changes it to blue
    			for(int number = 0; number <= 21; number++)
    			{
    				for(int number2 = 0; number2 <= 21; number2++)
    				{
    					if(cell[number][number2] == event.getSource())
    					{
    						number3 = number;
    						number4 = number2;
    						number = 22;
    						number2 = 22;
    					}
    				}
    			}
    			//tried to get the cells to turn white as well but for some reason this code didn't work
    			if(GameCode.buttonclick(number3, number4) == 1)
    			{
    				cell[number3][number4].setBackground(Color.blue);
    			}
    			else if(GameCode.buttonclick(number3, number4) == 0)
    			{
    				cell[number3][number4].setBackground(Color.white);
    			}
    		}
    	}
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What happens? errors? provide more details please.

  3. #3
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default

    I'm not having any errors, I am having trouble putting together the JOptionPane with the code I have.
    Last edited by thorobred; 04-19-2011 at 03:48 AM.

  4. #4
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default

    I forgot to add the game code to this sorry
    Java Code:
    public class GameCode {
    
        private static int[][] gamecalculations;
        private static int[][] gamecalculations2;
       //declares the array gamecalculations and initializes it at zero.
        public static boolean initialize()
        {
        	gamecalculations = new int[22][22];
        	
        	for(int number = 0; number <= 21; number++)
        	{
        		for(int number2 = 0; number2 <= 21; number2++)
        		{
        			gamecalculations[number][number2] = 0;
        		}
        	}
        	return true;
        }
        //just returns the value of the current matrix to TheGameOfLife.java
        public static int matrixtogui(int number, int number2)
        {
        	return gamecalculations[number][number2];
        }
        //--------------------------------------------------------------------------------
        //This program finds out which cells are born, which die, and which just stay alive.
        //The eight spaces around each cell is checked and tallied into a integer called
        //neighbor.  After that this value is checked according to the rules of the game
        //--------------------------------------------------------------------------------
        public static boolean gamecalculation()
        {
        	int neighbors;
        	//declares gamecalculations2 and copies gamecalculations values to it.
        	gamecalculations2 = new int[22][22];
        	for(int number = 0; number <= 21; number++)
        	{
        		for(int number2 = 0; number2 <= 21; number2++)
        		{
        			gamecalculations2[number][number2] = gamecalculations[number][number2];
        		}
        		
        	}
        	//this loops through each cell.  upon the selection of a cell it is first determined
        	//to be alive or dead
           	for(int number = 0; number <= 21; number++)
        	{
        		for(int number2 = 0; number2 <= 21; number2++)
        		{	
        			//neighbors is initialized at zero
        			neighbors = 0;
        			//cell is dead, now checks to see if there are three neighbors, if there are
        			//a new cell is born
        			//this section checks to make sure that it doesn't go off the grid to check to
        			//see if there are any neighbors
        			if(gamecalculations2[number][number2] == 0)
        			{
        				if((number - 1) >= 0 && (number2 - 1) >= 0)
        				{
        					if(gamecalculations2[number - 1][number2 - 1] == 1)
        					{
        						neighbors++;
        					}
        				}
        				if((number - 1) >= 0)
        				{
        					if(gamecalculations2[number - 1][number2] == 1)
        					{
        						neighbors++;
        					}
        				}
        				if((number2 - 1) >= 0)
        				{
        					if(gamecalculations2[number][number2 - 1] == 1)
        					{
        						neighbors++;
        					}
        				}
        				if((number + 1) <= 21 && (number2 + 1) <= 21  )
        				{
        					if(gamecalculations2[number + 1][number2 + 1] == 1)
        					{
        						neighbors++;
        					}
        				}
        				if((number + 1) <= 21)
        				{
        					if(gamecalculations2[number + 1][number2] == 1)
        					{
        						neighbors++;
        					}
        				}
        				if((number2 + 1) <= 21)
        				{
        					if(gamecalculations2[number][number2 + 1] == 1)
        					{
        						neighbors++;
        					}
        				}	
        				if((number - 1) >= 0 && (number2 + 1) <= 21)
        				{
        					if(gamecalculations2[number - 1][number2 + 1] == 1)
        					{
        						neighbors++;
        					}
        				}
        				if((number + 1) <= 21 && (number2 - 1) >= 0)
        				{
        					if(gamecalculations2[number + 1][number2 - 1] == 1)
        					{
        						neighbors++;
        					}
        				}
        				if(neighbors == 3)
        				{
        					gamecalculations[number][number2] = 1;
        				}
        			}
      				//cell is alive, now checks to see if there are two or three neighbors, if there are
        			//then the cell stays alive, if not it dies
        			//this section checks to make sure that it doesn't go off the grid to check to
        			//see if there are any neighbors
        			else if(gamecalculations2[number][number2] == 1) 
        			{
        				if((number - 1) >= 0 && (number2 - 1) >= 0)
        				{
        					if(gamecalculations2[number - 1][number2 - 1] == 1)
        					{
        						neighbors++;
        					}
        				}
        				if((number - 1) >= 0)
        				{
        					if(gamecalculations2[number - 1][number2] == 1)
        					{
        						neighbors++;
        					}
        				}
        				if((number2 - 1) >= 0)
        				{
        					if(gamecalculations2[number][number2 - 1] == 1)
        					{
        						neighbors++;
        					}
        				}
        				if((number + 1) <= 21 && (number2 + 1) <= 21  )
        				{
        					if(gamecalculations2[number + 1][number2 + 1] == 1)
        					{
        						neighbors++;
        					}
        				}
        				if((number + 1) <= 21)
        				{
        					if(gamecalculations2[number + 1][number2] == 1)
        					{
        						neighbors++;
        					}
        				}
        				if((number2 + 1) <= 21)
        				{
        					if(gamecalculations2[number][number2 + 1] == 1)
        					{
        						neighbors++;
        					}
        				}	
        				if((number - 1) >= 0 && (number2 + 1) <= 21)
        				{
        					if(gamecalculations2[number - 1][number2 + 1] == 1)
        					{
        						neighbors++;
        					}
        				}
        				if((number + 1) <= 21 && (number2 - 1) >= 0)
        				{
        					if(gamecalculations2[number + 1][number2 - 1] == 1)
        					{
        						neighbors++;
        					}
        				}
        				if(neighbors > 3 || neighbors < 2)
        				{
        					gamecalculations[number][number2] = 0;
        				}	
        			}
        		}
        	}
        	//checks to see if gamecalculations has changed at all.
        	int similar = 0;
        	for(int number = 0; number <= 21; number++)
        	{
        		for(int number2 = 0; number2 <= 21; number2++)
        		{
        			if(gamecalculations[number][number2] == gamecalculations2[number][number2])
        			{
        				similar++;
        			}
        		}
        	}
        	if(similar == 424)
        	{
        		return true;
        	}
        	else
        	{
        		return false;
        	}
        }
        //updates this matrix to reflect the button click
        public static int buttonclick(int number, int number2)
        {
        	int temp = 0;
        	if(gamecalculations[number][number2] == 1)
        	{
        		temp = 0;
        	}
        	else if(gamecalculations[number][number2] == 0)
        	{
        		temp = 1;
        	}
        	gamecalculations[number][number2] = temp;
        	return gamecalculations[number][number2];
        }
        //updates this matrix to whatever preset is selected.  The presets are
        //stored here.
        public static boolean preset(int number)
        {
        	if(number == 1)
        	{
        		gamecalculations[11][6] = 1;
        		gamecalculations[11][7] = 1;
        		gamecalculations[11][8] = 1;
        		gamecalculations[11][9] = 1;
        		gamecalculations[11][10] = 1;
        		gamecalculations[11][11] = 1;
        		gamecalculations[11][12] = 1;
        		gamecalculations[11][13] = 1;
        		gamecalculations[11][14] = 1;
        		gamecalculations[11][15] = 1;
        	}
        	if(number == 2)
        	{
        		gamecalculations[11][11] = 1;
        		gamecalculations[11][12] = 1;
        		gamecalculations[12][11] = 1;
        		gamecalculations[12][10] = 1;
        		gamecalculations[10][10] = 1;
        	}
        	if(number == 3)
        	{
        		gamecalculations[9][11] = 1;
        		gamecalculations[10][10] = 1;
        		gamecalculations[11][10] = 1;
        		gamecalculations[12][11] = 1;
        		gamecalculations[10][12] = 1;
        		gamecalculations[11][12] = 1;
        	}
        	return true;
        }
    }

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

Similar Threads

  1. need help with a JOptionPane
    By dr4g0nk1ng in forum Advanced Java
    Replies: 2
    Last Post: 02-19-2010, 09:40 PM
  2. need help for JOptionPane....
    By keycoffee in forum New To Java
    Replies: 1
    Last Post: 02-09-2010, 08:59 AM
  3. JOptionpane
    By tommyyyy in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 08:33 AM
  4. JOptionPane
    By Mir in forum New To Java
    Replies: 5
    Last Post: 11-29-2008, 02:04 AM
  5. JOptionPane
    By whosadork in forum New To Java
    Replies: 2
    Last Post: 10-23-2008, 02:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •