foma
foma

Reputation: 457

Placing image buttons on the image background

I am trying to put buttons with images (GIF) on the background which has already been set as an image (shell.setBackgroundImage(image)) and I can't figure out how to remove transparent border around buttons with images. I would be grateful if somebody could give me some tip about this issue.

Here is my code:

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;

public class Main_page {
        public static void main(String[] args) {
                Display display = new Display();
                Shell shell = new Shell(display);
                Image image = new Image(display, "bg.gif");
                shell.setBackgroundImage(image);
                shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
                shell.setFullScreen(true);

                Button button = new Button(shell, SWT.PUSH);
                button.setImage(new Image(display, "button.gif"));

                RowLayout Layout = new RowLayout();
                shell.setLayout(Layout);

                shell.open();
                while (!shell.isDisposed()) {
                        if (!display.readAndDispatch())
                            display.sleep();      
                }
                display.dispose();
        }
}

Sorceror, thanks for your answer I will definitely look into this article. Maybe I will find my way. So far I have improved my code a little bit. Firstly, I managed to get rid of the gray background noise. Secondly, I finally succeeded in creating the button as I had seen it in the first place. Yet, another obstacle has arisen. When I removed image(button) transparent border it turned out that the button change its mode(from push button to check box). The problem is that I came so close to the thing I was looking for and now I am a little puzzled. If you have some time please give a glance at my code.

Here is the code, if you launch it you will see what the problem is(hope you didn't have problems downloading images):

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;

public class Main_page {
        public static void main(String[] args) {
                Display display = new Display();
                Shell shell = new Shell(display);
                Image image = new Image(display, "bg.gif"); // Launch on a screen 1280x1024
                shell.setBackgroundImage(image);
                shell.setBackgroundMode(SWT.TRANSPARENT);
                shell.setFullScreen(true);


                GridLayout gridLayout = new GridLayout();
                gridLayout.marginTop = 200; 
                gridLayout.marginLeft = 20;
                shell.setLayout(gridLayout);



                // If you replace SWT.PUSH with SWT.COLOR_TITLE_INACTIVE_BACKGROUND
                // you will see what I am looking for, despite nasty check box  

                Button button = new Button(shell, SWT.PUSH); 
                button.setImage(new Image(display, "moneyfast.gif"));



                shell.open();
                while (!shell.isDisposed()) {
                        if (!display.readAndDispatch())
                            display.sleep();      
                }
                display.dispose();
        }

Upvotes: 3

Views: 8236

Answers (3)

Viktor Sirotin
Viktor Sirotin

Reputation: 313

The style SWT.TRANSPARENT helps:

    Button button = new Button(buttonParent,  SWT.TRANSPARENT);
    InputStream is = getClass().getResourceAsStream("my_image.png");
    ImageData imageData = new ImageData(is).scaledTo(16, 16);
    Image image = new Image (button.getDisplay(), imageData);
    button.setImage(image);

Upvotes: 0

user847988
user847988

Reputation: 984

You can also be sure that no background will appear in any platform by creating a label with a picture which acts as a button and a mouse listener for the label.

Upvotes: 1

Sorceror
Sorceror

Reputation: 4843

The only way how to get rid of the space around button is to set background Color or background Image.

button.setBackgroundImage(image);

But, if the button will not be in the left top corner (as it is for now), the background image will not be correctly positioned. So first you'll have to position the button, then make image of appropriate part of background and then set it to button as background image.

Check article Taking a look at SWT Images - Transparency for additional details of transparency in Label (and Button as well).

Upvotes: 3

Related Questions