title: How to change array element in Java using g.getColor()?
body: <p>This is my first post on this site, but I have spent a lot of time looking for a solution to my problem and just can't seem to find it anywhere. Essentially, I've been tasked with developing the Game of Life program in a Java applet. Our team wants to change the elements in our array(which represent the 'cells') based on the color of the cell, which the user changes via mouse click(red = living(1), white = dead(0)). So, we tried writing the following code:</p>
  
  <p>^^^<strong>EDIT</strong>^^^</p>
  
  <p>I've gotten the array to populate by doing it manually. The only problem I have now is that the nested for loops that are meant to change the values are only looking on the lines of the grid, not the cells themselves. Here is the whole class for those who requested. Once again, thanks for your help!</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  import java.awt.event.MouseListener;
  import java.awt.event.MouseEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.ActionEvent;
  import java.util.Arrays;
  
  /**
   * Class ForCs120 - write a description of the class here
   * 
   * @author (your name) 
   * @version (a version number)
  */
  
  public class ForCs120 extends JApplet
  {
      // instance variables - replace the example below with your own
      int oldx, oldy;
  
  JFrame myFrame = new JFrame();
  JPanel aPanel = new JPanel();
  JButton aButton = new JButton("Next Generation");
  JButton clearButton = new JButton("Clear Board");
  int[][] thisGen = new int[40][40];
  private class ButtonListener implements ActionListener
  {
     public void actionPerformed(ActionEvent e)
     {
         for(int i = 0; i&lt;40; i++)
         {
             for(int j = 0; j&lt;40; j++)
             {
                 thisGen[i][j] = 0;
             }
         }      
         Graphics g = getGraphics();
  
     for(int i = 0; i&lt;40; i++)
     {
         for(int j = 0; j&lt;40; j++)
         {
             g.getColor();
             if(g.getColor() == Color.red)
             {
                 thisGen[i][j] = 1;
             }
             else
             {
                 thisGen[i][j] = 0;
             }
         }
         g.drawString(Arrays.deepToString(thisGen), 20, 20);
     }      
  
  
  }
  }
  ButtonListener aButtonListener = new ButtonListener();
  private class ClearButtonListener implements ActionListener
  {
     public void actionPerformed(ActionEvent e)
     {
         Graphics g = getGraphics();
         g.drawString("You need to add code to clear the board", 10,50);
     }
  }
  ClearButtonListener clear = new ClearButtonListener();
      /**
       * Called by the browser or applet viewer to inform this JApplet that it
       * has been loaded into the system. It is always called before the first 
       * time that the start method is called.
       */
      public void init()
      {
          // this is a workaround for a security conflict with some browsers
          // including some versions of Netscape &amp; Internet Explorer which do 
          // not allow access to the AWT system event queue which JApplets do 
          // on startup to check access. May not be necessary with your browser. 
          JRootPane rootPane = this.getRootPane();    
          rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
          oldx = -1;
          oldy = -1;
          this.addMouseListener(myListener);
          aButton.setSize(150,150);
     aButton.addActionListener(aButtonListener);
     clearButton.addActionListener(clear);
     myFrame.setSize(200,200);
     this.setSize(400,400);
     aPanel.add(aButton);
     aPanel.add(clearButton);
     myFrame.add(aPanel);
     myFrame.setVisible(true);   
  
      // provide any initialisation necessary for your JApplet
  }
  
  /**
   * Called by the browser or applet viewer to inform this JApplet that it 
   * should start its execution. It is called after the init method and 
   * each time the JApplet is revisited in a Web page. 
   */
  public void start()
  {
      // provide any code requred to run each time 
      // web page is visited
  }
  
  /** 
   * Called by the browser or applet viewer to inform this JApplet that
   * it should stop its execution. It is called when the Web page that
   * contains this JApplet has been replaced by another page, and also
   * just before the JApplet is to be destroyed. 
   */
  public void stop()
  {
      // provide any code that needs to be run when page
      // is replaced by another page or before JApplet is destroyed 
  }
  
  /**
   * Paint method for applet.
   * 
   * @param  g   the Graphics object for this applet
   */
  
  private class MousePressedListener implements MouseListener
  {
  
     public void mousePressed(MouseEvent e)
     {
         //int [][] firstGen = new int[40][40]
         //for(int i=0;
         Graphics g = getGraphics();
         int x = e.getX();
         int y = e.getY();
         int d = x/10;
         int c = y/10;
         g.setColor(Color.red);
         g.fillRect(d*10, c*10, 10, 10);
     }
  
     public void mouseReleased(MouseEvent e)
     {
     }
  
     public void mouseClicked(MouseEvent e)
     {
         Graphics g = getGraphics();
     }
  
     public void mouseEntered(MouseEvent e)
     {
     }
  
     public void mouseExited(MouseEvent e)
     {
     }
      }
  
  MousePressedListener myListener = new MousePressedListener();
  
  public void paint(Graphics g)
  {
      g.setColor(Color.white);
      g.fillRect(0,0,400,400);
      g.setColor(Color.black);
      for(int i = 0; i &lt;=this.getWidth(); i+=10)
      {
          g.drawLine(i, 0, i, 400);
          g.drawLine(0, i, 400, i);
      }
  }
  
  /**
   * Called by the browser or applet viewer to inform this JApplet that it
   * is being reclaimed and that it should destroy any resources that it
   * has allocated. The stop method will always be called before destroy. 
   */
  public void destroy()
  {
      // provide code to be run when JApplet is about to be destroyed.
  }
  
  
  /**
   * Returns information about this applet. 
   * An applet should override this method to return a String containing 
   * information about the author, version, and copyright of the JApplet.
   *
   * @return a String representation of information about this JApplet
   */
  public String getAppletInfo()
  {
      // provide information about the applet
      return "Title:   \nAuthor:   \nA simple applet example description. ";
  }
  
  
  /**
   * Returns parameter information about this JApplet. 
   * Returns information about the parameters than are understood by this JApplet.
   * An applet should override this method to return an array of Strings 
   * describing these parameters. 
   * Each element of the array should be a set of three Strings containing 
   * the name, the type, and a description.
   *
   * @return a String[] representation of parameter information about this JApplet
   */
  public String[][] getParameterInfo()
  {
      // provide parameter information about the applet
      String paramInfo[][] = {
               {"firstParameter",    "1-10",    "description of first parameter"},
               {"status", "boolean", "description of second parameter"},
               {"images",   "url",     "description of third parameter"}
      };
      return paramInfo;
  }
  }
  </code></pre>