SWT 创建不规则Shell 电子表示例.

代码如下:

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Transform;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.PaintEvent;

import java.util.TimerTask;
import java.util.Timer;
import java.util.Calendar;

/**
 * Created by IntelliJ IDEA.
 * User: yaojianzhong
 * Date: 2009-4-27
 * Time: 10:38:28

* 电子表显示器
 */
public class CTimer extends Composite implements PaintListener {
    private int offset = 1;
    private int margin = 0;
    private int top_length = 14;
    private int spacing = top_length / 2 * 3;
    private int trim_side = top_length / 7;
    private int left_right_height = top_length * 5 / 7;
    private int spliter_width = 5;  //default
    private Calendar now = Calendar.getInstance();
    private Color background;

    /**
     * @param composite 父容器
     * @param type      样式#SWT
     */
    public CTimer(Composite composite, int type) {
        super(composite, type);
        this.addPaintListener(this);
        TimerTaskImpl task = new TimerTaskImpl(this);
        Timer timer = new Timer(true);
        timer.schedule(task, 1l, 1000l);//每秒执行一次
    }

    /**
     * @param composite     父容器
     * @param type          窗口样式类型#SWT
     * @param offset        数字组成部分的间距
     * @param margin        数字与上与左的距离
     * @param top_length    数字宽度
     * @param spliter_width 时间分隔点的边长
     */
    public CTimer(Composite composite, int type, int offset, int margin, int top_length, int spliter_width) {
        super(composite, type);
        this.offset = offset;
        this.margin = margin;
        this.top_length = top_length;
        this.spliter_width = spliter_width;
    }

    public CTimer(Composite composite, int i, Color background, int spliter_width, int top_length, int margin, int offset) {
        super(composite, i);
        this.background = background;
        this.spliter_width = spliter_width;
        this.top_length = top_length;
        this.margin = margin;
        this.offset = offset;
    }

    public void paintControl(PaintEvent event) {
        GC gc = event.gc;
        if (background == null)
            gc.setBackground(new Color(Display.getDefault(),50,50,50));
        else
            gc.setBackground(background);
        Transform translate = new Transform(Display.getDefault());
        drawTime(gc, translate, now.get(Calendar.HOUR_OF_DAY));//时
        drawSpliter(gc, translate);
        drawTime(gc, translate, now.get(Calendar.MINUTE));//画分
        drawSpliter(gc, translate);
        drawTime(gc, translate, now.get(Calendar.SECOND));//画秒
    }

    //根据时间段画时间
    private void drawTime(GC gc, Transform translate, int timeSection) {

        if (timeSection == 60) {
            paint(6, gc);
            translate.translate(spacing, 0);
            gc.setTransform(translate);
            paint(0, gc);
        } else {
            paint(timeSection / 10, gc);
            translate.translate(spacing, 0);
            gc.setTransform(translate);
            paint(timeSection % 10, gc);
        }
    }

    private void drawSpliter(GC gc, Transform translate) {
        translate.translate(spacing, 0);
        gc.setTransform(translate);
        int number_height = left_right_height * 2 + 2 * offset + 2 * trim_side;//数字高度
        int number_width = top_length + 2 * offset;
        int x = (number_width - spliter_width) / 2;
        int percent = (number_height - 2 * spliter_width) / 3;

        gc.fillRectangle(x, percent, spliter_width, spliter_width);
        gc.fillRectangle(x, 2 * percent + spliter_width, spliter_width, spliter_width);
        translate.translate(spacing, 0);
        gc.setTransform(translate);
    }

    private void drawTop(GC gc) {
        gc.fillPolygon(new int[]{margin + offset, margin, margin + top_length + offset, margin, margin + top_length - 2 * trim_side + offset, margin + trim_side * 2, margin + trim_side * 2 + offset, margin + trim_side * 2});
    }

    private void drawLeftTop(GC gc) {
        gc.fillPolygon(new int[]{margin, margin + offset, margin, margin + left_right_height + offset, margin + trim_side, margin + left_right_height + trim_side + offset, margin + trim_side * 2, margin + left_right_height + offset, margin + trim_side * 2, margin + trim_side * 2 + offset});
    }

    private void drawLeftButtom(GC gc) {
        int transform = margin + left_right_height + trim_side + offset;
        gc.fillPolygon(new int[]{margin + trim_side, transform + offset * 2, margin, transform + offset * 2 + trim_side, margin, transform + offset * 2 + trim_side + left_right_height, margin + trim_side * 2, transform + left_right_height + offset * 2 - trim_side, margin + trim_side * 2, transform + trim_side + offset * 2});
    }

    private void drawMiddle(GC gc) {
        gc.fillPolygon(new int[]{margin + trim_side + offset, margin + trim_side + left_right_height + 2 * offset, margin + trim_side * 2 + offset, margin + left_right_height + 2 * offset, margin + top_length - trim_side * 2 + offset, margin + left_right_height + 2 * offset, margin + top_length + offset - trim_side, margin + trim_side + left_right_height + 2 * offset, margin + top_length - trim_side * 2 + offset, margin + trim_side * 2 + left_right_height + 2 * offset, margin + trim_side * 2 + offset, margin + left_right_height + 2 * offset + 2 * trim_side});
    }

    private void drawButtom(GC gc) {
        gc.fillPolygon(new int[]{margin + offset, margin + left_right_height * 2 + offset * 4 + trim_side * 2, margin + top_length + offset, margin + left_right_height * 2 + offset * 4 + trim_side * 2, margin + top_length - 2 * trim_side + offset, margin + 2 * left_right_height + 4 * offset, margin + trim_side * 2 + offset, margin + 2 * left_right_height + 4 * offset});
    }

    private void drawRightTop(GC gc) {
        gc.fillPolygon(new int[]{margin + 2 * offset + top_length, margin + offset, margin + 2 * offset + top_length, margin + left_right_height + offset, margin + trim_side + 2 * offset + top_length - 2 * trim_side, margin + left_right_height + trim_side + offset, margin + trim_side * 2 + 2 * offset + top_length - 4 * trim_side, margin + left_right_height + offset, margin + trim_side * 2 + 2 * offset + top_length - 4 * trim_side, margin + trim_side * 2 + offset});
    }

    private void drawRightButtom(GC gc) {
        int transform = margin + left_right_height + trim_side + offset;
        gc.fillPolygon(new int[]{margin + trim_side + 2 * offset + top_length - 2 * trim_side, transform + offset * 2, margin + 2 * offset + top_length, transform + offset * 2 + trim_side, margin + 2 * offset + top_length, transform + offset * 2 + trim_side + left_right_height, margin + trim_side * 2 + 2 * offset + top_length - 4 * trim_side, transform + left_right_height + offset * 2 - trim_side, margin + trim_side * 2 + 2 * offset + top_length - 4 * trim_side, transform + trim_side + offset * 2});
    }

    //画1
    private void paintOne(GC gc) {
        this.drawRightTop(gc);
        this.drawRightButtom(gc);
    }

    //画2
    private void paintTwo(GC gc) {
        this.drawTop(gc);
        this.drawRightTop(gc);
        this.drawMiddle(gc);
        this.drawLeftButtom(gc);
        this.drawButtom(gc);
    }

    //画3
    private void paintThree(GC gc) {
        this.drawTop(gc);
        this.drawRightTop(gc);
        this.drawMiddle(gc);
        this.drawRightButtom(gc);
        this.drawButtom(gc);
    }

    //画4
    private void paintFour(GC gc) {
        this.drawLeftTop(gc);
        this.drawMiddle(gc);
        this.drawRightTop(gc);
        this.drawRightButtom(gc);
    }

    //画5
    private void paintFive(GC gc) {
        this.drawTop(gc);
        this.drawLeftTop(gc);
        this.drawMiddle(gc);
        this.drawRightButtom(gc);
        this.drawButtom(gc);
    }

    //画6
    private void paintSix(GC gc) {
        this.drawTop(gc);
        this.drawLeftTop(gc);
        this.drawMiddle(gc);
        this.drawRightButtom(gc);
        this.drawButtom(gc);
        this.drawLeftButtom(gc);
    }

    //画7
    private void paintSeven(GC gc) {
        this.drawTop(gc);
        this.drawRightButtom(gc);
        this.drawRightTop(gc);
    }

    //画8
    private void paintNight(GC gc) {
        this.drawButtom(gc);
        this.drawLeftButtom(gc);
        this.drawLeftTop(gc);
        this.drawMiddle(gc);
        this.drawRightButtom(gc);
        this.drawRightTop(gc);
        this.drawTop(gc);
    }

    //画9
    private void paintNine(GC gc) {
        this.drawButtom(gc);
        this.drawLeftTop(gc);
        this.drawMiddle(gc);
        this.drawRightButtom(gc);
        this.drawRightTop(gc);
        this.drawTop(gc);
    }

    //画10
    private void paintZero(GC gc) {
        this.drawButtom(gc);
        this.drawLeftButtom(gc);
        this.drawLeftTop(gc);
        this.drawRightButtom(gc);
        this.drawRightTop(gc);
        this.drawTop(gc);
    }

    private void paint(int i, GC gc) {
        switch (i) {
            case 0:
                this.paintZero(gc);
                break;
            case 1:
                this.paintOne(gc);
                break;
            case 2:
                this.paintTwo(gc);
                break;
            case 3:
                this.paintThree(gc);
                break;
            case 4:
                this.paintFour(gc);
                break;
            case 5:
                this.paintFive(gc);
                break;
            case 6:
                this.paintSix(gc);
                break;
            case 7:
                this.paintSeven(gc);
                break;
            case 8:
                this.paintNight(gc);
                break;
            case 9:
                this.paintNine(gc);
                break;
        }
    }

    public Calendar getNow() {
        return now;
    }

    public void setNow(Calendar now) {
        this.now = now;
        Display.getDefault().asyncExec(new Runnable() {

            public void run() {
                redraw();
            }
        });

    }

    class TimerTaskImpl extends TimerTask {
        CTimer timer;

        TimerTaskImpl(CTimer timer) {
            this.timer = timer;
        }

        public void run() {
            timer.setNow(Calendar.getInstance());
        }
    }
}

 

 

 

Main类

import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;

import java.io.*;
import java.net.URL;
import java.net.URISyntaxException;

/**
 * Created by IntelliJ IDEA.
 * User: yaojianzhong
 * Date: 2009-4-28
 * Time: 9:35:54
 */
public class NoTrimApplication {
    public static Shell shell = null;
    public static Image image;
    public static ImageData imageData;

    public static void main(String[] args) throws IOException, URISyntaxException {
        Display display = new Display();
        shell = new Shell(display, SWT.NO_TRIM);
        shell.setText("制作人:姚建忠");
        URL path = NoTrimApplication.class.getResource("app.png");
        URL maskPath = NoTrimApplication.class.getResource("mask.png");
        imageData = new ImageData(path.openStream());
        ImageData mask = new ImageData(maskPath.openStream());
        image = new Image(display, imageData, mask);
        Region region = new Region();
        ImageData data = image.getImageData().getTransparencyMask();
        for (int i = 0; i < data.width; i++) {
            for (int j = 0; j < data.height; j++) {
                if (data.getPixel(i, j) != 0)
                    region.add(i + data.x, j + data.y, 1, 1);
            }
        }
        shell.setRegion(region);
        shell.setSize(imageData.width + imageData.x, imageData.height + imageData.y);
        Listener listener = new Listener() {
            int startX
                    ,
                    startY;

            public void handleEvent(Event e) {
                // 注入鼠标事件
                if (e.type == SWT.MouseDown && e.button == 1) {
                    startX = e.x;  //组件相对位置
                    startY = e.y;
                }
                if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) {
                    Point p = shell.toDisplay(e.x, e.y);   //转换为屏幕相对位置
                    p.x -= startX;
                    p.y -= startY;
                    shell.setLocation(p);
                }
                if (e.type == SWT.Paint) {
                    e.gc.drawImage(image, 0, 0);
                }
            }
        };

        shell.addListener(SWT.KeyDown, listener);
        shell.addListener(SWT.MouseDown, listener);
        shell.addListener(SWT.MouseMove, listener);
        shell.addListener(SWT.Paint, listener);
        shell.setLayout(null);
        CTimer timer = new CTimer(shell, SWT.NONE);
        timer.setBackground(new Color(display, 196, 216, 214));
        timer.setBounds(imageData.x + 130, imageData.y + 325, 170, 40);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值