基本上,我想要做的是为一个JComponent做一个光标,它的像素显示为它们结束的颜色的反义词。例如,将鼠标悬停在此页面的url中的字母上。如果仔细观察,光标在字母的黑色像素上方的像素会变成白色。我知道您可以通过从每个对应字段的255中减去当前的红色、绿色和蓝色来倒置RGB颜色,但我不知道如何以我想要的方式实现这一点。
这是我正在制作的一个基本油漆程序的一部分。我前面提到的JComponent是我的“画布”,您可以使用各种工具使用它。我是,而不是,使用java.awt.Cursor来更改游标,因为我希望游标根据光标下面的内容动态变化。我使用的“游标”被定义为一个.png映像,我正在从这个文件中创建一个BufferedImage,然后我可以在整个组件的现有BufferedImage之上绘制该文件。我用一个MouseListener定义的点重新绘制这个图像。
我查看了AlphaComposite,它看起来很接近我想要的,但没有任何关于倒置光标下的颜色我想要。请帮帮忙。
编辑:
所以我不得不用一种算法来做这件事,因为没有为此目的内置的任何东西。这里的代码有点脱离上下文:
/**
* Changes the color of each pixel under the cursor shape in the image
* to the complimentary color of that pixel.
*
* @param points an array of points relative to the cursor image that
* represents each black pixel of the cursor image
* @param cP the point relative to the cursor image that is used
* as the hotSpot of the cursor
*/
public void drawCursorByPixel(ArrayList<Point> points, Point cP) {
Point mL = handler.getMouseLocation();
if (mL != null) {
for (Point p : points) {
int x = mL.x + p.x - cP.x;
int y = mL.y + p.y - cP.y;
if (x >= 0 && x < image.getWidth() && y >= 0 && y < image.getHeight()) {
image.setRGB(x, y, getCompliment(image.getRGB(x, y)));
}
}
}
}
public int getCompliment(int c) {
Color col = new Color(c);
int newR = 255 - col.getRed();
int newG = 255 - col.getGreen();
int newB = 255 - col.getBlue();
return new Color(newR, newG, newB).getRGB();
}发布于 2014-05-31 07:17:27
我相信你要找的是一个图像过滤器。听起来你甚至已经拥有了所有的东西。您的过滤器将是光标的图像,它将被绘制在所有其他内容之上。诀窍是,正如您所说的那样,绘制光标的每个像素,从而使所述像素的颜色是光标后面绘制的空间中像素颜色的计算“相反”。
我不知道最好的方法去做这件事,但我知道有一种方式,你可能可以改进。把你的背景画到一个缓冲的图像上,然后用BufferedImage的颜色模型得到光标将悬停在上面的像素的颜色。这个例子是我从另一个问题中找到的here。
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2 = image.createGraphics();
_mainPanel.paint(g2);
image.getColorModel().getRGB(pixel);
g2.dispose();最终,您将使用背景的缓冲图像获取光标重叠的像素(及其颜色),然后您可以在颜色上运行一些算法,将它们在光标中反转,然后用新的颜色重新绘制光标。
This question为该算法提供了几种解决方案,尽管我没有亲自尝试过它们的效果。
https://stackoverflow.com/questions/22290630
复制相似问题