views:

55

answers:

1
+1  Q: 

Java: Composite

Hi,

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.

A: 

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

KarlP
Thanks for the comment. I could also implement a painter. The method getRaster(int x, int y, int w, int h) seems to fit perfectly. y should provide the value I am looking for to determine the height and colour.
Max