[英]java get color of pixel for transparent, gradient overlay
In my application I have an image (world map) as background picture. 在我的应用程序中,我有一张图像(世界地图)作为背景图片。 Over this background picture, there is a polygon with a color gradient and transparent filling effect. 在此背景图片上,有一个具有颜色渐变和透明填充效果的多边形。
Here you find a code snippet of the overlay: 在这里,您可以找到叠加层的代码段:
public void paint(Graphics g) {
//draw a polygon with a gradient filling effect
Graphics2D g2 = (Graphics2D)g;
GradientPaint gp = new GradientPaint(x1, y1, color1, x2, y2, color2, false);
g2.setPaint(gp);
g2.fill(polygon);
}
Does somebody know a method to get the color of one pixel of the overlay ? 有人知道一种获取覆盖图一个像素颜色的方法吗? I don't need the color, which can be seen on the screen including the background picture - just the color of the overlay. 我不需要可以在屏幕上看到的颜色(包括背景图片),而只是覆盖层的颜色。
Best regards, 最好的祝福,
Michael 麦可
This is somewhat ugly, but works: 这有点难看,但是可以工作:
GradientPaint gp = new GradientPaint(0, 0, new Color(255, 0, 0, 50),
10, 10, new Color(128, 255, 0, 150));
ColorModel cm = ColorModel.getRGBdefault();
Rectangle r = new Rectangle(0, 0, 10, 10);
Raster raster = gp.createContext(cm, r, r, new AffineTransform(), null)
.getRaster(0, 0, 10, 10);
int[] rgba = raster.getPixel(5, 5, (int[])null);
Alternately, you could just paint the overlay into a BufferedImage (which you had first cleared to transparent). 或者,您可以将叠加层绘制到BufferedImage中(您首先将其清除为透明)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.