JFrame显示透明图片与动态图片

一、介绍

最近发现桌面上显示的透明图片和动态图片看起来不错,于是研究了一下java能否做到这一点。使用JFrame基本是可以实现的,但是还有些缺陷。其中,核心语句是

jframe.setBackground(new Color(0,0,0,0));
jPanel.setBackground(new Color(0,0,0,0));

这两句都写上,才能实现透明图片的效果。

//网上说这句也可以:AWTUtilities.setWindowOpaque(frame, false);
//不过我加上不行,是jdk1.8不支持吗?还是win10下不支持呢?

直接上截图与代码了:

1.透明图片:

JFrame显示透明图片与动态图片_第1张图片

2.动态图片(gif):

JFrame显示透明图片与动态图片_第2张图片

3.有问题的动态图片(本来应该是雪花效果的,要不不透明,要不就长这样,不会调了):

二、代码

1.透明图片

import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class noBankImg {
    public static void main(String[] args) {
        new noBankImg();
    }
    public noBankImg(){
        start();
    }

    public static void start() {
        final JFrame frame = new JFrame();
        //设置在最顶层
        frame.setAlwaysOnTop(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置隐藏标题栏与无法移动
        frame.setUndecorated(true);
        //禁止改变大小
        frame.setResizable(false);
        //设置出现位置
        frame.setLocation(frame.getToolkit().getScreenSize().width-300, frame.getToolkit().getScreenSize().height-350);
        //设置大小
        frame.setSize(300, 300);

        //这句不能用,无效,不兼容
        //AWTUtilities.setWindowOpaque(frame, false);
        //设置背景透明
        frame.setBackground(new Color(0,0,0,0));

        final JPanel pane = new JPanel() {
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                Graphics2D g2d = (Graphics2D) g;
                InputStream imgIS = this.getClass().getResourceAsStream("t3.png");
                Image image = null;
                try {
                    image = ImageIO.read(imgIS);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                g2d.drawImage(image, 0, 0,300,300, null);

            }
        };

        //设置背景透明
        pane.setBackground(new Color(0,0,0,0));
        frame.setContentPane(pane);

        //设置完毕后再显示
        frame.setVisible(true);

    }
}

2.动态图片(图片原本是gif的,不过重命名成jpg也不影响)

import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

//动图

public class t1 extends JFrame {
    public t1() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setSize(400, 300);
        //设置在最顶层
        setAlwaysOnTop(true);
        //设置出现位置
        setLocation(getToolkit().getScreenSize().width-400, getToolkit().getScreenSize().height-350);
        //设置隐藏标题栏与无法移动
        setUndecorated(true);
        //禁止改变大小
        setResizable(false);
        getContentPane().setLayout(null);
        JPanel panel = new ImagePanel();
        panel.setBounds(0, 0, 400, 300);

        //设置背景透明
        setBackground(new Color(0,0,0,0));
        //panel.setBackground(new Color(0,0,0,0));


        getContentPane().add(panel);
        setVisible(true);
    }

    public static void main(String[] args) {
        new t1();
    }

    class ImagePanel extends JPanel {
        public void paint(Graphics g) {
            super.paint(g);
            ImageIcon icon = new ImageIcon(this.getClass().getResource("t5.jpg"));
            g.drawImage(icon.getImage(), 0, 0, 400, 300, this);
        }
    }
}

3.有问题的动态图片

import com.sun.awt.AWTUtilities;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;

//pane.setBackground(new Color(0,0,0,0));
//AWTUtilities.setWindowOpaque(frame, false);

public class t3 {

    public static void main(String[] args) {
        new t3();
    }
    public t3(){
        start();
    }

    public static void start() {
        final JFrame frame = new JFrame();

        frame.setAlwaysOnTop(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        //这句不能用,无效,不兼容
        //AWTUtilities.setWindowOpaque(frame, false);
        frame.setBackground(new Color(0,0,0,0));

        final JPanel pane = new JPanel() {

            private int[] snowX = null;
            private int[] snowY = null;
            private int[] angles = null;
            private int count = 50;

            @Override
            public void paint(Graphics g) {
                super.paint(g);
                Rectangle bounds = frame.getBounds();
                if (snowX == null) {

                    snowX = new int[count];
                    for (int i = 0; i < snowX.length; i++) {
                        snowX[i] = getRandomInt(bounds.width);
                    }
                    snowY = new int[count];
                    for (int i = 0; i < snowY.length; i++) {
                        snowY[i] = getRandomInt(bounds.height);
                    }
                    angles = new int[count];
                    for (int i = 0; i < snowY.length; i++) {
                        angles[i] = getRandomInt(360);
                    }
                }

                Graphics2D g2d = (Graphics2D) g;


                InputStream imgIS = this.getClass().getResourceAsStream("t3.png");
                Image image = null;
                try {
                    image = ImageIO.read(imgIS);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                for (int i = 0; i < count; i++) {
                    snowX[i] += getRandomInt(5) - 3;
                    snowY[i] += 5;
                    angles[i] += i / 5;
                    snowY[i] = snowY[i] > bounds.height ? 0 : snowY[i];
                    angles[i] = angles[i] > 360 ? 0 : angles[i];
                    int x = snowX[i];
                    int y = snowY[i];
                    int angle = angles[i];
                    g2d.translate(x, y);
                    double angleValue = Math.toRadians(angle);
                    g2d.rotate(angleValue);
                    g2d.drawImage(image, 0, 0, null);
                    g2d.rotate(-angleValue);
                    g2d.translate(-x, -y);
                }
            }
        };

        //设置透明背景
        pane.setBackground(new Color(0,0,0,0));

        frame.setContentPane(pane);
        frame.setVisible(true);
        Thread thread = new Thread() {

            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(10);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }

                    pane.repaint();

                }
            }
        };

        thread.start();
    }



    public static int getRandomInt(int width) {
        int i = 0;
        double d = Math.random() * width;
        i = (int)d;
        return i;
    }
}




三、说明

以上代码可以直接使用。

有问题的动态图片的代码,还请大佬看看怎么优化;要不实现不了透明效果,要不就是那样,有重叠,很尴尬。

你可能感兴趣的:(JFrame显示透明图片与动态图片)