2013-04-18 52 views
0

我寫了一個小程序來讀取鼠標指針位置上的一個像素的顏色。但是我沒有在鼠標指針處獲取顏色,總是在控制檯上打印RGB {0,0,0}。請修復/更正下面的程序,以便在鼠標指針上打印像素的顏色。如何在鼠標指針上讀取SWT畫布上像素的顏色?

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(); 

    } 
} 

回答

1

更新這些行:

Image image = new Image(e.display, 20, 20); 
GC gc = new GC(image); 
gc.copyArea(image, e.x, e.y); 

此:

Image image = new Image(e.display, 1, 1); 
GC gc = new GC((Canvas)e.widget); 
gc.copyArea(image, e.x, e.y); 

GC gc = new GC((Canvas)e.widget);是很重要的。

作爲一個很好的做法,處置GCimage對象一旦你完成!

+0

它不能完全工作,只有第一個像素顏色正確打印(即鼠標指針首次進入畫布區域時)。所有下一個鼠標移動事件都將打印RGB {0,0,0}。任何想法? – 2013-04-18 10:47:43

+0

你正在使用哪種操作系統,SWT,Java版本?該示例在Win7,SWT 3.7.2和Oracle JDK 1.6_b35上運行良好。即使你只是更新'GC GC =新的GC(圖像);'GC GC =新GC((帆布)e.widget);'它的工作。 – Favonius 2013-04-18 10:51:40

+0

哦。我正在使用Ubuntu 12.04。 JDK 1.6_b39和SWT 4.234(不確定)。是否依賴於操作系統 – 2013-04-18 11:03:52

相關問題