一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Image Show by SWT

时间:2008-04-28 编辑:简简单单 来源:一聚教程网

在做一个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, 000));

//        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] 
= (byte255;
                            imageData2.alphaData[i] 
= (byte255;
                        }

                        Image image 
= new Image(display, imageData2);
                        e.gc.drawImage(image, 
00);

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


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

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


            }

        }
);
        canvas.setBounds(
00, 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();
     

热门栏目