2017-04-23 88 views
0

這是我在這個網站上的第一篇文章,但我花了很多時間尋找我的問題的解決方案,只是似乎無法找到任何地方。基本上,我一直負責用Java applet開發遊戲生活程序。我們的團隊希望根據單元格的顏色(通過鼠標單擊(red = living(1),white = dead(0)))更改陣列中的元素(代表「單元格」)。所以,我們試着寫了下面的代碼:如何使用g.getColor()在Java中更改數組元素?

^^^ 編輯 ^^^

我已經得到了陣列通過做手工來填充。我現在唯一的問題是,意圖更改值的嵌套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; 
} 
} 
+1

你初始化了thisGen數組嗎? – Aryan

+0

是的,我有。這裏是代碼:int [] [] thisGen = new int [40] [40]; – bmancod4

+0

是否有可能看到全班?當您在本地啓動代碼時,您在此處顯示的代碼無錯誤運行... – DiabolicWords

回答

0

我沒有任何異常,在你的代碼中只有兩條建議。

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(); 
     } 
} 
相關問題