SWT Java - Putting composite with image background on a shell with image background

10,003

These are not gray borders (you removed them by SWT.INHERIT_FORCE), these are transparent corners of the image that are not transparent because there is a composite behind them. As far as I know you cannot remove the corners of the image using it as a background of a Composite. If these corners are transparent (and not white), you can draw this image on the Shell (without creating any Composite) using GC as shown here.
When placing transparent images, put them on the object you expect to see behind. Hope I helped u. :)

Share:
10,003
foma
Author by

foma

Updated on June 14, 2022

Comments

  • foma
    foma almost 2 years

    I am writing a graphical application using SWT. Firstly I created a shell with image background (shell.setBackgroundImage(image)) then I put a composite with another background image, so that I could use it(composite) to place labels inside it. The problem is that I cannot get rid of the gray border around composite. Is it possible to do at all or perhaps there are others way to do that?

    Here is the code:

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.graphics.Image;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;
    
    public class Test {
    
        protected static int gap = 20;
    
        public static void main (String[] args) {
    
           Display display = new Display();
           Shell shell = new Shell(display);   
           Image bg_Image = new Image(display, "bg_full.png"); 
           shell.setBackgroundImage(bg_Image);
           shell.setBackgroundMode(SWT.INHERIT_FORCE);  
           shell.setFullScreen(true);
    
           Image bg = new Image(display, "bg.png");
           Image border = new Image(display, "bg_info.png");
           Image b1 = new Image(display, "button_total_active.png");
           Image b2 = new Image(display, "button_terminal.png");
           Image b3 = new Image(display, "button_operator.png");
           Image b4 = new Image(display, "button_contract.png");
    
           Composite composite = new Composite(shell, SWT.NONE);
           composite.setBackgroundImage(bg);
           composite.setBackgroundMode(SWT.INHERIT_FORCE);
           composite.setBounds(80, 220, bg.getBounds().width, bg.getBounds().height);
    
           Label label1 = new Label(composite, SWT.NONE);
           label1.setImage(b1);
           label1.setBounds(gap, gap, b1.getBounds().width, b1.getBounds().height);
    
           Label label2 = new Label(composite, SWT.NONE);
           label2.setImage(b2);
           label2.setBounds(gap, gap*2+b1.getBounds().height, b2.getBounds().width, b2.getBounds().height);
    
           Label label3 = new Label(composite, SWT.NONE);
           label3.setImage(b3);
           label3.setBounds(gap, gap*3+b1.getBounds().height*2, b3.getBounds().width, b3.getBounds().height);
    
           Label label4 = new Label(composite, SWT.NONE);
           label4.setImage(b4);
           label4.setBounds(gap, gap*4+b1.getBounds().height*3, b4.getBounds().width, b4.getBounds().height);
    
           Label label5 = new Label(composite, SWT.NONE);
           label5.setImage(border);
           label5.setBounds(gap*2+b1.getBounds().width, gap, border.getBounds().width, border.getBounds().height);
    
           shell.open();
           while (!shell.isDisposed()) {
               if (!display.readAndDispatch())
                    display.sleep();      
           }
           display.dispose();
    
       }
    }
    

    Here is a sample image

  • foma
    foma over 12 years
    Gene Marin thanks for answering my question, yes I know that these borders are transparent (maybe I didn't make that clear) and I also familiar with drawing using GC) The problem is that I don't how to handle this obstacle: I create shell(with image background), then I can draw Image on it, then I need to put more labels(with images) above this Image, but the transparent parts of these images inherit shell's background, I want to overcome this, maybe you have any ideas? Hope this link(file.karelia.ru/5rrvrv) will clarify my question. Thanks in advance
  • foma
    foma over 12 years
    Never mind I used Swing for handling this obstacle it works perfect. Thanks anyway