填充矩形
我创建了一个 winform 程序,它使用 Graphics 对象和 2 个 for 循环来根据用户输入生成方形网格。
我还创建了一种方法,通过使用与网格相同的坐标,用随机颜色填充网格中的每个方块。
现在我想通过使用光标位置单击每个方块来独立绘制每个方块。我应该怎么做?
Ive created a winform-program that uses a Graphics object and 2 for-loops to generate a square grid, depending on user input.
I also created a method that fills each square in the grid with a random color by using the same coordinates as the grid.
Now I want to paint each square independently by clicking on it, using the cursor position. How should i do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不跟踪网格线,从而知道您在哪个方块内单击了呢?根据这些知识,你可以在一个人所属的地方画出一个正方形。
Why don't you just keep track of the gridlines and thus know what square you clicked within? From this knowledge you could paint a square where one belongs.
洪水填充是最简单的。与其他方法相比,它的速度较慢,并且会占用堆栈空间,但对于使用时间不到 15 年的计算机来说,这应该不成问题。
更新
正如@Ron提到的,典型的递归洪水填充很容易破坏堆栈。因此,我修改了代码以使用
Stack<>
实例(我相信它是从堆分配的)和所谓的“数据递归”。对于大(2000x2000+像素)区域来说它仍然相当慢,但对于小区域来说应该没问题。注意我什至没有尝试编译它。
基本上:
A flood fill is easiest. It's slow compared to other methods and eats stack space, but it shouldn't be a problem on a computer that's less than 15 years old.
Update
As @Ron mentioned, a typical recursive floodfill blows the stack pretty easily. So, I modified the code to use a
Stack<>
instance (which I believe is allocated from the heap) and so-called "data recursion". It's still pretty slow for large (2000x2000+ pixel) areas, but should be just fine for small ones.Note that I haven't even tried to compile this.
Basically: