SWT实现透明背景代码

本文介绍如何使用SWT库在Java中绘制带有渐进Alpha通道的图像,并通过添加自定义paintListener来实现每次绘制时图像的更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
public class Talpha {
        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                final Display display = new Display();
                final Shell shell = new Shell(display);
                Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND);
                //使用paintlistener,保证每次均重新绘制。
                canvas.addPaintListener(new PaintListener() {
                        public void paintControl(PaintEvent e) {
                                GC gc = e.gc;
                        //读图像
                        ImageData imageData = new ImageData("t.jpg");
                                 //这里是建立从左到右的渐进Alpha。
                   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;
                                Image image = new Image(display, imageData);
                         //绘制                               
                        gc.drawImage(image,0,0);
                        }
                });
                FillLayout fillLayout = new FillLayout();
                fillLayout.type = SWT.VERTICAL;
                shell.setLayout(fillLayout);
                shell.setSize(200, 600);
                shell.open();
                while (!shell.isDisposed()) {
                        if (!display.readAndDispatch())
                                display.sleep();
                }
                display.dispose();
        }
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值