简体   繁体   English

确定Java.awt.Rectangle是否包含Java.awt.Color

[英]Determining if a Java.awt.Rectangle contains a Java.awt.Color

I was wondering how I would determine if a Java.awt.Rectangle contains a pixel that is a specific Java.awt.Color. 我想知道如何确定Java.awt.Rectangle是否包含特定Java.awt.Color的像素。 I've been searching all over but I cannot find any information on this or if it's even possible. 我一直在搜索,但是找不到任何信息,甚至可能。

So I was wondering how to determine if a Rectangle contains aa pixel with a specific Color or if a specific colored pixel is inside a defined Rectangle. 所以我想知道如何确定一个Rectangle是否包含具有特定颜色的像素,或者是否在定义的Rectangle中包含特定颜色的像素。

Example: The left image is a black rectangle. 示例:左侧图像是黑色矩形。 Lets say I have a defined Java.awt.Color that is red. 可以说我有一个红色定义的Java.awt.Color。 I want to determine if that rectangle contains a pixel that is that color. 我想确定该矩形是否包含该颜色的像素。 在此处输入图片说明

If you need to find out if some pixel on a Graphics was painted in this color, you can only discover it if the Graphics was created over a BufferedImage. 如果您需要确定图形上的某些像素是否以此颜色绘制,则只有在图形是在BufferedImage上创建的情况下才能发现它。 So you could run the same code that draws on screen, say call a Component's paint(Graphics) method, passing to it the result of BufferedImage.createGraphics() call. 因此,您可以运行在屏幕上绘制的相同代码,例如,调用组件的paint(Graphics)方法,并将BufferedImage.createGraphics()调用的结果传递给该方法。 Once painting is done, check pixel values in the range you need by calling BufferedImage.getRGB(x, y). 绘制完成后,通过调用BufferedImage.getRGB(x,y)检查所需范围内的像素值。

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

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