Java:复合

发布于 2024-09-18 12:15:22 字数 1092 浏览 2 评论 0原文

我正在实现一个显示容器级别的图表。根据填充水平,线条的颜色应该改变(例如,接近最大值时它应该显示红色)。我不想计算线条的不同段并手动设置它们的颜色,而是想定义一个颜色自动更改的带。我想用自定义 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 值(实际上,使用 32x32 栅格多次调用 compose 方法)。也许有人知道如何检索组件上的位置,甚至知道如何更好地定义将给定像素值替换为另一个值的区域。

I'm implementing a diagram that shows the level of a container. Depending on the fill level, the colour of the line should change (for instance, close to the maximum it should show red). Rather than calculating different segments of the line and setting their colours manually, I'd like to define a band in which the colour automatically changes. I thought to do this with a custom Composite/CompositeContext, but I seem not to be able to work out the locations of the pixels returned by the raster. My idea is to check for their y-Values and change the colour if a colour value is defined in the source and if the y-Value exceeds a threshold value.

My CompositeContext looks like this:

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" seems to be related to something, but it does not contain the absolute y-Value (in fact the compose method is called several times with 32x32 rasters). Maybe someone knows how to retrieve the position on the component or even a better way to define an area in which a given pixel value is replaced by another value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万水千山粽是情ミ 2024-09-25 12:15:22

不能先用 0 alpha 填充渐变,然后用全 alpha 绘制线条吗?

Can't you just fill with a gradient with 0 alpha and then draw the line with full alpha?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文