2015-07-01 50 views
1

除此之外question我明白我無法實現我的目標,我嘗試從圖像創建自己的光標。如何結合圖像作爲SWT光標使用

在我的示例中,我只是試圖在上面繪製一個帶有Starting Image File的綠色框。

我希望得到的結果看起來像Expected Result

這裏是我的代碼:

import org.eclipse.jface.resource.ImageDescriptor; 
import org.eclipse.swt.SWT; 

import org.eclipse.swt.graphics.Color; 
import org.eclipse.swt.graphics.Cursor; 
import org.eclipse.swt.graphics.GC; 
import org.eclipse.swt.graphics.Image; 
import org.eclipse.swt.graphics.ImageData; 
import org.eclipse.swt.graphics.RGB; 
import org.eclipse.swt.widgets.Canvas; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 

public class CursorTest { 

    public static void main(String[] args) { 
     final Display display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setBounds(10, 10, 200, 200); 

     Canvas canvas = new Canvas(shell, SWT.BORDER); 
     canvas.setBounds(10, 50, 150, 100); 

     canvas.setCursor(CursorTest.getCursor(display, new Color(display, new RGB(0, 255 ,0)))); 

     shell.open(); 

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

     display.dispose(); 
    } 

    public static Cursor getCursor(Display display, Color c) { 

     ImageDescriptor element = ImageDescriptor.createFromFile(
       ImageProvider.class, "arrow.png"); 

     Image a = element.createImage(); 


     int width = a.getBounds().width; 
     int height = a.getBounds().height; 
     Image image = makeTransparent(new Image(display, width, height)); 

     GC gc = new GC(image); 

     gc.setBackground(c); 
     gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK)); 
     gc.fillRectangle(4, 10, 12, 12); 
     gc.drawRectangle(4, 10, 12, 12); 
     gc.drawImage(a, 0, 0); 


     Cursor result = new Cursor(display, image.getImageData(), 
       0, 0); 
     gc.dispose(); 
     image.dispose(); 

     return result; 

    } 

    private static Image makeTransparent(Image sourceImage) { 
     ImageData data = sourceImage.getImageData(); 
     data.transparentPixel = data.palette.getPixel(new RGB(255, 255, 255)); 
     Image ret = new Image(sourceImage.getDevice(), data); 
     sourceImage.dispose(); 
     return ret; 
    } 

} 

正如你可以採用相當奇怪的看到這個功能,我無法明白爲什麼。任何人都可以幫忙嗎?

回答

1

SWT的透明度相當棘手。您可以使用CompositeImageDescriptor,以幫助這一點:

public static Cursor getCursor(final Display display) 
{ 
    OverlayImageDescriptor overlay = new OverlayImageDescriptor(); 

    Cursor result = new Cursor(display, overlay.getImageData(), 0, 0); 

    return result; 
} 

private static class OverlayImageDescriptor extends CompositeImageDescriptor 
{ 
    private final ImageData base; 

    OverlayImageDescriptor() 
    { 
    base = ImageDescriptor.createFromFile(ImageProvider.class, "arrow.pnh").getImageData(); 
    } 

    @Override 
    protected void drawCompositeImage(int width, int height) 
    { 
    Display display = Display.getCurrent(); 

    // Overlay image 

    Image image = new Image(display, 13, 13); 

    GC gc = new GC(image); 

    Color color = new Color(display, new RGB(0, 255 ,0)); 

    gc.setBackground(color); 
    gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK)); 
    gc.fillRectangle(0, 0, 12, 12); 
    gc.drawRectangle(0, 0, 12, 12); 

    gc.dispose(); 
    color.dispose(); 

    drawImage(image.getImageData(), 3, 3); 

    image.dispose(); 

    // Draw base image over the top of the overlay 

    drawImage(base, 0, 0); 
    } 

    @Override 
    protected Point getSize() 
    { 
    return new Point(base.width, base.height); 
    } 

}

+0

謝謝!是的透明度是一場噩夢,我從來沒有這樣做過。想你。 – Link19