简体   繁体   English

如何在Java Applet中测试像素的颜色?

[英]How do I test for the color of a pixel in my Java Applet?

I have been doing some searching, and I have not found anything that suits my needs. 我一直在进行搜索,但没有找到适合自己需求的内容。 I have a small game applet I am writing in Java. 我有一个用Java编写的小游戏小程序。 I am curious if there is a way for me to obtain the color value (preferably RGB format) of a pixel on the Applet Viewer screen. 我很好奇我是否有办法在Applet Viewer屏幕上获得像素的颜色值(最好是RGB格式)。 I am planning on using this to detect collisions. 我正计划使用它来检测冲突。 Is there a way to get the color values of a pixel on the Java Applet screen? 有没有办法在Java Applet屏幕上获取像素的颜色值?

  1. Create a BufferedImage, with the width/height of your applet. 创建一个具有Applet宽度/高度的BufferedImage。
  2. Create a Graphics2D object by calling createGraphics() on your BufferedImage object. 通过在BufferedImage对象上调用createGraphics()创建一个Graphics2D对象。
  3. Pass this Graphics2D object to the paint method of your applet - now the applet will be painted on your BufferedImage. 将此Graphics2D对象传递给applet的paint方法-现在,applet将被绘制在您的BufferedImage上。
  4. Call dispose() on your Graphics2D object. 在Graphics2D对象上调用dispose()。
  5. Examine the pixel colors in your BufferedImage. 检查您的BufferedImage中的像素颜色。

BTW, Andrew Thompson is probably right when he says that this must be an ineffective way of calculating collisions. 顺便说一句,安德鲁·汤普森(Andrew Thompson)可能是正确的,他说这肯定是计算碰撞的无效方法。 Probably you should be looking into the intersects method of Rectangle. 可能您应该研究Rectangle的相交方法。 Or for best results use a game engine, they have support for collision detection. 为了获得最佳效果,请使用游戏引擎,它们支持碰撞检测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM