Skip to main content


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 KumarswamimathFriend
Messages: 26
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 RajFriend
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 next message
Vijay RajFriend
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 #1055642 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1055646 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1055713 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1055714 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1055715 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1055716 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1055720 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1055786 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1055787 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1055788 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1055789 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1055793 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1055864 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1055865 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1055866 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1055867 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1055871 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1055942 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1055943 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1055944 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1055945 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1055949 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1056021 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1056022 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1056023 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1056024 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1056028 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1056102 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1056103 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1056104 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1056105 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1056109 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1056187 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1056188 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1056189 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1056190 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1056194 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1056273 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1056274 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1056275 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1056276 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1056280 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1056361 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1056362 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1056363 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1056364 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1056368 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1056450 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1056451 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1056452 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1056453 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1056457 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1056542 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1056543 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1056544 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1056545 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1056549 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1056637 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1056638 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1056639 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1056640 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1056644 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1056733 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1056734 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1056735 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1056736 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1056740 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1056831 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1056832 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1056833 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1056834 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1056838 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1056932 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1056933 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1056934 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1056935 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1056939 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1057033 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1057034 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1057035 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1057036 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1057040 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1057138 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1057139 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1057140 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1057141 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1057145 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1057242 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1057243 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1057244 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1057245 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1057249 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1057347 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1057348 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1057349 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1057350 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1057354 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1057453 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1057454 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1057455 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1057456 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1057460 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1057565 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1057566 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1057567 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1057568 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1057572 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1057673 is a reply to message #1043770] Wed, 08 May 2013 10:01 Go to previous messageGo to next message
Vijay RajFriend
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 #1057674 is a reply to message #1043770] Wed, 08 May 2013 10:05 Go to previous messageGo to next message
Vijay RajFriend
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 #1057675 is a reply to message #1043770] Wed, 08 May 2013 10:06 Go to previous messageGo to next message
Vijay RajFriend
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 #1057676 is a reply to message #1043770] Wed, 08 May 2013 10:09 Go to previous messageGo to next message
Vijay RajFriend
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 #1057680 is a reply to message #1043770] Wed, 08 May 2013 10:36 Go to previous messageGo to next message
Vijay RajFriend
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 #1057719 is a reply to message #1055574] Wed, 08 May 2013 12:52 Go to previous message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
Sorry for so many replies, bug in the forum...

---------------------
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: Thu Mar 28 08:15:07 GMT 2024

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

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

Back to the top