Example usage for org.eclipse.swt.graphics Color dispose

List of usage examples for org.eclipse.swt.graphics Color dispose

Introduction

In this page you can find the example usage for org.eclipse.swt.graphics Color dispose.

Prototype

public void dispose() 

Source Link

Usage

From source file:org.eclipse.swt.snippets.Snippet141.java

public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setText("Snippet 141");
    shell.setSize(300, 300);/*from   w  w  w.  j a  v  a 2 s .  c  om*/
    shell.open();
    shellGC = new GC(shell);
    shellBackground = shell.getBackground();

    FileDialog dialog = new FileDialog(shell);
    dialog.setFilterExtensions(new String[] { "*.gif" });
    String fileName = dialog.open();
    final AtomicBoolean stopAnimation = new AtomicBoolean(false);
    if (fileName != null) {
        loader = new ImageLoader();
        try {
            imageDataArray = loader.load(fileName);
            if (imageDataArray.length > 1) {
                animateThread = new Thread("Animation") {
                    @Override
                    public void run() {
                        /* Create an off-screen image to draw on, and fill it with the shell background. */
                        Image offScreenImage = new Image(display, loader.logicalScreenWidth,
                                loader.logicalScreenHeight);
                        GC offScreenImageGC = new GC(offScreenImage);
                        offScreenImageGC.setBackground(shellBackground);
                        offScreenImageGC.fillRectangle(0, 0, loader.logicalScreenWidth,
                                loader.logicalScreenHeight);

                        try {
                            /* Create the first image and draw it on the off-screen image. */
                            int imageDataIndex = 0;
                            ImageData imageData = imageDataArray[imageDataIndex];
                            if (image != null && !image.isDisposed())
                                image.dispose();
                            image = new Image(display, imageData);
                            offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height,
                                    imageData.x, imageData.y, imageData.width, imageData.height);

                            /* Now loop through the images, creating and drawing each one
                             * on the off-screen image before drawing it on the shell. */
                            int repeatCount = loader.repeatCount;
                            while ((loader.repeatCount == 0 || repeatCount > 0) && !stopAnimation.get()) {
                                switch (imageData.disposalMethod) {
                                case SWT.DM_FILL_BACKGROUND:
                                    /* Fill with the background color before drawing. */
                                    Color bgColor = null;
                                    if (useGIFBackground && loader.backgroundPixel != -1) {
                                        bgColor = new Color(display,
                                                imageData.palette.getRGB(loader.backgroundPixel));
                                    }
                                    offScreenImageGC.setBackground(bgColor != null ? bgColor : shellBackground);
                                    offScreenImageGC.fillRectangle(imageData.x, imageData.y, imageData.width,
                                            imageData.height);
                                    if (bgColor != null)
                                        bgColor.dispose();
                                    break;
                                case SWT.DM_FILL_PREVIOUS:
                                    /* Restore the previous image before drawing. */
                                    offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height,
                                            imageData.x, imageData.y, imageData.width, imageData.height);
                                    break;
                                }

                                imageDataIndex = (imageDataIndex + 1) % imageDataArray.length;
                                imageData = imageDataArray[imageDataIndex];
                                image.dispose();
                                image = new Image(display, imageData);
                                offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height,
                                        imageData.x, imageData.y, imageData.width, imageData.height);

                                /* Draw the off-screen image to the shell. */
                                shellGC.drawImage(offScreenImage, 0, 0);

                                /* Sleep for the specified delay time (adding commonly-used slow-down fudge factors). */
                                try {
                                    int ms = imageData.delayTime * 10;
                                    if (ms < 20)
                                        ms += 30;
                                    if (ms < 30)
                                        ms += 10;
                                    Thread.sleep(ms);
                                } catch (InterruptedException e) {
                                }

                                /* If we have just drawn the last image, decrement the repeat count and start again. */
                                if (imageDataIndex == imageDataArray.length - 1)
                                    repeatCount--;
                            }
                        } catch (SWTException ex) {
                            System.out.println("There was an error animating the GIF");
                        } finally {
                            if (offScreenImage != null && !offScreenImage.isDisposed())
                                offScreenImage.dispose();
                            if (offScreenImageGC != null && !offScreenImageGC.isDisposed())
                                offScreenImageGC.dispose();
                            if (image != null && !image.isDisposed())
                                image.dispose();
                        }
                    }
                };
                animateThread.setDaemon(true);
                animateThread.start();
            }
        } catch (SWTException ex) {
            System.out.println("There was an error loading the GIF");
        }
    }

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    stopAnimation.set(true);
    display.dispose();
}

From source file:AnimatedGIFDisplay.java

public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setSize(300, 300);/*from   w ww  .  ja  v a2 s .  c o m*/
    shell.open();
    shellGC = new GC(shell);
    shellBackground = shell.getBackground();

    FileDialog dialog = new FileDialog(shell);
    dialog.setFilterExtensions(new String[] { "*.gif" });
    String fileName = dialog.open();
    if (fileName != null) {
        loader = new ImageLoader();
        try {
            imageDataArray = loader.load(fileName);
            if (imageDataArray.length > 1) {
                animateThread = new Thread("Animation") {
                    public void run() {
                        /*
                         * Create an off-screen image to draw on, and fill it with the
                         * shell background.
                         */
                        Image offScreenImage = new Image(display, loader.logicalScreenWidth,
                                loader.logicalScreenHeight);
                        GC offScreenImageGC = new GC(offScreenImage);
                        offScreenImageGC.setBackground(shellBackground);
                        offScreenImageGC.fillRectangle(0, 0, loader.logicalScreenWidth,
                                loader.logicalScreenHeight);

                        try {
                            /* Create the first image and draw it on the off-screen image. */
                            int imageDataIndex = 0;
                            ImageData imageData = imageDataArray[imageDataIndex];
                            if (image != null && !image.isDisposed())
                                image.dispose();
                            image = new Image(display, imageData);
                            offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height,
                                    imageData.x, imageData.y, imageData.width, imageData.height);

                            /*
                             * Now loop through the images, creating and drawing each one on
                             * the off-screen image before drawing it on the shell.
                             */
                            int repeatCount = loader.repeatCount;
                            while (loader.repeatCount == 0 || repeatCount > 0) {
                                switch (imageData.disposalMethod) {
                                case SWT.DM_FILL_BACKGROUND:
                                    /* Fill with the background color before drawing. */
                                    Color bgColor = null;
                                    if (useGIFBackground && loader.backgroundPixel != -1) {
                                        bgColor = new Color(display,
                                                imageData.palette.getRGB(loader.backgroundPixel));
                                    }
                                    offScreenImageGC.setBackground(bgColor != null ? bgColor : shellBackground);
                                    offScreenImageGC.fillRectangle(imageData.x, imageData.y, imageData.width,
                                            imageData.height);
                                    if (bgColor != null)
                                        bgColor.dispose();
                                    break;
                                case SWT.DM_FILL_PREVIOUS:
                                    /* Restore the previous image before drawing. */
                                    offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height,
                                            imageData.x, imageData.y, imageData.width, imageData.height);
                                    break;
                                }

                                imageDataIndex = (imageDataIndex + 1) % imageDataArray.length;
                                imageData = imageDataArray[imageDataIndex];
                                image.dispose();
                                image = new Image(display, imageData);
                                offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height,
                                        imageData.x, imageData.y, imageData.width, imageData.height);

                                /* Draw the off-screen image to the shell. */
                                shellGC.drawImage(offScreenImage, 0, 0);

                                /*
                                 * Sleep for the specified delay time (adding commonly-used
                                 * slow-down fudge factors).
                                 */
                                try {
                                    int ms = imageData.delayTime * 10;
                                    if (ms < 20)
                                        ms += 30;
                                    if (ms < 30)
                                        ms += 10;
                                    Thread.sleep(ms);
                                } catch (InterruptedException e) {
                                }

                                /*
                                 * If we have just drawn the last image, decrement the repeat
                                 * count and start again.
                                 */
                                if (imageDataIndex == imageDataArray.length - 1)
                                    repeatCount--;
                            }
                        } catch (SWTException ex) {
                            System.out.println("There was an error animating the GIF");
                        } finally {
                            if (offScreenImage != null && !offScreenImage.isDisposed())
                                offScreenImage.dispose();
                            if (offScreenImageGC != null && !offScreenImageGC.isDisposed())
                                offScreenImageGC.dispose();
                            if (image != null && !image.isDisposed())
                                image.dispose();
                        }
                    }
                };
                animateThread.setDaemon(true);
                animateThread.start();
            }
        } catch (SWTException ex) {
            System.out.println("There was an error loading the GIF");
        }
    }

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

From source file:cookxml.cookswt.util.SwtUtils.java

/**
 * Dispose a color if it is not a system color.
 *
 * @param   color The color to be disposed.
 * @param   device The device that contains the color.
 *//*  ww  w.  j a  v a  2s  .  co m*/
public static void disposeColor(Color color, Device device) {
    if (color.isDisposed())
        return;
    if (!isSystemColor(color, device)) {
        assert disposeDebug("Dispose color: " + color);
        color.dispose();
    }
}

From source file:org.eclipse.swt.snippets.Snippet288.java

private static void loadAllImages(String directory, String[] filenames) throws SWTException {
    int numItems = filenames.length;
    loader = new ImageLoader[numItems];
    imageDataArray = new ImageData[numItems][];
    image = new Image[numItems][];
    for (int i = 0; i < numItems; i++) {
        loader[i] = new ImageLoader();
        int fullWidth = loader[i].logicalScreenWidth;
        int fullHeight = loader[i].logicalScreenHeight;
        imageDataArray[i] = loader[i].load(directory + File.separator + filenames[i]);
        int numFramesOfAnimation = imageDataArray[i].length;
        image[i] = new Image[numFramesOfAnimation];
        for (int j = 0; j < numFramesOfAnimation; j++) {
            if (j == 0) {
                //for the first frame of animation, just draw the first frame
                image[i][j] = new Image(display, imageDataArray[i][j]);
                fullWidth = imageDataArray[i][j].width;
                fullHeight = imageDataArray[i][j].height;
            } else {
                //after the first frame of animation, draw the background or previous frame first, then the new image data
                image[i][j] = new Image(display, fullWidth, fullHeight);
                GC gc = new GC(image[i][j]);
                gc.setBackground(shellBackground);
                gc.fillRectangle(0, 0, fullWidth, fullHeight);
                ImageData imageData = imageDataArray[i][j];
                switch (imageData.disposalMethod) {
                case SWT.DM_FILL_BACKGROUND:
                    /* Fill with the background color before drawing. */
                    Color bgColor = null;
                    if (useGIFBackground && loader[i].backgroundPixel != -1) {
                        bgColor = new Color(display, imageData.palette.getRGB(loader[i].backgroundPixel));
                    }/*from  w w  w . j a  v  a2s  .  co m*/
                    gc.setBackground(bgColor != null ? bgColor : shellBackground);
                    gc.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height);
                    if (bgColor != null)
                        bgColor.dispose();
                    break;
                default:
                    /* Restore the previous image before drawing. */
                    gc.drawImage(image[i][j - 1], 0, 0, fullWidth, fullHeight, 0, 0, fullWidth, fullHeight);
                    break;
                }
                Image newFrame = new Image(display, imageData);
                gc.drawImage(newFrame, 0, 0, imageData.width, imageData.height, imageData.x, imageData.y,
                        imageData.width, imageData.height);
                newFrame.dispose();
                gc.dispose();
            }
        }
    }
}

From source file:org.eclipse.swt.examples.controlexample.LinkTab.java

@Override
void resetColorsAndFonts() {
    super.resetColorsAndFonts();
    Color oldColor = linkForegroundColor;
    linkForegroundColor = null;/*w  ww . j a va  2s  .  com*/
    setLinkForeground();
    if (oldColor != null)
        oldColor.dispose();
}

From source file:org.eclipse.swt.examples.javaviewer.JavaLineStyler.java

void disposeColors() {
    for (Color color : colors) {
        color.dispose();
    }
}

From source file:org.eclipse.swt.examples.helloworld.HelloWorld5.java

public Shell open(Display display) {
    final Color red = new Color(display, 0xFF, 0, 0);
    final Shell shell = new Shell(display);
    shell.addPaintListener(event -> {
        GC gc = event.gc;/*from  w ww. j  ava  2  s  .c  o m*/
        gc.setForeground(red);
        Rectangle rect = shell.getClientArea();
        gc.drawRectangle(rect.x + 10, rect.y + 10, rect.width - 20, rect.height - 20);
        gc.drawString(resHello.getString("Hello_world"), rect.x + 20, rect.y + 20);
    });
    shell.addDisposeListener(e -> red.dispose());
    shell.open();
    return shell;
}

From source file:org.eclipse.swt.examples.paint.PaintExample.java

/**
 * Disposes of all resources associated with a particular
 * instance of the PaintExample.//w w w . j  a  va  2s  . co m
 */
public void dispose() {
    if (paintSurface != null)
        paintSurface.dispose();
    if (paintColors != null) {
        for (final Color color : paintColors) {
            if (color != null)
                color.dispose();
        }
    }
    paintDefaultFont = null;
    paintColors = null;
    paintSurface = null;
    freeResources();
}

From source file:org.eclipse.swt.examples.controlexample.LinkTab.java

@Override
void changeFontOrColor(int index) {
    switch (index) {
    case LINK_FOREGROUND_COLOR: {
        Color oldColor = linkForegroundColor;
        if (oldColor == null)
            oldColor = link1.getLinkForeground();
        colorDialog.setRGB(oldColor.getRGB());
        RGB rgb = colorDialog.open();//from  w ww . j  av a2 s  .  co  m
        if (rgb == null)
            return;
        oldColor = linkForegroundColor;
        linkForegroundColor = new Color(display, rgb);
        setLinkForeground();
        if (oldColor != null)
            oldColor.dispose();
    }
        break;
    default:
        super.changeFontOrColor(index);
    }
}

From source file:HelloWorld5.java

public Shell open(Display display) {
    final Color red = new Color(display, 0xFF, 0, 0);
    final Shell shell = new Shell(display);
    shell.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent event) {
            GC gc = event.gc;/*w w w.  j a  v  a 2s  . co  m*/
            gc.setForeground(red);
            Rectangle rect = shell.getClientArea();
            gc.drawRectangle(rect.x + 10, rect.y + 10, rect.width - 20, rect.height - 20);
            gc.drawString("Hello_world", rect.x + 20, rect.y + 20);
        }
    });
    shell.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent e) {
            red.dispose();
        }
    });
    shell.open();
    return shell;
}