Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8491805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:32:20+00:00 2026-06-10T22:32:20+00:00

I have a shell which has a background image. Within this shell, I have

  • 0

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.

  1. Rectangle rect = sc.getClientArea(); // this rect always returns width and height as 0.
  2. 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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:32:22+00:00Added an answer on June 10, 2026 at 10:32 pm

    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 into SWT.Resize event.

    Anyway check the below code:

    Output

    enter image description here

    Code !!

    Note You have to provide image.png

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.custom.ScrolledComposite;
    import org.eclipse.swt.graphics.Color;
    import org.eclipse.swt.graphics.GC;
    import org.eclipse.swt.graphics.Image;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.layout.FormLayout;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Event;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Listener;
    import org.eclipse.swt.widgets.Shell;
    
    public class ImageGradientTest {
    
        private Image imageGradient;
        private Image image;
    
        private void createComponents(Shell parent) 
        {
            ScrolledComposite 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));
    
    
            final Composite composite = new Composite(sc, SWT.NONE);
            composite.addListener(SWT.Resize, new Listener() {
                public void handleEvent(Event e) {
                    changeImage(composite);
                }
            });
            composite.setLayout(new FormLayout());
            composite.setBackgroundMode(SWT.INHERIT_DEFAULT);
    
            Label label = new Label(composite, SWT.None);
            label.setText("Hello, World!");
    
            sc.setContent(composite);
            sc.layout();
    
        }
    
        private void changeImage(Composite composite) 
        {
            Image oldImage = imageGradient;
    
            Display display = composite.getDisplay();
            Rectangle rect = composite.getClientArea();
            imageGradient = new Image(display, image, SWT.IMAGE_COPY);
    
    
            GC gc = new GC(imageGradient);
            Color color1 = new Color(display, 200, 200, 255);
            Color color2 = new Color(display, 255, 255, 255);
    
            gc.setAlpha(245);           // Two have a layer effect you should set the alpha
            gc.setForeground(color1);
            gc.setBackground(color2);
            gc.fillGradientRectangle(rect.x, rect.y, rect.width, rect.height, true);
    
            color2.dispose();
            color1.dispose();
            gc.dispose();
    
    
            composite.setBackgroundImage(imageGradient);
    
            if (oldImage != null) {
                oldImage.dispose();
            }
        }
    
        private void openShell() 
        {
            Display display = new Display();
    
            Shell shell = new Shell(display);
            shell.setLayout(new FillLayout());
    
            image = new Image(display, "image.png");
    
            shell.setBackgroundImage(image);
            shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
    
            createComponents(shell);
    
            shell.open();
    
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
    
            image.dispose();
            imageGradient.dispose();        
            display.dispose();
        }
    
        public static void main(String[] args) {
            ImageGradientTest sweet = new ImageGradientTest();
            sweet.openShell();
        }
    }
    

    Code for capturing client area image !!

    Display display = composite.getDisplay();
    Rectangle rect = composite.getClientArea();
    Image i = new Image(display, composite.getClientArea().width, composite.getClientArea().height);
    GC gc = new GC(i);
    
    composite.print(gc);            // This is important
    
    ImageLoader loader = new ImageLoader();
    loader.data = new ImageData[]{i.getImageData()};
    loader.save("hello.png", SWT.IMAGE_PNG);
    
    gc.dispose();
    i.dispose();
    

    Although check this link as the approach is not cross-platform.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this site: http://www.problemio.com which currently has a solid-colored background and the body
I have a xml which has several numbers, something like this: <xml> <case1> <amount>3.140000</amount>
I have the below shell script in which sql file is called which has
I have an issue that has got me pretty stumped, and would appreciate some
I have looked at this question which has been suggested as a duplicate: Make
I have a shell script which I'd like to trigger from a J2EE web
I have a shell script which has a for loop that prints the value
I have a shell script which runs a python program which has its own
While in a Linux shell I have a string which has the following contents:
I have a CodeIgniter app which has a controller to invoke Capistrano. Like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.