如何组合图像以用作 SWT 光标
How to combine images to use as SWT cursor
此外 question 据我所知,我无法实现我的目标,我尝试从图像创建我自己的光标。
在我的示例中,我只是试图在顶部绘制一个带有 的绿色框。
我希望结果看起来像
这是我的代码:
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;
}
}
如您所见,此功能相当奇怪,我无法弄清楚原因。有人可以帮忙吗?
透明度对于 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);
}
}
此外 question 据我所知,我无法实现我的目标,我尝试从图像创建我自己的光标。
在我的示例中,我只是试图在顶部绘制一个带有
我希望结果看起来像
这是我的代码:
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;
}
}
如您所见,此功能相当奇怪,我无法弄清楚原因。有人可以帮忙吗?
透明度对于 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);
}
}