Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaSWT在透明图像上绘制_Java_Graphics_Drawing_Swt - Fatal编程技术网

使用JavaSWT在透明图像上绘制

使用JavaSWT在透明图像上绘制,java,graphics,drawing,swt,Java,Graphics,Drawing,Swt,如何创建内存中完全透明的SWT图像,并在启用了antialias的情况下在其上绘制一条黑线 我希望结果只包括黑色和alpha值,范围从0到255,这是由于抗锯齿 我在谷歌上搜索并尽我所能。。。这有可能吗?我能够做到这一点,尽管感觉有点不舒服: Display display = Display.getDefault(); int width = 10; int height = 10; Image canvas = new Image(display, width, height); GC

如何创建内存中完全透明的SWT图像,并在启用了antialias的情况下在其上绘制一条黑线

我希望结果只包括黑色和alpha值,范围从0到255,这是由于抗锯齿


我在谷歌上搜索并尽我所能。。。这有可能吗?

我能够做到这一点,尽管感觉有点不舒服:

Display display = Display.getDefault();

int width = 10;
int height = 10;

Image canvas = new Image(display, width, height);

GC gc = new GC(canvas);

gc.setAntialias(SWT.ON);

// This sets the alpha on the entire canvas to transparent
gc.setAlpha(0);
gc.fillRectangle(0, 0, width, height);

// Reset our alpha and draw a line
gc.setAlpha(255);
gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
gc.drawLine(0, 0, width, height);

// We're done with the GC, so dispose of it
gc.dispose();

ImageData canvasData = canvas.getImageData();
canvasData.alphaData = new byte[width * height];

// This is the hacky bit that is making assumptions about
// the underlying ImageData.  In my case it is 32 bit data
// so every 4th byte in the data array is the alpha for that
// pixel...
for (int idx = 0; idx < (width * height); idx++) {
    int coord = (idx * 4) + 3;
    canvasData.alphaData[idx] = canvasData.data[coord];
}

// Now that we've set the alphaData, we can create our
// final image
Image finalImage = new Image(canvasData);

// And get rid of the canvas
canvas.dispose();
Display Display=Display.getDefault();
整数宽度=10;
整数高度=10;
图像画布=新图像(显示、宽度、高度);
GC=新GC(画布);
gc.setAntialias(SWT.ON);
//这会将整个画布上的alpha设置为透明
gc.setAlpha(0);
填充矩形(0,0,宽度,高度);
//重置我们的alpha并画一条线
gc.setAlpha(255);
gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
gc.绘制线(0,0,宽度,高度);
//我们已经处理完GC了,所以把它处理掉
gc.dispose();
ImageData canvasData=canvas.getImageData();
canvasData.alphaData=新字节[宽度*高度];
//这是一个令人讨厌的部分,它正在对
//底层的ImageData。在我的例子中,它是32位数据
//因此,数据数组中的每4个字节就是它的alpha
//像素。。。
对于(int-idx=0;idx<(宽度*高度);idx++){
int-coord=(idx*4)+3;
canvasData.alphaData[idx]=canvasData.data[coord];
}
//现在我们已经设置了alphaData,我们可以创建
//最终图像
图像最终图像=新图像(canvasData);
//然后扔掉画布
canvas.dispose();

在此之后,
finalImage
可以通过
drawImage
绘制到
GC
中,透明部分将得到尊重。

我就是这样做的,它的工作原理是:

    Image src = new Image(null, 16, 16);        
    ImageData imageData = src.getImageData();
    imageData.transparentPixel = imageData.getPixel(0, 0);
    src.dispose();
    Image icon = new Image(null, imageData);
    //draw on the icon with gc

我通过分配一个
ImageData
,使其透明,然后从数据中创建
图像

static Image createTransparentImage(Display display, int width, int height) {
    // allocate an image data
    ImageData imData = new ImageData(width, height, 24, new PaletteData(0xff0000,0x00ff00, 0x0000ff));
    imData.setAlpha(0, 0, 0); // just to force alpha array allocation with the right size
    Arrays.fill(imData.alphaData, (byte) 0); // set whole image as transparent

    // Initialize image from transparent image data
    return new Image(display, imData);
}

为了增加透明度,我发现我必须手动设置alpha字节数组,如下所示。因此,alpha以最近邻抗锯齿结束

public static Image scaleImage(Device device, Image orig, int scaledWidth, int scaledHeight) {
    Rectangle origBounds = orig.getBounds();
    if (origBounds.width == scaledWidth && origBounds.height == scaledHeight) {
        return orig;
    }

    ImageData origData = orig.getImageData();
    ImageData imData = new ImageData(scaledWidth, scaledHeight, origData.depth, origData.palette);
    if (origData.alphaData != null) {
        imData.alphaData = new byte[imData.width * imData.height];
        for (int row = 0; row < imData.height; row++) {
            for (int col = 0; col < imData.width; col++) {
                int origRow = row * origData.height / imData.height;
                int origCol = col * origData.width / imData.width;
                byte origAlpha = origData.alphaData[origRow * origData.width + origCol];
                imData.alphaData[row * imData.width + col] = origAlpha;
            }
        }
    }
    final Image scaled = new Image(device, imData);
    GC gc = new GC(scaled);
    gc.setAntialias(SWT.ON);
    gc.setInterpolation(SWT.HIGH);
    gc.setBackground(device.getSystemColor(SWT.COLOR_WHITE));
    gc.fillRectangle(0, 0, scaledWidth, scaledHeight);
    gc.drawImage(orig, 0, 0, origBounds.width, origBounds.height, 0, 0, scaledWidth, scaledHeight);
    gc.dispose();
    return scaled;
}
公共静态图像缩放图像(设备、图像源、int-scaledWidth、int-scaledHeight){
矩形origBounds=orig.getBounds();
if(origBounds.width==scaledWidth&&origBounds.height==scaledHeight){
返回原点;
}
ImageData origData=orig.getImageData();
ImageData imData=新的ImageData(缩放宽度、缩放高度、origData.depth、origData.palette);
if(origData.alphaData!=null){
imData.alphaData=新字节[imData.width*imData.height];
对于(int row=0;row
gc.setAlpha影响其以下修改,而不是图像的alpha数据。所以,这条线没有任何效果这会将整个画布上的alpha设置为透明gc.setAlpha(0);填充矩形(0,0,宽度,高度);你是否因为代码不起作用或者因为你提到的两行没有任何影响而否决了投票?哦,对不起。有一个问题我忘了写。若您使用“finalImage”创建GC,则该GC将具有每个像素的alpha值。因此,绘图将受每个像素的alpha值的影响。(尽管GC.getAlpha reutrns 255;)这意味着您无法在高级模式下绘制完全不透明的线(或矩形等)。我建议使用透明像素制作图像。看到这个:如果你因为我的错误而受到伤害,我真的很抱歉。没有造成伤害,但我仍然不能完全确定我发布的代码到底出了什么问题。在我的测试中,它按照OP的要求工作。另外,在这种情况下,透明像素将不起作用,因为OP需要一条反走样线,这条线与单个透明像素不起作用。下面是我提到的问题的一个步骤。1.使用alpha通道制作一个完全透明的32位PNG图像文件。2.使用该文件创建GC和映像实例。3.用GC填充矩形。4.在画布(或合成图等)上绘制图像注意:我的测试环境是Win32_x86。如果你在不同的环境下测试,结果可能会有所不同。如果是这样,请向我报告。这对标签很有效,但在ToolbarItem中,结果是一个纯黑色图像:(这在标签和ToolbarItems中都有效,很容易推广,并且支持绘图。这应该是公认的答案。