我正在实现一个显示容器级别的图表。根据填充水平,线条的颜色应该会发生变化(例如,接近最大值时它应该显示为红色)。我不想计算线条的不同部分并手动设置它们的颜色,而是定义一个颜色自动变化的波段。我想用自定义的 Composite/CompositeContext 来做到这一点,但我似乎无法计算出光栅返回的像素的位置。我的想法是检查它们的 y 值并在源中定义颜色值并且 y 值超过阈值时更改颜色。
我的 CompositeContext 看起来像这样:
CompositeContext context = new CompositeContext() {
@Override
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
int width = Math.min(src.getWidth(), dstIn.getWidth());
int height = Math.min(src.getHeight(), dstIn.getHeight());
int[] dstPixels = new int[width];
for (int y = 0; y < height; y++) {
dstIn.getDataElements(0, y, width, 1, dstPixels);
for (int x = 0; x < width; x++) {
if ( y ??? > 50) {
dstPixels[x] = 1;
} else {
// copy pixels from src
}
}
dstOut.setDataElements(0, y, width, 1, dstPixels);
}
}
"y" 似乎与某事有关,但它不包含绝对 y 值(实际上 compose 方法在 32x32 栅格中被多次调用)。也许有人知道如何检索组件上的位置,甚至知道如何更好地定义一个给定像素值被另一个值替换的区域。