动态创建矩形网格

发布于 2024-12-11 13:13:32 字数 305 浏览 1 评论 0原文

我想创建康威的生命游戏。我阅读了 Java 2d API,但 Graphics 类仅提供 JPanel 的 PaintComponent 上的 drawRect()fillRect() 方法。我的意思是,这些矩形不能作为对象单独处理,这样我就可以检查哪个矩形与附近的矩形有关。
所以我想问如何制作正方形,以便可以单独处理它们并动态创建网格?

I wanted to create Conway's Game of Life. I read the Java 2d API, but the Graphics class only provides methods to drawRect() and fillRect() on the paintComponent of a JPanel. I mean that the rectangles cannot be handled individually as objects i.e. so that i can check which one is on in relation to the ones in the vicinity.
So I wanted to ask how are squares to be made so that they can be handled individually and the grid be created dynamically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏日落 2024-12-18 13:13:32

创建一个具有所需所有属性的 Square 类。
创建代表棋盘的 Square 对象列表。
在 JPanel 的绘制方法中,迭代 Square 对象列表,根据其属性绘制每个对象。

尽可能将显示代码与逻辑分开 - 这几乎总是一个好主意。

Create a Sqaure class with all properties required.
Create a list of Square objects representing the board.
In the draw method for the JPanel, iterate over your list of Square objects, drawing each one out, based on its properties.

Keep your display code separate from your logic as much as possible - it's nearly always a good idea.

渡你暖光 2024-12-18 13:13:32

我想提出一个完全不同的解决方案。通常必须将生成的图形视为仅输出,这意味着您不想从图形中读取状态,因为那样太慢了。
您必须在其他地方保留单元格的状态,例如在二维数组中。
例如 boolean[][] 或 int[][]。
然后,您需要一种“渲染”方法,它获取单元格的值并将其绘制出来。

但我想提出一种更酷的方法来做到这一点。不要保留二维数组,而是使用 BufferedImage 组成的(一维)数组。
通常,每个“像素”都是该数组中的一个元素。然后使用drawImage 绘制该图像,并放大该图像。这可以表现得非常好。您也许可以实时绘制整个屏幕。

BufferedImage 有多种方法,一开始会有点混乱。最后你会在某个地方找到一个DataBuffer。您将需要访问 int[]。

然后,设置一个单元格:data[y * width + x] = -1; (白细胞)
清除单元格:data[y * width + x] = 0; (黑细胞)
(例如 - 或反之亦然 - 或任何其他颜色)。

你可以非常喜欢这个。您可以使用各种偏移变量,而不必一直计算 y*width+x,并且可以很好地优化它。
事实上,我尽可能让它变得如此高效,以至于你实际上可以超越另一个用 C++ 编写完全相同程序的人。

I'd like to propose a completely different solution. Normally have to treat the generated graphics as output-only, meaning, you don't want to read state from the graphics because that'd be too slow.
You'll have to keep the state of the cells elsewhere, like in a two-dimensional array.
boolean[][] or int[][] for example.
Then you'll need a "render" method of sorts, that takes the values of your cells, and draws it out.

But I'd like to propose an even cooler way of doing this. Instead of keeping a two dimensional array, use the (one dimensional) array that a BufferedImage is made up of.
Normally, each "pixel" is an element in that array. Then you use drawImage to draw that image, and scale that image up. This could perform really well. You might be able to have an entire screen draw in real time pretty much.

There are various methods on BufferedImage, it gets a little confusing at first. In the end you'll find a DataBuffer somewhere. You'll want access to the int[].

Then, to set a cell: data[y * width + x] = -1; (white cell)
to clear a cell: data[y * width + x] = 0; (black cell)
(for example - or vice versa - or any other color).

You can get really fancy with this. You could use various offset variables instead of having to calculate the y*width+x all the time, and optimize it really well.
In fact, I go as far as being able to make it so efficient, that you could actually outperform another guy making the exact same program in C++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文