什么是图块以及它们是如何在 BufferedImage 中创建的
我前段时间在 sun java 论坛上发布了一个问题,我发现很难理解我从回复者那里收到的第一个回复,尽管他似乎给了我解决问题的正确方法。问题的链接是:
http://forums.sun.com /thread.jspa?threadID=5436562&tstart=0
有人回复说我应该使用 BufferedImage
并制作图块。我不太明白这些图块与 BufferedImage 相关的含义。
我希望有人向我解释这些图块是什么以及它们是如何在 BufferedImage
中创建的。
我在网上搜索了一段时间,但找不到任何可以帮助我了解图块基础知识和创建图块的链接。任何指向网站的指针也将受到赞赏。
我需要帮助来理解与 BufferedImage 相关的图块以及它们的创建方式。
I posted a question in sun java forums sometime ago and i am finding it hard to understand the first response i received from the replier though it seems he gave me the correct approach to my problem. The link to the question is:
http://forums.sun.com/thread.jspa?threadID=5436562&tstart=0
Someone replied that i should use BufferedImage
and make tiles. I don't really understand what the tiles mean in connection with the BufferedImage
.
I would like someone to explain to me what the tiles are and how they are created in the BufferedImage
.
I have searched the web for a while but couldn't find any link that can help me understanding the basics of the tiles and creating the tiles. Any pointer to a site is also appreciated.
I need help in understanding the tiles in connection with the BufferedImage
and also how they are created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
2D 游戏中的“图块”仅表示“小于整个屏幕的图像,您可以多次重复使用它来创建背景”。
这是一个工作示例,其中创建了四个图块(向每个像素添加一些随机噪声)。每个图块为 50x50 像素。
然后有一个“地图”(在您的情况下称为“网格”)代表您想要放置哪些图块。
从该映射中,创建了一个更大的 BufferedImage(请注意,这只是一个示例,在真实的程序中,您需要使用 BufferedImage 副本,而不是逐像素副本)。
地图为 9x7,每个图块为 50x50 像素,因此生成的图像为 9*50 x 7*50(即 450 x 350)。
请注意,以下实际上只是一个尽可能简短的简单示例,展示了如何使用多个图块创建更大的 BufferedImage:目标不是提供有关 Swing 最佳用法或如何使用的教程。充分利用 BufferedImages 等的每一点性能。
A "tile" in a 2D game simply means an "image smaller than whole screen that you can reuse several times to create the background".
Here's a a working example where four tiles are created (adding some random noise to every pixel). Each tile is 50x50 pixels.
Then there's a "map" (that you call a "grid" in your case) representing which tiles you want to put where.
From that map, a bigger
BufferedImage
is created (note that it's just an example, in a real program you'll want to use a BufferedImage copy, not a pixel-by-pixel copy).The map is 9x7, each tile is 50x50 pixels, hence the resulting image is 9*50 x 7*50 (ie 450 by 350).
Note that the following is really just a simple example, as short as possible, showing how to create a bigger BufferedImage using several tiles: the goal is not to give a tutorial on best Swing usage nor on how to squeeze every bit of performances out of BufferedImages, etc.
如果您想旋转 BufferedImage 的一部分,您可能会发现这些类/方法很有用:
AffineTransform.getRotateInstance
或AffineTransform.getQuadrantRotateInstance
BufferedImage.getSubImage()
BufferedImage.setData
示例:
If you want to rotate a portion of a
BufferedImage
you might find these classes/methods useful:AffineTransform.getRotateInstance
orAffineTransform.getQuadrantRotateInstance
AffineTransformOp
BufferedImage.getSubImage()
BufferedImage.setData
Example: