Java用SWT画一颗电池并动态显示电量SOC

 效果:

package test;
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class AnimatedRoundedBatteryIcon {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Animated Rounded Battery Icon");
        shell.setSize(250, 400);
        shell.setLayout(new FillLayout());

        Canvas canvas = new Canvas(shell, SWT.NONE);

        // 初始模拟电量值
        final int[] batteryLevel = { 30 };

        // 添加绘制监听器
        canvas.addPaintListener(e -> drawVerticalBattery(e.gc, batteryLevel[0]));

        // 定时更新电量并重绘(模拟动画)
        // 定义定时任务(使用Lambda自引用实现循环)
        Runnable timerTask = new Runnable() {
            @Override
            public void run() {
                batteryLevel[0] = (batteryLevel[0] + 5) % 101; // 循环变化
                canvas.redraw();
                display.timerExec(1000, this); // 再次注册自己
            }
        };

        display.timerExec(1000, timerTask); // 启动定时器

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

    /**
     * 绘制带圆角、电量数字和颜色渐变的竖向电池
     *
     * @param gc   Graphics Context
     * @param level 电量百分比 (0 - 100)
     */
    private static void drawVerticalBattery(GC gc, int level) {
        // 电池整体尺寸
        int batteryWidth = 80;
        int batteryHeight = 240;
        int x = 85;
        int y = 40;

        // 正极头部尺寸
        int headWidth = 25;
        int headHeight = 10;

        // 内部电量区域
        int padding = 6;
        int innerWidth = batteryWidth - 2 * padding;
        int innerHeight = (batteryHeight - 2 * padding) * level / 100;
        int innerY = y + batteryHeight - padding - innerHeight;
        int innerX = x + padding;

        // 圆角半径
        int cornerRadius = 15;

        // 绘制电池外框(圆角)
        gc.setLineWidth(2);
        gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
        gc.drawRoundRectangle(x, y, batteryWidth, batteryHeight, cornerRadius, cornerRadius);

        // 绘制顶部正极头
        gc.fillRectangle(x + (batteryWidth - headWidth) / 2, y - headHeight, headWidth, headHeight);
        gc.drawRectangle(x + (batteryWidth - headWidth) / 2, y - headHeight, headWidth, headHeight);

        // 填充电量部分(从下往上填充)
        Color fillColor = level > 20 ?
                Display.getCurrent().getSystemColor(SWT.COLOR_GREEN) :
                Display.getCurrent().getSystemColor(SWT.COLOR_RED);
        gc.setBackground(fillColor);
        gc.fillRoundRectangle(innerX, innerY, innerWidth, innerHeight, 8, 8);

        // 边框
        gc.drawRoundRectangle(innerX - 1, innerY - 1, innerWidth + 2, innerHeight + 2, 8, 8);

        // 显示电量百分比文字
        Font font = new Font(Display.getCurrent(), "Arial", 16, SWT.BOLD);
        gc.setFont(font);
        String text = level + "%";
        Point textSize = gc.textExtent(text);
        int textX = x + batteryWidth / 2 - textSize.x / 2;
        int textY = y + batteryHeight / 2 - textSize.y / 2;
        gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY));
        gc.drawText(text, textX, textY, true);
        font.dispose();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值