Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用g.getColor()在Java中更改数组元素?_Java_Arrays_Applet - Fatal编程技术网

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

如何使用g.getColor()在Java中更改数组元素?,java,arrays,applet,Java,Arrays,Applet,这是我在这个网站上的第一篇帖子,但我花了很多时间寻找解决问题的方法,但似乎在任何地方都找不到。基本上,我的任务是在Java小程序中开发生活游戏程序。我们的团队希望根据单元格的颜色来更改数组中的元素(表示“单元格”),用户可以通过鼠标单击来更改单元格的颜色(红色=存活(1),白色=死亡(0))。因此,我们尝试编写以下代码: ^^^编辑^^^ 我已经通过手动操作来填充数组。我现在唯一的问题是,用于更改值的嵌套for循环只查看网格线,而不是单元格本身。这是为那些提出要求的人准备的全班课程。再次感谢你的

这是我在这个网站上的第一篇帖子,但我花了很多时间寻找解决问题的方法,但似乎在任何地方都找不到。基本上,我的任务是在Java小程序中开发生活游戏程序。我们的团队希望根据单元格的颜色来更改数组中的元素(表示“单元格”),用户可以通过鼠标单击来更改单元格的颜色(红色=存活(1),白色=死亡(0))。因此,我们尝试编写以下代码:

^^^编辑^^^

我已经通过手动操作来填充数组。我现在唯一的问题是,用于更改值的嵌套for循环只查看网格线,而不是单元格本身。这是为那些提出要求的人准备的全班课程。再次感谢你的帮助

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;
}
}
import java.awt.*;
导入javax.swing.*;
导入java.awt.event.MouseListener;
导入java.awt.event.MouseEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
导入java.util.array;
/**
*CS120类-在这里写一个类的描述
* 
*@author(你的名字)
*@version(版本号)
*/
CS120的公共类扩展了JApplet
{
//实例变量-将下面的示例替换为您自己的
int oldx,oldy;
JFrame myFrame=新的JFrame();
JPanel aPanel=新的JPanel();
JButton aButton=新JButton(“下一代”);
JButton clearButton=新JButton(“清除板”);
int[]thisGen=新int[40][40];
私有类ButtonListener实现ActionListener
{
已执行的公共无效操作(操作事件e)
{

对于(inti=0;i我在代码中没有任何异常,只有两个建议

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();
       }
}
int[]thisGen=newint[40][40];
已执行的公共无效操作(操作事件e){
Graphics g=getGraphics();

对于(int i=0;我是否已初始化thisGen数组?是的,我已初始化。以下是代码:int[][]thisGen=new int[40][40];是否可以查看整个类?我在本地启动时,您在此处显示的代码不会出现错误…在初始化
thisGen
数组时,我在运行代码时不会出现任何错误。我将使用整个类编辑我的帖子,因为我已经做了一些调整,现在面临不同的问题。