0

我写了一个小程序来读取鼠标指针位置像素的颜色。但我没有得到鼠标指针的颜色,RGB {0, 0, 0} 总是在控制台上打印。请修复/更正下面的程序,以便它在鼠标指针处打印像素的颜色。

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

    }
}
4

1 回答 1

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);很重要。

作为一个很好的做法,一旦你完成了就GC处理 和对象!image

于 2013-04-18T09:35:22.613 回答