如何使用 g.getColor() 更改 Java 中的数组元素?

How to change array element in Java using g.getColor()?

这是我第一次 post 访问此站点,但我花了很多时间寻找问题的解决方案,但似乎无法在任何地方找到它。本质上,我的任务是在 Java 小程序中开发 Game of Life 程序。我们的团队希望根据单元格的颜色更改数组中的元素(代表 'cells'),用户通过鼠标单击更改该颜色(红色 = 活的(1),白色 = 死的(0)) .因此,我们尝试编写以下代码:

^^^编辑^^^

我已经通过手动操作获得了要填充的数组。我现在遇到的唯一问题是,旨在更改值的嵌套 for 循环仅查看网格的线条,而不是单元格本身。这是所有请求者的 class。再次感谢您的帮助!

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<40; i++)
       {
           for(int j = 0; j<40; j++)
           {
               thisGen[i][j] = 0;
           }
       }      
       Graphics g = getGraphics();

   for(int i = 0; i<40; i++)
   {
       for(int j = 0; j<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 & 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 <=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;
}
}

我在你的代码中没有任何例外,只有两个建议。

int[][] thisGen = new int[40][40]; 

public void actionPerformed(ActionEvent e){
       Graphics g = getGraphics();
       for(int i = 0; i<40; i++)
       {
           for(int j = 0; j<40; j++)
           {
               if(g.getColor() == Color.red)
               {
                   thisGen[i][j] = 1;
               }
               else
               {
                   thisGen[i][j] = 0;
               }
           }
       }

       //////1. I don't feel there is any link between these two loops

        for(int x = 0; x<thisGen.length; x++)
        {
           for(int y = 0; y<thisGen[x].length; y++)     //2. replaced o by x if you've Jagged array
           {
               System.out.println(thisGen[x][y] +" ");
           }
           System.out.println();
       }
}