Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-swt-dev] GC on native components and painting


The following snippet is the best I can do.
Hope it helps.
Carolyn

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

/**
 * Buttons on Windows don't like transparent images.
 * So you have to simulate transparency by setting the image's
 * transparent pixel to the button's background color.
 */
public class Main {
       
public static void main (String [] args) {
        Display display = new Display();
       
        /* Create a shell with a button. */
        Shell shell = new Shell (display);
        shell.setLayout (new FillLayout ());
        Button button = new Button (shell, SWT.PUSH | SWT.BORDER);

        /* Create an image data with a 2-color palette (red, white). */
        PaletteData palette = new PaletteData (new RGB[] {
                display.getSystemColor (SWT.COLOR_RED).getRGB (),   // pixel 0 = red
                display.getSystemColor (SWT.COLOR_WHITE).getRGB (), // pixel 1 = white
        });
        ImageData imageData = new ImageData (24, 24, 1, palette);
        imageData.transparentPixel = 1; // set the transparent color to white
       
        /* Create an image from the image data, and draw on it. */
        Image image = new Image (display, imageData);
        GC gc = new GC (image);
        gc.setBackground (display.getSystemColor (SWT.COLOR_WHITE));
        gc.fillRectangle (0, 0, 24, 24); // fill with white
        gc.setBackground (display.getSystemColor (SWT.COLOR_RED));
        gc.fillArc (2, 2, 20, 20, 0, 360); // draw a red circle in the middle
        gc.dispose ();
       
        /* Tell the image to use the button's background color for transparent pixels. */
        image.setBackground (button.getBackground ());
        button.setImage (image);
       
        shell.pack ();
        shell.open ();
        while (!shell.isDisposed ()) {
                if (!display.readAndDispatch ()) display.sleep ();
        }
        image.dispose ();
        display.dispose ();
}
}





Minh Chiem <minhchiem@xxxxxxxxxxxx>
Sent by: platform-swt-dev-admin@xxxxxxxxxxx

11/26/2003 03:13 AM

Please respond to
platform-swt-dev

To
platform-swt-dev@xxxxxxxxxxx
cc
Subject
[platform-swt-dev] GC on native components and painting





Hi.

I've followed the guide on
http://www.eclipse.org/articles/Article-SWT-images/graphics-resources.html#Transparency
to find out how to do transparency.

I've added a paintlistener on a button however, when you go to click on
the button there seems to be no refresh so it appears as if a image as
disappeared.

It looks like there's another paint listener when the button is pressed
so what I've done is made the button repaint everytime the mouse down
and mouse up event occurs and that seems to be a nice work around.
However, I've got lots of buttons/toolbar items that have images and
adding mouse listeners to all the buttons makes code look really messy
IMHO. Since I can't just extend the Button so that it encapsulates the
extra repainting behaviour, is another way to approach this problem?

BTW, I'm running this on the windows version of SWT.

_______________________________________________
platform-swt-dev mailing list
platform-swt-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/platform-swt-dev


Back to the top