I have a shell which has a background image.
Within this shell, I have a scrolled composite. This scrolled composite inherits the background image of the shell.
I would like to perform some sort of alpha blending / gradient on the inherited background image of the scrolled composite.
I follow the codes from here.
Rectangle rect = sc.getClientArea();// this rect always returns width and height as 0.Image newImage = new Image(sc.getDisplay(), Math.max(1, rect.width), rect.height);// doesn’t seems to grab the background image, even if I hardcoded the width and height.
Some codes here:
sc = new ScrolledComposite(parent, SWT.BORDER_SOLID | SWT.V_SCROLL);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setAlwaysShowScrollBars(true);
sc.setMinWidth(0);
GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);
sc.setLayoutData(gd);
sc.setLayout(new GridLayout(1, false));
Rectangle rect = sc.getClientArea();
Image newImage = new Image(sc.getDisplay(), sc.getParent().getBackgroundImage(), SWT.IMAGE_COPY);
GC gc = new GC(newImage);
// need to do clipping on the image so that only the background underlying the scrolled composite gets the gradient
// fill background
gc.setForeground(new Color(sc.getDisplay(), 100,200,123));
gc.setBackground(new Color(sc.getDisplay(), 23,23,23));
gc.fillGradientRectangle(rect.x, rect.y, rect.width, rect.height, true);
gc.dispose();
sc.setBackgroundImage(newImage);
sc.setBackgroundMode(SWT.INHERIT_DEFAULT);
innerComposite = new Composite(sc, SWT.NONE);
GridLayout gl = new GridLayout();
gl.numColumns = 1;
innerComposite.setLayout(gl);
gd = new GridData(GridData.FILL_BOTH);
gd.grabExcessVerticalSpace = true;
innerComposite.setLayoutData(gd);
/**
* Creates other widgets here!
*/
sc.setContent(innerComposite);
sc.layout();
One logical problem that I can see here is that you are doing all the gradient stuff too early. Even if you do something like this
sc = new ScrolledComposite(parent, SWT.BORDER_SOLID | SWT.V_SCROLL);it does not mean the shell is created and ready or the window/composite itself. A very convenient handle is to hook intoSWT.Resizeevent.Anyway check the below code:
Output
Code !!
Note You have to provide
image.pngCode for capturing client area image !!
Although check this link as the approach is not cross-platform.