专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 图形/图像

SWT Image闪屏有关问题

发布时间:2011-06-27 20:28:39 文章来源:www.iduyao.cn 采编人员:星星草
SWT Image闪屏问题
转载:http://blog.csdn.net/chen09/article/details/2254198

Image Show by SWT
2008-04-06 02:34 548人阅读 评论(0) 收藏 举报
在做一个SWT的通用DB(jdbc)工具时,为了在主画面上加个logo,用上了俺家猫猫狗狗的照片。为了展示出这些照片,这个工具成了一个副产品。

SWT的image功能是俺以前没有碰过的,第一次用的时候碰到了不少问题。比如说闪屏,比如说淡入淡出的实现。

说实话,现在的淡入淡出感觉比较完美——前图的淡出和后图的淡入非常连贯。但是这也只是个偶然的bug造成的结果,并不是俺一开始就希望得到的结果,一开始,俺希望是白淡入,黑淡出,再白淡入。最后,黑淡出并没有实现,有些遗憾。

淡入淡出功能的实现,主要是设置imagedata的alphaData。研究alphaData的时候顺便也看一点mask的文档,用mask似乎可以实现不规则窗口,以后再好好研究一下。

闪屏的问题,一直想用双缓存去解决,最后画图用的canvas只要设置成SWT.NO_BACKGROUND就OK了,于是双缓存也没有去实现。其实,双缓存也试过,不知道是俺写得不对,还是别的什么原因,双缓存也不能解决非SWT.NO_BACKGROUND的闪屏问题。



package per.chenxin.test;

import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ImageTest {
    public ImageTest imageTest;
    public Display display;
    public Canvas canvas;
    Util util = null;
    ImageData imageData = null;
    byte[] alphaData = null;
    public int fade = 0;

//    public boolean bMouseFlg = false;
//    public int intMouseX = -1;
//    public int intMouseY = -1;
    FadeThread fadeThread = null;
    public boolean isFirst = true;

    public ImageTest() throws IOException {
        imageTest = this;
        util = Util.getInstance();
        imageData = new ImageData(util.getInputStream());
        alphaData = new byte[imageData.data.length];
        imageData.alphaData = alphaData;
    }

    public void open() {
        display = Display.getDefault();
        final Shell shell = new Shell();
        shell.addDisposeListener(new DisposeListener() {
            public void widgetDisposed(final DisposeEvent e) {
                try {
                    util.finalize();
                } catch (Throwable e1) {
                    e1.printStackTrace();
                }
            }
        });
        shell.setText("Image Show");
        //

        shell.open();

        canvas = new Canvas(shell, SWT.NO_BACKGROUND);
        canvas.addDisposeListener(new DisposeListener() {
            public void widgetDisposed(final DisposeEvent e) {
                fadeThread.isActive = false;
            }
        });
        canvas.setBackground(new Color(display, 0, 0, 0));

//        canvas.addMouseMoveListener(new MouseMoveListener() {
//            public void mouseMove(final MouseEvent e) {
//                if ((e.x - intMouseX) * (e.x - intMouseX) + (e.y - intMouseY)
//                        * (e.y - intMouseY) > 10) {
//                    canvas.redraw();
//                    bMouseFlg = true;
//                    intMouseX = e.x;
//                    intMouseY = e.y;
//                }
//
//            }
//        });
        canvas.addMouseListener(new MouseAdapter() {
            public void mouseDown(final MouseEvent e) {
                try {
                    if (!util.next())
                        util = Util.getInstance();
                    fadeThread.isActive = false;
                    fadeThread = new FadeThread(imageTest);
                    new Thread(fadeThread).start();
                    fade = 0;
                    imageData = new ImageData(util.getInputStream());
                    for (int i = 0; i < alphaData.length; i++)
                        alphaData[i] = (byte) fade;
                    imageData.alphaData = alphaData;
                    fade = 0;
//                    bMouseFlg = true;
                    canvas.redraw();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
        canvas.addPaintListener(new PaintListener() {
            public void paintControl(final PaintEvent e) {
                try {
                    if (imageData == null) {
                        imageData = new ImageData(util.getInputStream());
                        alphaData = new byte[imageData.data.length];
                        imageData.alphaData = alphaData;
                        fade = 0;
                    }
                    canvas.setSize(imageData.width, imageData.height);
                    shell.setSize(imageData.width, imageData.height);
                    if (isFirst) {
                        ImageData imageData2 = (ImageData) imageData.clone();
                        for (int i = 0; i < imageData2.data.length; i++) {
                            imageData2.data[i] = (byte) 255;
                            imageData2.alphaData[i] = (byte) 255;
                        }
                        Image image = new Image(display, imageData2);
                        e.gc.drawImage(image, 0, 0);

                        isFirst = false;
                    } else {
                        Image image = new Image(display, imageData);
                        e.gc.drawImage(image, 0, 0);
                    }

//                    if (bMouseFlg) {
//                        e.gc.drawOval(intMouseX - 50, intMouseY - 50, 100, 100);
//                        bMouseFlg = false;
//                    }

                } catch (IOException e1) {
                    e1.printStackTrace();
                }

            }
        });
        canvas.setBounds(0, 0, imageData.width, imageData.height);
        shell.layout();

        fadeThread = new FadeThread(this);
        new Thread(fadeThread).start();

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

    /**
     * Launch the application
     *
     * @param args
     */
    public static void main(String[] args) {
        try {
            (new ImageTest()).open();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    static class FadeThread implements Runnable {
        ImageTest imageTest;
        public boolean isActive = false;

        public FadeThread(ImageTest imageTest) {
            this.imageTest = imageTest;
        }

        public void run() {
            isActive = true;
            while (imageTest.fade <= 255) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                }
                if (imageTest.isFirst)
                    continue;
                if (!isActive)
                    break;
                for (int i = 0; i < imageTest.alphaData.length; i++)
                    imageTest.alphaData[i] = (byte) imageTest.fade;
//                if (imageTest.intMouseX >= 0 || imageTest.intMouseY >= 0) {
//                    imageTest.bMouseFlg = true;
//                }
                imageTest.display.asyncExec(new Runnable() {
                    public void run() {
                        imageTest.canvas.redraw();
                    }
                });
                imageTest.fade += 5;
            }
            isActive = false;
        }
    }

}



package per.chenxin.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Util {
    private static boolean isInited = false;
    private static Util util;
    private static String STR_IMAGE = "IMG_0328.zip";

    public byte[] bytesZipInputStream = null;
    public String strZipEntryName = null;

    private ZipInputStream zipInputStream = null;
    private ZipEntry zipEntry = null;

    private Util() throws IOException {
        zipInputStream = new ZipInputStream(new FileInputStream(STR_IMAGE));
        zipEntry = zipInputStream.getNextEntry();
        strZipEntryName = zipEntry.getName();
        zipEntry = zipInputStream.getNextEntry();
        if (zipEntry != null)
            strZipEntryName = zipEntry.getName();
    }

    public InputStream getInputStream() throws IOException {
        while (zipInputStream.available() == 0)
            next();
        return zipInputStream;
    }

    public boolean next() throws IOException {
        zipEntry = zipInputStream.getNextEntry();
        if (zipEntry == null) {
            try {
                finalize();
            } catch (Throwable e) {
                e.printStackTrace();
            }
            isInited = false;
            init();
            return false;
        } else
            strZipEntryName = zipEntry.getName();
        return true;
    }

    public static Util getInstance() throws IOException {
        Util.init();
        return util;
    }

    public static void init() throws IOException {

        if (!isInited) {
            util = new Util();
            isInited = true;
        }
    }

    static {
        try {
            init();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected void finalize() throws Throwable {
        if (zipInputStream != null) {
            if (zipEntry != null)
                zipInputStream.closeEntry();
            zipInputStream.close();
        }
    }

    public byte[] getBytesZipInputStream() throws IOException {
        bytesZipInputStream = new byte[zipInputStream.available()];
        zipInputStream.read(bytesZipInputStream);
        return bytesZipInputStream;
    }

    public String getStrZipEntryName() {
        return strZipEntryName;
    }
}

上面代码被注释掉的部分是在mouse移动时,在mouse处加个圆圈,是用来实现放大镜功能的。但是imagedata的构造还没有完全搞清楚,放大镜功能如何实现,也想到网上找找资料,所以先注释掉了。
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: