Java Swing Loading转圈的进度提示框

Java Swing Loading转圈的进度提示框

 

前因:我们开发的web应用,有个奇葩的需求,需要客户触发到客户端与第三方交互的地方加个Loading效果,已经脱离了web程序页面交互范围了,只是通过socket进行通信交互,就产生了swing 画个转圈圈的想法是不是很奇葩。

总是觉得Java Swing没有Android的好,不能自定义组件,实现漂亮的进度提示框,比如那种转圈的,谷歌了一下竟然发现有大牛实现了
 

类似的额效果:

在这里插入图片描述

具体只需要两个类

AnimatedPanel.java

// 展示部分代码
synchronized (convolvedImage) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

            FontRenderContext context = g2.getFontRenderContext();
            TextLayout layout = new TextLayout(message, font, context);
            Rectangle2D bounds = layout.getBounds();

            int x = (width - convolvedImage.getWidth(null)) / 2;
            int y = (int) (height - (convolvedImage.getHeight(null) + bounds.getHeight() + layout.getAscent())) / 2;

            g2.drawImage(convolvedImage, x, y, this);
            g2.setColor(new Color(0, 0, 0, (int) (gradient * 255)));
            layout.draw(g2, (float) (width - bounds.getWidth()) / 2,
                    (float) (y + convolvedImage.getHeight(null) + bounds.getHeight() + layout.getAscent()));
        }
......//
 

InfiniteProgressPanel.java

// 展示部分代码
public void paintComponent(Graphics g) {
    if (started) {
        int width = getWidth();
        int height = getHeight();

        double maxY = 0.0;

        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHints(hints);

        g2.setColor(new Color(255, 255, 255, (int) (alphaLevel * shield)));
        g2.fillRect(0, 0, getWidth(), getHeight());

        for (int i = 0; i < ticker.length; i++) {
            int channel = 224 - 128 / (i + 1);
            g2.setColor(new Color(channel, channel, channel, alphaLevel));
            g2.fill(ticker[i]);

            Rectangle2D bounds = ticker[i].getBounds2D();
            if (bounds.getMaxY() > maxY) {
                maxY = bounds.getMaxY();
            }
        }

        if (text != null && text.length() > 0) {
            FontRenderContext context = g2.getFontRenderContext();
            TextLayout layout = new TextLayout(text, getFont(), context);
            Rectangle2D bounds = layout.getBounds();
            g2.setColor(getForeground());
            layout.draw(g2, (float) (width - bounds.getWidth()) / 2,
                    (float) (maxY + layout.getLeading() + 2 * layout.getAscent()));
        }
    }
}

如何在你的Java Swing使用呢,如下步骤:

JFrame frame = new JFrame();
// ...
InfiniteProgressPanel glasspane = new InfiniteProgressPanel();
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    glasspane.setBounds(100, 100, (dimension.width) / 2, (dimension.height) / 2);
frame.setGlassPane(glasspane);
glasspane.start();//开始动画加载效果
frame.setVisible(true);

// Later, to disable,在合适的地方关闭动画效果
glasspane.stop();

最终效果:
在这里插入图片描述

可以添加订阅号:码猿趣事

 

回覆: “loading-demo” 下载用例即可

posted @ 2024-07-20 16:23  去你的鸟命  阅读(49)  评论(0编辑  收藏  举报