转自:Swing图片对象到SWT图片对象的转换(透明效果保持)
站在巨人的肩膀上才能看得更远。因此在SWT应用开发中,我们希望继承现有的基于Swing的丰富代码。转移过程需要的关注点不少,其中图片对象的转换必不可少。Swing的Image对象与SWT的Image对象不尽相同,但转换的办法还是有的。我参考过网上的一些实现代码,但是图标的透明处理效果总留遗憾。转换后,原本透明的部分变成了黑色,很是扎眼。我对相关代码进行了修改,解决了这一困扰。一点经验,与大家分享。
透明的实现关键是SWT ImageData对象的transparentPixel属性设置。它将特定的色素指定为透明。下面是完整的程序代码:
package com.example.draw;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.DirectColorModel;
import java.awt.image.ImageObserver;
import java.awt.image.WritableRaster;
import javax.swing.ImageIcon;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
/**
*
* Change javax.swing.ImageIcon object to org.eclipse.swt.graphics.Image object.
*
* @author Freshwind
*/
public class ImageConvertor {
public ImageConvertor() {
super();
}
/**
* change ImageIcon to BufferedImage
*
* @param icon
* @return
*/
public static BufferedImage getBufferedImage(ImageIcon icon) {
int width = icon.getIconWidth();
int height = icon.getIconHeight();
ImageObserver observer = icon.getImageObserver();
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics gc = bufferedImage.createGraphics();
gc.drawImage(icon.getImage(), 0, 0, observer);
return bufferedImage;
}
/**
* change BufferedImage to ImageData
*
* @param bufferedImage
* @return
*/
public static ImageData getImageData(BufferedImage bufferedImage) {
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);
int whitePixel = data.palette.getPixel(new RGB(255, 255, 255)); //将白色设定为透明色
WritableRaster raster = bufferedImage.getRaster();
int[] pixelArray = new int[3];
for (int y = 0; y < data.height; y++) {
for (int x = 0; x < data.width; x++) {
raster.getPixel(x, y, pixelArray);
int pixel = palette.getPixel(new RGB(pixelArray[0],
pixelArray[1], pixelArray[2]));
if (pixel == 0) //Swing读取PNG图形文件,透明的色素被置为0,所以格式转换是,将它变为白色(之前定义的透明色素)。
data.setPixel(x, y, whitePixel);
else
data.setPixel(x, y, pixel);
}
}
data.transparentPixel = whitePixel; //将白色指定为透明色素(同理将其它颜色定为透明色素也是可行的)。
return data;
}
public static void main(String[] args) {
ImageIcon icon = new ImageIcon(
"E:\\mypic\\picture.png");
BufferedImage bufferedImage = getBufferedImage(icon);
ImageData data = getImageData(bufferedImage);
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Image Convertor");
shell.setLayout(new FillLayout());
shell.setBackground(new Color(display, new RGB(255, 255, 255)));
shell.setSize(350, 100);
Label label = new Label(shell, SWT.NONE);
// new Image with ImageData which is generated previously.
Image image = new Image(display, data);
label.setImage(image);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}