SWT获取System文件夹图像?

时间:2016-07-20 05:33:01

标签: java image swt

我刚刚使用SWT完成了我的新程序,但我想添加一些东西。我有一个带有一些文件和文件夹的树。

我使用了Program类的SWT来获取文件的系统映像,但我不知道如何获取文件夹的映像。

您知道一种获取系统用于每个文件夹的图像的方法吗?

1 个答案:

答案 0 :(得分:1)

根据SO(How to display system icon for a file in SWT?)的其他答案,您可以这样做:

private static Display display;

public static void main(String[] args)
{
    display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Stackoverflow");
    shell.setLayout(new FillLayout());
    shell.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
    shell.setBackgroundMode(SWT.INHERIT_FORCE);

    /* Folder icon */
    Label folder = new Label(shell, SWT.CENTER);
    folder.setImage(getImage(new File(System.getProperty("user.home")).getParentFile()));

    /* File icon */
    Program p = Program.findProgram("txt");
    Image i = new Image(display, p.getImageData());
    Label txtFile = new Label(shell, SWT.CENTER);
    txtFile.setImage(i);

    shell.pack();
    shell.open();

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

}

private static Image getImage(File file)
{
    ImageIcon systemIcon = (ImageIcon) FileSystemView.getFileSystemView().getSystemIcon(file);
    java.awt.Image image = systemIcon.getImage();
    if (image instanceof BufferedImage)
    {
        return new Image(display, convertToSWT((BufferedImage) image));
    }
    int width = image.getWidth(null);
    int height = image.getHeight(null);
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bufferedImage.createGraphics();
    g2d.drawImage(image, 0, 0, null);
    g2d.dispose();
    return new Image(display, convertToSWT(bufferedImage));
}

private static ImageData convertToSWT(BufferedImage bufferedImage)
{
    if (bufferedImage.getColorModel() instanceof DirectColorModel)
    {
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(), colorModel.getBlueMask());
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette);
        for (int y = 0; y < data.height; y++)
        {
            for (int x = 0; x < data.width; x++)
            {
                int rgb = bufferedImage.getRGB(x, y);
                int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF));
                data.setPixel(x, y, pixel);
                if (colorModel.hasAlpha())
                {
                    data.setAlpha(x, y, (rgb >> 24) & 0xFF);
                }
            }
        }
        return data;
    }
    else if (bufferedImage.getColorModel() instanceof IndexColorModel)
    {
        IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
        int size = colorModel.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        colorModel.getReds(reds);
        colorModel.getGreens(greens);
        colorModel.getBlues(blues);
        RGB[] rgbs = new RGB[size];
        for (int i = 0; i < rgbs.length; i++)
        {
            rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
        }
        PaletteData palette = new PaletteData(rgbs);
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette);
        data.transparentPixel = colorModel.getTransparentPixel();
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++)
        {
            for (int x = 0; x < data.width; x++)
            {
                raster.getPixel(x, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}

这将显示文件夹图标(使用链接方法)和文件图标(使用Program方法)。

看起来像这样:

enter image description here