Java 循环遍历图像中的像素?
我正在尝试找到一种为我的 2D java 游戏制作地图的方法,我想到了一个想法,其中我将循环遍历图像的每个像素,并取决于要绘制的图块的像素是什么颜色。
例如
是否可以循环遍历图像像素?如果是的话,怎么样?
您能给我提供一些有用的链接或代码片段吗?
I'm trying to find a way to make maps for my 2D java game, and I thought of one Idea in which I would loop through each pixel of an image and depends on what color the pixel it was that would be the tile to draw.
e.g.
Is it possible to loop through an Images pixels? If it is, how?
Could you please supply me with some helpful links or code snippets?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,如果您想循环遍历图像中的所有像素,请确保在 y 坐标上进行外部循环,如下所示:
这可能会使您的代码更快,因为您将访问将其存储在内存中的顺序。 (作为像素行。)
Note that if you want to loop over all pixels in an image, make sure to make the outer loop over the y-coordinate, like so:
This will likely make your code much faster, as you'll be accessing the image data in the order it's stored in memory. (As rows of pixels.)
我认为 Pixelgrabber 就是您所寻找的。如果您对代码有疑问,请写评论。以下是 javadoc 的链接:[Pixelgrabber][1] 和另一个简短示例:[获取特定像素的颜色][2],获取像素颜色的Java程序
以下示例来自上一个链接。感谢roseindia.net
[1]:https ://docs.oracle.com/javase/7/docs/api/java/awt/image/PixelGrabber.html [2]:http://www.rgagnon.com/javadetails/java-0257.html
I think Pixelgrabber is what you looking for. If you have problems with the code please write a comment. Here is the link to the javadoc: [Pixelgrabber][1] and another short examples: [Get the color of a specific pixel][2], Java program to get the color of pixel
The following example is from the last link. Thanks to roseindia.net
[1]: https://docs.oracle.com/javase/7/docs/api/java/awt/image/PixelGrabber.html [2]: http://www.rgagnon.com/javadetails/java-0257.html