在数组中将更多值设置为1?

时间:2014-12-19 12:46:28

标签: c# arrays

class spielfeld
{
    int[,] spielfeldgitter = new int[16, 16];

    public void spielfeldnullsetzen(/*PictureBox pictureBox1*/)
    {

        for (int i = 0; i < spielfeldgitter.GetLength(0); i++)
        {
            for (int j = 0; j < spielfeldgitter.GetLength(1); j++)
            {

                spielfeldgitter[i, j] = 0;

            }
        }
    }

    public void spielfeldol(PictureBox pictureBox1)
    {
        Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        Graphics feld = Graphics.FromImage(bmp);
        for (int i = 0; i < 16; i++)
        {
            feld.DrawLine(Pens.Black, 0, (320 / 16) * i, 320, (320 / 16) * i);
        }
        for (int i = 0; i < 16; i++)
        {
            feld.DrawLine(Pens.Black, (320 / 16) * i, 0, (320 / 16) * i, 320);
        }
        pictureBox1.Image = bmp;
    }

    public void mouseclick(int eX, int eY, PictureBox pictureBox1)
    {
        int cellw = (eX / 20);
        int cellh = (eY / 20);
        if (spielfeldgitter[cellw, cellh] != 1)
        {

            spielfeldgitter[cellw, cellh] = 1;
            Graphics rectangle = Graphics.FromImage(pictureBox1.Image);
            rectangle.FillRectangle(Brushes.Green, ((320 / 16) * cellw + 1), (320 / 16) * cellh + 1, (320 / 16) - 1, (320 / 16) - 1);
        }
        else
        {
            spielfeldgitter[cellw, cellh] = 0;
            Graphics rectangle = Graphics.FromImage(pictureBox1.Image);
            rectangle.FillRectangle(Brushes.White, ((320 / 16) * cellw + 1), (320 / 16) * cellh + 1, (320 / 16) - 1, (320 / 16) - 1);
        }
    }
}

所以它是src的片段,其中包含逻辑事物。

    private void Form1_Load(object sender, EventArgs e)
    {   
        Feld.spielfeldnullsetzen(/*pictureBox1*/);       
        Feld.spielfeldol(pictureBox1);
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        Feld.spielfeldol(pictureBox1);
    }

    private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {
        int eX = e.X;
        int eY = e.Y;
        Feld.spielfeldol(pictureBox1);
        Feld.mouseclick(eX, eY, pictureBox1);
    }


}

那就是form.cs  我的问题是我正在创建一个16d6大的二维数组,并将每个值设置为0.当我检查它时,断点说同样的事情。

然后当我在mouseclick上做断点时 特定的数组值也会改变,矩形的颜色也会改变。但我不能设法用另一种颜色制作多个字段

也许有人可以提供帮助?

2 个答案:

答案 0 :(得分:1)

public partial class Form1 : Form
{
    spielfeld Feld = new spielfeld();

    public Form1()
    {
        InitializeComponent();

        Load += Form1_Load;
        //pictureBox1.Click += pictureBox1_Click;
        pictureBox1.MouseClick += pictureBox1_MouseClick;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Feld.spielfeldnullsetzen();
        Feld.spielfeldol(pictureBox1);
    }

    //private void pictureBox1_Click(object sender, EventArgs e)
    //{
    //    Feld.spielfeldol(pictureBox1);
    //}

    private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {
        int eX = e.X;
        int eY = e.Y;
        //Feld.spielfeldol(pictureBox1);
        Feld.mouseclick(eX, eY, pictureBox1);
    }
}

与这个班级一起

class spielfeld
{
    int[,] spielfeldgitter = new int[16, 16];

    public void spielfeldnullsetzen(/*PictureBox pictureBox1*/)
    {

        for (int i = 0; i < spielfeldgitter.GetLength(0); i++)
        {
            for (int j = 0; j < spielfeldgitter.GetLength(1); j++)
            {

                spielfeldgitter[i, j] = 0;

            }
        }
    }

    public void spielfeldol(PictureBox pictureBox1)
    {
        Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        Graphics feld = Graphics.FromImage(bmp);
        for (int i = 0; i < 16; i++)
        {
            feld.DrawLine(Pens.Black, 0, (320 / 16) * i, 320, (320 / 16) * i);
        }
        for (int i = 0; i < 16; i++)
        {
            feld.DrawLine(Pens.Black, (320 / 16) * i, 0, (320 / 16) * i, 320);
        }
        pictureBox1.Image = bmp;
    }

    public void mouseclick(int eX, int eY, PictureBox pictureBox1)
    {
        int cellw = (eX / 20);
        int cellh = (eY / 20);
        if (spielfeldgitter[cellw, cellh] != 1)
        {

            spielfeldgitter[cellw, cellh] = 1;
            Graphics rectangle = Graphics.FromImage(pictureBox1.Image);
            rectangle.FillRectangle(Brushes.Green, ((320 / 16) * cellw + 1), (320 / 16) * cellh + 1, (320 / 16) - 1, (320 / 16) - 1);
        }
        else
        {
            spielfeldgitter[cellw, cellh] = 0;
            Graphics rectangle = Graphics.FromImage(pictureBox1.Image);
            rectangle.FillRectangle(Brushes.White, ((320 / 16) * cellw + 1), (320 / 16) * cellh + 1, (320 / 16) - 1, (320 / 16) - 1);
        }
        pictureBox1.Refresh();
    }
}

应该让你开始。

原始代码的更改:

    电路板鼠标点击方法
  1. pictureBox1.Refresh()
  2. 表单中的更改将发表评论

答案 1 :(得分:0)

这是因为spielfeldol清除了所有图像,而mouseclick只在先前清除的图像中绘制了一个矩形。因此,解决这个问题的方法很少:

  1. spielfeldol事件处理程序中删除pictureBox1_MouseClick方法调用,并添加某种按钮以调用spielfeldol方法来清除图像。
  2. spielfeldol方法重用pictureBox1.Image(不要创建新的位图 - 它将为空),就像在“鼠标点击”方法中一样。