Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Reading pixel color on SWT canvas at mouse pointer(Detecting the pixel color on SWT canvas at mouse pointer location in mouse move listener)
Reading pixel color on SWT canvas at mouse pointer [message #1043770] Thu, 18 April 2013 05:28 Go to next message
Chandrayya Kumarswamimath is currently offline Chandrayya Kumarswamimath
Messages: 20
Registered: August 2010
Location: Bangalore
Junior Member
I wrote a small program to read the colour of a pixel at the mouse pointer location. But I am not getting the colour at mouse pointer, always RGB {0, 0, 0} is printing on console. Please fix/correct the program below so that it prints the colour of a pixel at mouse pointer.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
    private static final int RECT_HEIGHT = 20;
    private static final int RECT_WIDTH = 20;
    private static final int CYCLE_OFFSET = 0;
    protected static final int Y_STEP = 20;
    static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
    static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display, shellStyle);
        shell.setLayout(new FillLayout());
        shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
        shell.setText("Canvas Test");
        shell.setSize(400, 300);

        Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        composite.setLayout(new GridLayout(1, false));

        final Canvas canvas = new Canvas(composite, canvasStyle);
        canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        final Point cycleOrigin = new Point(CYCLE_OFFSET, 0);

        // Create a paint handler for the canvas
        canvas.addPaintListener(new PaintListener() {
            @Override
            public void paintControl(PaintEvent e) {

                for (int i = 0; i < 100; i++) {                    
                    for (int j = 0; j < 100; j++) {                     
                        Color oldBgColor = e.gc.getBackground();
                        Color oldFgColor = e.gc.getForeground();
                        if(j%2 == 0) {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));                            
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        } else if(j%3 == 0) {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        } else {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        }
                    }
                }
            }

        });
        canvas.addMouseMoveListener(new MouseMoveListener() {

            @Override
            public void mouseMove(MouseEvent e) {
                Image image = new Image(e.display, 20, 20);
                GC gc = new GC(image);
                gc.copyArea(image, e.x, e.y);
                ImageData imageData = image.getImageData();                
                int pixelValue = imageData.getPixel(0, 0);
                PaletteData palette = imageData.palette;
                RGB rgb = palette.getRGB(pixelValue);
                System.out.println(rgb);

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

    }
}
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055015 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
    private static final int RECT_HEIGHT = 20;
    private static final int RECT_WIDTH = 20;
    private static final int CYCLE_OFFSET = 0;
    protected static final int Y_STEP = 20;
    static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
    static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display, shellStyle);
        shell.setLayout(new FillLayout());
        shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
        shell.setText("Canvas Test");
        shell.setSize(400, 300);

        Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        composite.setLayout(new GridLayout(1, false));

        final Canvas canvas = new Canvas(composite, canvasStyle);
        canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

        // Create a paint handler for the canvas
        canvas.addPaintListener(new PaintListener() {
            @Override
            public void paintControl(PaintEvent e) {

                for (int i = 0; i < 100; i++) {                    
                    for (int j = 0; j < 100; j++) {                     
                        Color oldBgColor = e.gc.getBackground();
                        Color oldFgColor = e.gc.getForeground();
                        if(j%2 == 0) {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));                            
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        } else if(j%3 == 0) {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        } else {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        }
                    }
                }
            }

        });
        canvas.addMouseMoveListener(new MouseMoveListener() {

            @Override
            public void mouseMove(MouseEvent e) {
                Image image = new Image(e.display, 20, 20);
                GC gc = new GC(canvas);
                gc.copyArea(image, e.x, e.y);
                ImageData imageData = image.getImageData();                
                int pixelValue = imageData.getPixel(0, 0);
                PaletteData palette = imageData.palette;
                RGB rgb = palette.getRGB(pixelValue);
                System.out.println(rgb);
                gc.drawImage(image, 0, 0);
                image.dispose();
                gc.dispose();
            }
        });
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();

    }
}


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055034 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
    private static final int RECT_HEIGHT = 20;
    private static final int RECT_WIDTH = 20;
    private static final int CYCLE_OFFSET = 0;
    protected static final int Y_STEP = 20;
    static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
    static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display, shellStyle);
        shell.setLayout(new FillLayout());
        shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
        shell.setText("Canvas Test");
        shell.setSize(400, 300);

        Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        composite.setLayout(new GridLayout(1, false));

        final Canvas canvas = new Canvas(composite, canvasStyle);
        canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

        // Create a paint handler for the canvas
        canvas.addPaintListener(new PaintListener() {
            @Override
            public void paintControl(PaintEvent e) {

                for (int i = 0; i < 100; i++) {                    
                    for (int j = 0; j < 100; j++) {                     
                        Color oldBgColor = e.gc.getBackground();
                        Color oldFgColor = e.gc.getForeground();
                        if(j%2 == 0) {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));                            
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        } else if(j%3 == 0) {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        } else {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        }
                    }
                }
            }

        });
        canvas.addMouseMoveListener(new MouseMoveListener() {

            @Override
            public void mouseMove(MouseEvent e) {
                Image image = new Image(e.display, 20, 20);
                GC gc = new GC(canvas);
                gc.copyArea(image, e.x, e.y);
                ImageData imageData = image.getImageData();                
                int pixelValue = imageData.getPixel(0, 0);
                PaletteData palette = imageData.palette;
                RGB rgb = palette.getRGB(pixelValue);
                System.out.println(rgb);
                gc.drawImage(image, 0, 0);
                image.dispose();
                gc.dispose();
            }
        });
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();

    }
}


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055074 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
Use the canvas to create the gc and dont forget to dispose the gc and image.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
    private static final int RECT_HEIGHT = 20;
    private static final int RECT_WIDTH = 20;
    private static final int CYCLE_OFFSET = 0;
    protected static final int Y_STEP = 20;
    static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
    static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display, shellStyle);
        shell.setLayout(new FillLayout());
        shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
        shell.setText("Canvas Test");
        shell.setSize(400, 300);

        Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        composite.setLayout(new GridLayout(1, false));

        final Canvas canvas = new Canvas(composite, canvasStyle);
        canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

        // Create a paint handler for the canvas
        canvas.addPaintListener(new PaintListener() {
            @Override
            public void paintControl(PaintEvent e) {

                for (int i = 0; i < 100; i++) {                    
                    for (int j = 0; j < 100; j++) {                     
                        Color oldBgColor = e.gc.getBackground();
                        Color oldFgColor = e.gc.getForeground();
                        if(j%2 == 0) {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));                            
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        } else if(j%3 == 0) {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        } else {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        }
                    }
                }
            }

        });
        canvas.addMouseMoveListener(new MouseMoveListener() {

            @Override
            public void mouseMove(MouseEvent e) {
                Image image = new Image(e.display, 20, 20);
                GC gc = new GC(canvas);
                gc.copyArea(image, e.x, e.y);
                ImageData imageData = image.getImageData();                
                int pixelValue = imageData.getPixel(0, 0);
                PaletteData palette = imageData.palette;
                RGB rgb = palette.getRGB(pixelValue);
                System.out.println(rgb);
                gc.drawImage(image, 0, 0);
                image.dispose();
                gc.dispose();
            }
        });
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();

    }
}


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055075 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
    private static final int RECT_HEIGHT = 20;
    private static final int RECT_WIDTH = 20;
    private static final int CYCLE_OFFSET = 0;
    protected static final int Y_STEP = 20;
    static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
    static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display, shellStyle);
        shell.setLayout(new FillLayout());
        shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
        shell.setText("Canvas Test");
        shell.setSize(400, 300);

        Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        composite.setLayout(new GridLayout(1, false));

        final Canvas canvas = new Canvas(composite, canvasStyle);
        canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

        // Create a paint handler for the canvas
        canvas.addPaintListener(new PaintListener() {
            @Override
            public void paintControl(PaintEvent e) {

                for (int i = 0; i < 100; i++) {                    
                    for (int j = 0; j < 100; j++) {                     
                        Color oldBgColor = e.gc.getBackground();
                        Color oldFgColor = e.gc.getForeground();
                        if(j%2 == 0) {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));                            
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        } else if(j%3 == 0) {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        } else {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        }
                    }
                }
            }

        });
        canvas.addMouseMoveListener(new MouseMoveListener() {

            @Override
            public void mouseMove(MouseEvent e) {
                Image image = new Image(e.display, 20, 20);
                GC gc = new GC(canvas);
                gc.copyArea(image, e.x, e.y);
                ImageData imageData = image.getImageData();                
                int pixelValue = imageData.getPixel(0, 0);
                PaletteData palette = imageData.palette;
                RGB rgb = palette.getRGB(pixelValue);
                System.out.println(rgb);
                gc.drawImage(image, 0, 0);
                image.dispose();
                gc.dispose();
            }
        });
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();

    }
}


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055111 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055112 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055170 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055171 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055172 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
Use the canvas to create the gc and dont forget to dispose the gc and image.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055173 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055232 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055233 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055234 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
Use the canvas to create the gc and dont forget to dispose the gc and image.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055235 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055297 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055298 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055299 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
Use the canvas to create the gc and dont forget to dispose the gc and image.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055300 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055363 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055364 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055365 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
Use the canvas to create the gc and dont forget to dispose the gc and image.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055366 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055428 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055429 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055430 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
Use the canvas to create the gc and dont forget to dispose the gc and image.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055431 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055459 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
    private static final int RECT_HEIGHT = 20;
    private static final int RECT_WIDTH = 20;
    private static final int CYCLE_OFFSET = 0;
    protected static final int Y_STEP = 20;
    static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
    static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display, shellStyle);
        shell.setLayout(new FillLayout());
        shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
        shell.setText("Canvas Test");
        shell.setSize(400, 300);

        Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        composite.setLayout(new GridLayout(1, false));

        final Canvas canvas = new Canvas(composite, canvasStyle);
        canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

        // Create a paint handler for the canvas
        canvas.addPaintListener(new PaintListener() {
            @Override
            public void paintControl(PaintEvent e) {

                for (int i = 0; i < 100; i++) {                    
                    for (int j = 0; j < 100; j++) {                     
                        Color oldBgColor = e.gc.getBackground();
                        Color oldFgColor = e.gc.getForeground();
                        if(j%2 == 0) {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));                            
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        } else if(j%3 == 0) {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        } else {
                            e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
                            e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);                        
                            e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
                            e.gc.setBackground(oldBgColor);
                            e.gc.setForeground(oldFgColor);
                        }
                    }
                }
            }

        });
        canvas.addMouseMoveListener(new MouseMoveListener() {

            @Override
            public void mouseMove(MouseEvent e) {
                Image image = new Image(e.display, 20, 20);
                GC gc = new GC(canvas);
                gc.copyArea(image, e.x, e.y);
                ImageData imageData = image.getImageData();                
                int pixelValue = imageData.getPixel(0, 0);
                PaletteData palette = imageData.palette;
                RGB rgb = palette.getRGB(pixelValue);
                System.out.println(rgb);
                gc.drawImage(image, 0, 0);
                image.dispose();
                gc.dispose();
            }
        });
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();

    }
}


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055497 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055498 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055499 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
Use the canvas to create the gc and dont forget to dispose the gc and image.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055500 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055567 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055568 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055569 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
Use the canvas to create the gc and dont forget to dispose the gc and image.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055570 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055574 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055639 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055640 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Reading pixel color on SWT canvas at mouse pointer [message #1055641 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to previous message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
Use the canvas to create the gc and dont forget to dispose the gc and image.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PixelColorPick {
private static final int RECT_HEIGHT = 20;
private static final int RECT_WIDTH = 20;
private static final int CYCLE_OFFSET = 0;
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND | SWT.CLOSE | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE | SWT.H_SCROLL | SWT.V_SCROLL;

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText("Canvas Test");
shell.setSize(400, 300);

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));

final Canvas canvas = new Canvas(composite, canvasStyle);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Point cycleOrigin = new Point(CYCLE_OFFSET, 20);

// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {

for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
Color oldBgColor = e.gc.getBackground();
Color oldFgColor = e.gc.getForeground();
if(j%2 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_RED));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else if(j%3 == 0) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
} else {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawRectangle(cycleOrigin.x + j * RECT_WIDTH, cycleOrigin.y + i * Y_STEP, RECT_WIDTH, RECT_HEIGHT);
e.gc.fillRectangle(cycleOrigin.x + 1 + j * RECT_WIDTH, cycleOrigin.y + 1 + i * Y_STEP, RECT_WIDTH - 1, RECT_HEIGHT -1);
e.gc.setBackground(oldBgColor);
e.gc.setForeground(oldFgColor);
}
}
}
}

});
canvas.addMouseMoveListener(new MouseMoveListener() {

@Override
public void mouseMove(MouseEvent e) {
Image image = new Image(e.display, 20, 20);
GC gc = new GC(canvas);
gc.copyArea(image, e.x, e.y);
ImageData imageData = image.getImageData();
int pixelValue = imageData.getPixel(0, 0);
PaletteData palette = imageData.palette;
RGB rgb = palette.getRGB(pixelValue);
System.out.println(rgb);
gc.drawImage(image, 0, 0);
image.dispose();
gc.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
--
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Previous Topic:MouseDown events on composite causing freezing?
Next Topic:Decrease Table row padding
Goto Forum:
  


Current Time: Tue Oct 21 18:43:06 GMT 2014

Powered by FUDForum. Page generated in 0.04363 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software