SWT(JFace)体验之图片的动态渐变效果

 更新时间:2009年06月25日 12:05:57   作者:  
SWT(JFace)体验之图片的动态渐变效果
1.渐变:
复制代码 代码如下:

package swt_jface.demo10;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class AlphaFadeIn {
    Display display = new Display();
    Shell shell = new Shell(display);
    public AlphaFadeIn() {

        shell.setLayout(new FillLayout());
        final Canvas canvas = new Canvas(shell, SWT.NULL);

        ImageData imageData = new ImageData("C:/icons/eclipse.jpg");
        byte[] alphaValues = new byte[imageData.height * imageData.width];
        for(int j=0; j<imageData.height; j++) {
            for(int i=0; i<imageData.width; i++) {
                alphaValues[j*imageData.width + i] = (byte) (255 - 255 * i / imageData.width);
            }
        }
        imageData.alphaData = alphaValues;

        final Image image = new Image(display, imageData);

        canvas.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                e.gc.drawImage(image, 10, 10);
            }
        });
        shell.setSize(200, 100);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
    public static void main(String[] args) {
        new AlphaFadeIn();
    }
}

2.动态
复制代码 代码如下:

package swt_jface.demo10;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Animations {

    Display display = new Display();
    Shell shell = new Shell(display);
    public Animations() {

        shell.setLayout(new FillLayout());
        ImageLoader imageLoader = new ImageLoader();
        final ImageData[] imageDatas = imageLoader.load("C:/icons/eclipse-ani.gif");
        final Image image = new Image(display, imageDatas[0].width, imageDatas[0].height);
        final Canvas canvas = new Canvas(shell, SWT.NULL);
        canvas.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                e.gc.drawImage(image, 0, 0);
            }
        });
        final GC gc = new GC(image);
        final Thread thread = new Thread() {
            int frameIndex = 0;
            public void run() {
                while (!isInterrupted()) {
                    frameIndex %= imageDatas.length;
                    final ImageData frameData = imageDatas[frameIndex];
                    display.asyncExec(new Runnable() {
                        public void run() {
                            Image frame =
                                new Image(display, frameData);
                            gc.drawImage(frame, frameData.x, frameData.y);
                            frame.dispose();
                            canvas.redraw();
                        }
                    });
                    try {
                        Thread.sleep(imageDatas[frameIndex].delayTime * 10);
                    } catch (InterruptedException e) {
                        return;
                    }
                    frameIndex += 1;
                }
            }
        };

        shell.addShellListener(new ShellAdapter() {
            public void shellClosed(ShellEvent e) {
                thread.interrupt();
            }
        });
        shell.setSize(400, 200);
        shell.open();

        thread.start();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
    public static void main(String[] args) {
        new Animations();
    }
}

相关文章

  • Springboot使用RestTemplate调用第三方接口的操作代码

    Springboot使用RestTemplate调用第三方接口的操作代码

    这篇文章主要介绍了Springboot使用RestTemplate调用第三方接口,我只演示了最常使用的请求方式get、post的简单使用方法,当然RestTemplate的功能还有很多,感兴趣的朋友可以参考RestTemplate源码
    2022-12-12
  • Spring Aop如何给Advice传递参数

    Spring Aop如何给Advice传递参数

    这篇文章主要介绍了Spring Aop如何给Advice传递参数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • java基于线程池和反射机制实现定时任务完整实例

    java基于线程池和反射机制实现定时任务完整实例

    这篇文章主要介绍了java基于线程池和反射机制实现定时任务的方法,以完整实例形式较为详细的分析了Java定时任务的功能原理与实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • SpringBoot中REST API 接口传参的实现

    SpringBoot中REST API 接口传参的实现

    我们在开发 REST API 的过程中,经常需要传递参数,本文主要介绍了SpringBoot中REST API 接口传参的实现,具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12
  • Java日期接收报错:could not be parsed, unparsed text found at index 10解决办法

    Java日期接收报错:could not be parsed, unparsed text found a

    在做Java开发时肯定会碰到传递时间参数的情况,这篇文章主要给大家介绍了关于Java日期接收报错:could not be parsed, unparsed text found at index 10的解决办法,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • 浅谈线程通信wait,notify作用

    浅谈线程通信wait,notify作用

    这篇文章主要介绍了浅谈线程通信wait,notify作用,具有一定借鉴价值,需要的朋友可以参考下
    2017-12-12
  • Spring之@Lookup注解详细解析

    Spring之@Lookup注解详细解析

    这篇文章主要介绍了Spring之@Lookup注解详细解析,当采用@Autowired注解对单例bean注依赖的原型bean时,会由于单例bean只会创建一次,导致依赖的原型bean也只会注入一次,@Lookup注解可以较为优雅的解决此类问题,需要的朋友可以参考下
    2024-01-01
  • 为什么说HashMap线程不安全

    为什么说HashMap线程不安全

    本文主要介绍了为什么说HashMap线程不安全,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • Springboot使用redisson实现分布式锁的代码示例

    Springboot使用redisson实现分布式锁的代码示例

    在实际项目中,某些场景下可能需要使用到分布式锁功能,那么实现分布式锁有多种方式,常见的如mysql分布式锁、zookeeper分布式锁、redis分布式锁,本文介绍springboot如何使用redisson实现分布式锁,需要的朋友可以参考下
    2023-06-06
  • Java编程伪共享与缓存行填充

    Java编程伪共享与缓存行填充

    这篇文章主要介绍了Java编程伪共享与缓存行填充,下面文章Disruptor提到的CPU缓存话题,做了一些尝试和研究,如Disruptor所说,CPU有缓存伪共享的问题,并且通过缓存行填充能完美的解决这个问题,需要的朋友可以参考一下
    2021-09-09

最新评论