矩形声明数组 [英] Array of Rectangles declaration problem

查看:68
本文介绍了矩形声明数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在尝试在C#中实现切片算法.我有几个问题,
我的方法是将网格的大小m x m作为输入,然后在我的图形对象上使用DrawRectangles方法绘制网格.然后,用户选择要在其周围平铺网格的瓷砖.
我的问题:
一个:OnPaint事件处理程序仅显示在窗体上调用时要生成的图形.我尝试在图片框控件和面板控件上调用它,但是图形不显示?
第二:我试图将我的矩形放置在数组中,以便我可以识别选择哪个矩形,并根据选择的tile(rectangle)应用切片算法.但它不起作用. NullExceptionReference是我运行代码时所说的.我是一个业余爱好者,所以请让我知道我是否错误地声明了它?还是我使用了错误的数据结构?

公共矩形[] rect;

Hey everyone,
Im trying to implement a tiling algorithm in C#. I''m having a couple of issues,
my approach is to take the size of a grid m x m as input, and then draw the grid using DrawRectangles method on my graphics object. the user then selects a tile around which the grid gets tiled.
My issues:
one : the OnPaint eventhandler only shows the graphics i want to generate when called on the form. I tried calling it on a picturebox control and a panel control but the graphics dont show up?
two: im trying to put my rectangles in an array so i can identify which rectangle is selected and apply a tiling algo based on which tile(rectangle) is selected. But its not working. NullExceptionReference is what is says when i run the code. Im an amateur, so please let me know if i''m declaring it incorrectly? or Am i using the wrong data structure?

public Rectangle [] rect;

rect[(m*m)]=new Rectangle();
           k=1;
           for(i=0;i<m;i++)
           {
               for(j=0;j<m;j++)
               {
                   rect [k]=new Rectangle((x+(j*offset)),(y+i*offset)),offset,offset);
                   g.DrawRectangle(myPen,rect[k]);
                   
                   k++;
               }
           }

推荐答案

在实例化一个矩形然后立即绘制它时,似乎没有任何理由使您的代码实际存储一个矩形数组.绘制矩形的一种简单方法是简单地将循环的内部替换为:
As you are instantiating a rectangle and then drawing it immediately, there doesn''t seem any reason for your code to be actually storing an array of rectangles. The simple way for you to draw the rectangle there is to simply replace the inner part of your loop with:
g.DrawRectangle(myPen, new Rectangle(x+(j*offset), (y+i*offset), offset, offset));

在其他地方是否有需要此矩形列表的处理?顺便说一句,这样做可以完全消除矩形数组的声明.

以下是OP对他的行为的澄清:

我将矩形的创建与矩形的绘制分开.当前面临的一个问题是,您将在每个OnPaint事件中实例化并填充数组.而是在绘制之前创建它,如下所示:

Is there processing elsewhere that requires this list of rectangles, or is it sufficient that you are simply drawing it. BTW, by doing this you can get rid of the rectangle array declaration altogether.

Following clarification from the OP as to what he was after:

I would separate the creation of the rectangle away from the drawing of the rectangle. One problem you currently face is that you are instantiating and populating the array in every OnPaint event. Instead, create it prior to drawing like this:

private List<Rectangle> rectangles = new List<Rectangle();
private void SetupRectangles(int m, int offset)
{
  for (int i = 0; i < m; i++)
  {
    for (int j = 0; j < m; j++)
    {
      rectangles.Add(new Rectangle(x+(j*offset), (y+i*offset), offset, offset));
    }
  }
}

然后在您的OnPaint处理程序中,您需要做的就是:

Then in your OnPaint handler, all you need do is this:

if (rectangles.Count == 0)
{
  // This will only happen once. This could be moved elsewhere, but I don't know   
  // where you are setting up offset and m.
  SetupRectangles(m, offset); 
}
foreach (Rectangle rect in rectangles)
{
  g.DrawRectangle(myPen, rect);
}

就这么简单.


这篇关于矩形声明数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆