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:ImageAnalyzer.java

void paintPalette(PaintEvent event) {
    GC gc = event.gc;/*from w w  w  . j  a v a 2  s .co  m*/
    gc.fillRectangle(paletteCanvas.getClientArea());
    if (imageData.palette.isDirect) {
        // For a direct palette, display the masks.
        int y = py + 10;
        int xTab = 50;
        gc.drawString("rMsk", 10, y, true);
        gc.drawString(toHex4ByteString(imageData.palette.redMask), xTab, y, true);
        gc.drawString("gMsk", 10, y += 12, true);
        gc.drawString(toHex4ByteString(imageData.palette.greenMask), xTab, y, true);
        gc.drawString("bMsk", 10, y += 12, true);
        gc.drawString(toHex4ByteString(imageData.palette.blueMask), xTab, y, true);
        gc.drawString("rShf", 10, y += 12, true);
        gc.drawString(Integer.toString(imageData.palette.redShift), xTab, y, true);
        gc.drawString("gShf", 10, y += 12, true);
        gc.drawString(Integer.toString(imageData.palette.greenShift), xTab, y, true);
        gc.drawString("bShf", 10, y += 12, true);
        gc.drawString(Integer.toString(imageData.palette.blueShift), xTab, y, true);
    } else {
        // For an indexed palette, display the palette colors and indices.
        RGB[] rgbs = imageData.palette.getRGBs();
        if (rgbs != null) {
            int xTab1 = 40, xTab2 = 100;
            for (int i = 0; i < rgbs.length; i++) {
                int y = (i + 1) * 10 + py;
                gc.drawString(String.valueOf(i), 10, y, true);
                gc.drawString(toHexByteString(rgbs[i].red) + toHexByteString(rgbs[i].green)
                        + toHexByteString(rgbs[i].blue), xTab1, y, true);
                Color color = new Color(display, rgbs[i]);
                gc.setBackground(color);
                gc.fillRectangle(xTab2, y + 2, 10, 10);
                color.dispose();
            }
        }
    }
}

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

void changeFontOrColor(int index) {
    switch (index) {
    case FOREGROUND_COLOR: {
        Color oldColor = foregroundColor;
        if (oldColor == null) {
            Control[] controls = getExampleControls();
            if (controls.length > 0)
                oldColor = controls[0].getForeground();
        }/*  ww  w .  j  ava2s .  co m*/
        if (oldColor != null)
            colorDialog.setRGB(oldColor.getRGB()); // seed dialog with current color
        RGB rgb = colorDialog.open();
        if (rgb == null)
            return;
        oldColor = foregroundColor; // save old foreground color to dispose when done
        foregroundColor = new Color(display, rgb);
        setExampleWidgetForeground();
        if (oldColor != null)
            oldColor.dispose();
    }
        break;
    case BACKGROUND_COLOR: {
        Color oldColor = backgroundColor;
        if (oldColor == null) {
            Control[] controls = getExampleControls();
            if (controls.length > 0)
                oldColor = controls[0].getBackground(); // seed dialog with current color
        }
        if (oldColor != null)
            colorDialog.setRGB(oldColor.getRGB());
        RGB rgb = colorDialog.open();
        if (rgb == null)
            return;
        oldColor = backgroundColor; // save old background color to dispose when done
        backgroundColor = new Color(display, rgb);
        setExampleWidgetBackground();
        if (oldColor != null)
            oldColor.dispose();
    }
        break;
    case FONT: {
        Font oldFont = font;
        if (oldFont == null) {
            Control[] controls = getExampleControls();
            if (controls.length > 0)
                oldFont = controls[0].getFont();
        }
        if (oldFont != null)
            fontDialog.setFontList(oldFont.getFontData()); // seed dialog with current font
        FontData fontData = fontDialog.open();
        if (fontData == null)
            return;
        oldFont = font; // dispose old font when done
        font = new Font(display, fontData);
        setExampleWidgetFont();
        setExampleWidgetSize();
        if (oldFont != null)
            oldFont.dispose();
    }
        break;
    }
}

From source file:ImageAnalyzer.java

void animateLoop() {
    // Create an off-screen image to draw on, and a GC to draw with.
    // Both are disposed after the animation.
    Image offScreenImage = new Image(display, loader.logicalScreenWidth, loader.logicalScreenHeight);
    GC offScreenImageGC = new GC(offScreenImage);

    try {//from  w  w  w. jav  a2  s.co m
        // Use syncExec to get the background color of the imageCanvas.
        display.syncExec(new Runnable() {
            public void run() {
                canvasBackground = imageCanvas.getBackground();
            }
        });

        // Fill the off-screen image with the background color of the
        // canvas.
        offScreenImageGC.setBackground(canvasBackground);
        offScreenImageGC.fillRectangle(0, 0, loader.logicalScreenWidth, loader.logicalScreenHeight);

        // Draw the current image onto the off-screen image.
        offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height, imageData.x, imageData.y,
                imageData.width, imageData.height);

        int repeatCount = loader.repeatCount;
        while (animate && (loader.repeatCount == 0 || repeatCount > 0)) {
            if (imageData.disposalMethod == SWT.DM_FILL_BACKGROUND) {
                // Fill with the background color before drawing.
                Color bgColor = null;
                int backgroundPixel = loader.backgroundPixel;
                if (showBackground && backgroundPixel != -1) {
                    // Fill with the background color.
                    RGB backgroundRGB = imageData.palette.getRGB(backgroundPixel);
                    bgColor = new Color(null, backgroundRGB);
                }
                try {
                    offScreenImageGC.setBackground(bgColor != null ? bgColor : canvasBackground);
                    offScreenImageGC.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height);
                } finally {
                    if (bgColor != null)
                        bgColor.dispose();
                }
            } else if (imageData.disposalMethod == 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);
            }

            // Get the next image data.
            imageDataIndex = (imageDataIndex + 1) % imageDataArray.length;
            imageData = imageDataArray[imageDataIndex];
            image.dispose();
            image = new Image(display, imageData);

            // Draw the new image data.
            offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height, imageData.x, imageData.y,
                    imageData.width, imageData.height);

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

            // Sleep for the specified delay time before drawing again.
            try {
                Thread.sleep(visibleDelay(imageData.delayTime * 10));
            } catch (InterruptedException e) {
            }

            // If we have just drawn the last image in the set,
            // then decrement the repeat count.
            if (imageDataIndex == imageDataArray.length - 1)
                repeatCount--;
        }
    } finally {
        offScreenImage.dispose();
        offScreenImageGC.dispose();
    }
}

From source file:org.eclipse.swt.examples.imageanalyzer.ImageAnalyzer.java

void animateLoop() {
    // Create an off-screen image to draw on, and a GC to draw with.
    // Both are disposed after the animation.
    Image offScreenImage = new Image(display, loader.logicalScreenWidth, loader.logicalScreenHeight);
    GC offScreenImageGC = new GC(offScreenImage);

    try {/*from w  w  w .j  a v  a 2 s .c  om*/
        // Use syncExec to get the background color of the imageCanvas.
        display.syncExec(() -> canvasBackground = imageCanvas.getBackground());

        // Fill the off-screen image with the background color of the canvas.
        offScreenImageGC.setBackground(canvasBackground);
        offScreenImageGC.fillRectangle(0, 0, loader.logicalScreenWidth, loader.logicalScreenHeight);

        // Draw the current image onto the off-screen image.
        offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height, imageData.x, imageData.y,
                imageData.width, imageData.height);

        int repeatCount = loader.repeatCount;
        while (animate && (loader.repeatCount == 0 || repeatCount > 0)) {
            if (imageData.disposalMethod == SWT.DM_FILL_BACKGROUND) {
                // Fill with the background color before drawing.
                Color bgColor = null;
                int backgroundPixel = loader.backgroundPixel;
                if (showBackground && backgroundPixel != -1) {
                    // Fill with the background color.
                    RGB backgroundRGB = imageData.palette.getRGB(backgroundPixel);
                    bgColor = new Color(null, backgroundRGB);
                }
                try {
                    offScreenImageGC.setBackground(bgColor != null ? bgColor : canvasBackground);
                    offScreenImageGC.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height);
                } finally {
                    if (bgColor != null)
                        bgColor.dispose();
                }
            } else if (imageData.disposalMethod == 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);
            }

            // Get the next image data.
            imageDataIndex = (imageDataIndex + 1) % imageDataArray.length;
            imageData = imageDataArray[imageDataIndex];
            image.dispose();
            image = new Image(display, imageData);

            // Draw the new image data.
            offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height, imageData.x, imageData.y,
                    imageData.width, imageData.height);

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

            // Sleep for the specified delay time before drawing again.
            try {
                Thread.sleep(visibleDelay(imageData.delayTime * 10));
            } catch (InterruptedException e) {
            }

            // If we have just drawn the last image in the set,
            // then decrement the repeat count.
            if (imageDataIndex == imageDataArray.length - 1)
                repeatCount--;
        }
    } finally {
        offScreenImage.dispose();
        offScreenImageGC.dispose();
    }
}

From source file:PaintExample.java

/**
 * Disposes of all resources associated with a particular
 * instance of the PaintExample./*from  w  w w  .  j ava2  s.c  om*/
 */
public void dispose() {
    if (paintSurface != null)
        paintSurface.dispose();
    if (paintColors != null) {
        for (int i = 0; i < paintColors.length; ++i) {
            final Color color = paintColors[i];
            if (color != null)
                color.dispose();
        }
    }
    paintDefaultFont = null;
    paintColors = null;
    paintSurface = null;
    freeResources();
}

From source file:PrintKTableExample.java

public void drawCell(GC gc, Rectangle rect, int col, int row, Object content, boolean focus, boolean fixed,
        boolean clicked) {
    // Performance test:
    /*/*from  w  ww  .j  a  v a  2s  . com*/
     * gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
     * gc.fillRectangle(rect);
     * 
     * int j=1; for (int i = 0; i < 10000000; i++) { j++; }
     */
    Color color = new Color(m_Display, (RGB) content);
    gc.setBackground(m_Display.getSystemColor(SWT.COLOR_WHITE));
    rect.height++;
    rect.width++;
    gc.fillRectangle(rect);

    gc.setBackground(color);
    if (!focus) {
        rect.x += 1;
        rect.y += 1;
        rect.height -= 2;
        rect.width -= 2;
    }
    gc.fillRectangle(rect);
    color.dispose();
}

From source file:CustomControlExample.java

/**
 * Sets the foreground color, background color, and font of the "Example"
 * widgets to their default settings. Also sets foreground and background
 * color of the Node 1 TreeItems to default settings.
 *///ww  w  .j  av  a 2  s .c o  m
void resetColorsAndFonts() {
    Color oldColor = foregroundSelectionColor;
    foregroundSelectionColor = null;
    if (oldColor != null)
        oldColor.dispose();
    oldColor = backgroundSelectionColor;
    backgroundSelectionColor = null;
    if (oldColor != null)
        oldColor.dispose();
    Font oldFont = itemFont;
    itemFont = null;
    if (oldFont != null)
        oldFont.dispose();
    super.resetColorsAndFonts();
}

From source file:CustomControlExample.java

/**
 * Sets the foreground color, background color, and font of the "Example"
 * widgets to their default settings. Subclasses may extend in order to
 * reset other colors and fonts to default settings as well.
 *//*from   ww w .  j  a  v  a  2  s  .  co  m*/
void resetColorsAndFonts() {
    Color oldColor = foregroundColor;
    foregroundColor = null;
    setExampleWidgetForeground();
    if (oldColor != null)
        oldColor.dispose();
    oldColor = backgroundColor;
    backgroundColor = null;
    setExampleWidgetBackground();
    if (oldColor != null)
        oldColor.dispose();
    Font oldFont = font;
    font = null;
    setExampleWidgetFont();
    setExampleWidgetSize();
    if (oldFont != null)
        oldFont.dispose();
}

From source file:CustomControlExample.java

/**
 * Sets the foreground color, background color, and font of the "Example"
 * widgets to their default settings. Also sets foreground and background
 * color of the Node 1 TreeItems to default settings.
 *//*from w  ww.ja va2s.c o  m*/
void resetColorsAndFonts() {
    super.resetColorsAndFonts();
    Color oldColor = itemForegroundColor;
    itemForegroundColor = null;
    setItemForeground();
    if (oldColor != null)
        oldColor.dispose();
    oldColor = itemBackgroundColor;
    itemBackgroundColor = null;
    setItemBackground();
    if (oldColor != null)
        oldColor.dispose();
    Font oldFont = font;
    itemFont = null;
    setItemFont();
    setExampleWidgetSize();
    if (oldFont != null)
        oldFont.dispose();
}

From source file:CustomControlExample.java

/**
 * Creates the "Colors" group.//w  w w.j a va2s  .c  o  m
 */
void createColorGroup() {
    super.createColorGroup();

    itemGroup = new Group(colorGroup, SWT.NONE);
    itemGroup.setText(ControlExample.getResourceString("Tree_Item_Colors"));
    GridData data = new GridData();
    data.horizontalSpan = 2;
    itemGroup.setLayoutData(data);
    itemGroup.setLayout(new GridLayout(2, false));
    new Label(itemGroup, SWT.NONE).setText(ControlExample.getResourceString("Foreground_Color"));
    itemForegroundButton = new Button(itemGroup, SWT.PUSH);
    new Label(itemGroup, SWT.NONE).setText(ControlExample.getResourceString("Background_Color"));
    itemBackgroundButton = new Button(itemGroup, SWT.PUSH);
    itemFontButton = new Button(itemGroup, SWT.PUSH);
    itemFontButton.setText(ControlExample.getResourceString("Font"));
    itemFontButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

    Shell shell = colorGroup.getShell();
    final ColorDialog foregroundDialog = new ColorDialog(shell);
    final ColorDialog backgroundDialog = new ColorDialog(shell);
    final FontDialog fontDialog = new FontDialog(shell);

    int imageSize = 12;
    Display display = shell.getDisplay();
    itemForegroundImage = new Image(display, imageSize, imageSize);
    itemBackgroundImage = new Image(display, imageSize, imageSize);

    /* Add listeners to set the colors and font */
    itemForegroundButton.setImage(itemForegroundImage);
    itemForegroundButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = itemForegroundColor;
            if (oldColor == null)
                oldColor = textNode1.getForeground();
            foregroundDialog.setRGB(oldColor.getRGB());
            RGB rgb = foregroundDialog.open();
            if (rgb == null)
                return;
            oldColor = itemForegroundColor;
            itemForegroundColor = new Color(event.display, rgb);
            setItemForeground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    itemBackgroundButton.setImage(itemBackgroundImage);
    itemBackgroundButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = itemBackgroundColor;
            if (oldColor == null)
                oldColor = textNode1.getBackground();
            backgroundDialog.setRGB(oldColor.getRGB());
            RGB rgb = backgroundDialog.open();
            if (rgb == null)
                return;
            oldColor = itemBackgroundColor;
            itemBackgroundColor = new Color(event.display, rgb);
            setItemBackground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    itemFontButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Font oldFont = itemFont;
            if (oldFont == null)
                oldFont = textNode1.getFont();
            fontDialog.setFontList(oldFont.getFontData());
            FontData fontData = fontDialog.open();
            if (fontData == null)
                return;
            oldFont = itemFont;
            itemFont = new Font(event.display, fontData);
            setItemFont();
            setExampleWidgetSize();
            if (oldFont != null)
                oldFont.dispose();
        }
    });
    shell.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent event) {
            if (itemBackgroundImage != null)
                itemBackgroundImage.dispose();
            if (itemForegroundImage != null)
                itemForegroundImage.dispose();
            if (itemBackgroundColor != null)
                itemBackgroundColor.dispose();
            if (itemForegroundColor != null)
                itemForegroundColor.dispose();
            if (itemFont != null)
                itemFont.dispose();
            itemBackgroundColor = null;
            itemForegroundColor = null;
            itemFont = null;
        }
    });
}