0
これはこのサイトの最初の投稿ですが、問題の解決策を探すのに多くの時間を費やしており、どこにも見つからないようです。本質的に、私はJavaアプレットでGame of Lifeプログラムを開発することを任されてきました。私たちのチームは、ユーザーがマウスクリック(赤=生きている、白=死んでいる(0))によって変化するセルの色に基づいて、配列の要素(セルを表す)を変更したいと考えています。私は手動でそれをすることによって移入する配列を得ているg.getColor()を使用してJavaで配列要素を変更する方法は?
^^^ EDIT ^^^
:だから、私たちは、次のコードを書いてみました。私が今抱えている唯一の問題は、値を変更するためのネストされた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;
}
}
このジェーン配列を初期化しましたか? – Aryan
はい、あります。ここにコードはあります:int [] [] thisGen = new int [40] [40]; – bmancod4
クラス全体を見ることはできますか?ここに表示されているコードは、ローカルで起動するとエラーから解放されます。 – DiabolicWords