java - How to read color of a pixel on SWT canvas at mouse pointer? -


i wrote small program read colour of pixel @ mouse pointer location. not getting colour @ mouse pointer, rgb {0, 0, 0} printing on console. please fix/correct program below prints colour of pixel @ 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 paint handler canvas         canvas.addpaintlistener(new paintlistener() {             @override             public void paintcontrol(paintevent e) {                  (int = 0; < 100; i++) {                                         (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 + * y_step, rect_width, rect_height);                                                     e.gc.fillrectangle(cycleorigin.x + 1 + j * rect_width, cycleorigin.y + 1 + * 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 + * y_step, rect_width, rect_height);                                                     e.gc.fillrectangle(cycleorigin.x + 1 + j * rect_width, cycleorigin.y + 1 + * 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 + * y_step, rect_width, rect_height);                                                     e.gc.fillrectangle(cycleorigin.x + 1 + j * rect_width, cycleorigin.y + 1 + * 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();      } } 

update these lines to:

image image = new image(e.display, 20, 20); gc gc = new gc(image); gc.copyarea(image, e.x, e.y); 

this:

image image = new image(e.display, 1, 1); gc gc = new gc((canvas)e.widget); gc.copyarea(image, e.x, e.y); 

the gc gc = new gc((canvas)e.widget); important.

and practice, dispose gc , image objects once done !


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -