免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3963 | 回复: 0
打印 上一主题 下一主题

【原创】Swing图片ImageIcon对象到SWT图片Image对象的转换 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-12-15 23:53 |只看该作者 |倒序浏览
看到这个标题也许会觉得很奇怪,有这个必要吗?
    答案是肯定的,说一种情况,比如代码复用,如果我曾经的项目中有一段程序是生成一个swing图片的,但是现在的界面要用swt实现了,我是不是应该将生成swing图片的代码改成生成swt图片,如果时间允许,无可厚非,但是很多情况下不仅是时间不允许,更有可能只能生成swing图片。
    下面转入正题,至于如何使用一段很长的代码产生一个swing图片就不说了,这里为了举例而举例,直接读入一个swing图片而后转换成swt图片,重要的是转换过程。

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);
        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]));
                data.setPixel(x, y, pixel);
            }
        }
        return data;
    }
   
    public static void main(String[] args){
        ImageIcon icon = new ImageIcon("C:\\temp\\freshwind.jpg");
        BufferedImage bufferedImage = getBufferedImage(icon);
        ImageData data = getImageData(bufferedImage);
        
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Image Convertor by Freshwind");
        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();
    }
}

更多原创访问我的blog
http://freshwind.cublog.cn/

freshwind.JPG (7.64 KB, 下载次数: 39)

示例图片

示例图片
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP