package co.dc.aptiv;

import javax.swing.*;
import java.awt.*;

public class JWindowLoding extends JFrame implements Runnable {
    /**
     * 进度条
     */
    JProgressBar progress;
    private JPanel contentPane;
    private int width = 500;
    private int height = 300;
    private Color backGroudColor = Color.white;
    private Color progressColor = Color.orange;
    private int pointX = 100;
    private int pointY = 200;
    private int progressWidth = 300;
    private int progressHeight = 15;

    public JWindowLoding() {
        initProgress();
    }

    /**
     * @param width          宽
     * @param height         高
     * @param backGroudColor 进度条背景颜色
     * @param progressColor  进度条颜色
     * @param progressWidth  进度条长
     * @param progressHeight 进度条宽
     */
    public JWindowLoding(int width, int height, Color backGroudColor, Color progressColor, int pointX, int pointY, int progressWidth, int progressHeight) {
        this.width = width;
        this.height = height;
        this.backGroudColor = backGroudColor;
        this.progressColor = progressColor;
        this.pointX = pointX;
        this.pointY = pointY;
        this.progressWidth = progressWidth;
        this.progressHeight = progressHeight;
        initProgress();
    }

    private void initProgress() {
        setSize(width, height);
        //取消包含关闭按钮的边框
        setUndecorated(true);
        //定位中间
        setLocationRelativeTo(null);
        contentPane = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Image image = new ImageIcon(getClass().getResource("img/background.png")).getImage();
                g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
            }
        };
        contentPane.setLayout(null);
        setContentPane(contentPane);
        // 设置光标
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        // 实例化进度条
        progress = new JProgressBar();
        // 描绘文字
        progress.setStringPainted(true);
        progress.setString("加载中,请稍候......");
        progress.setValue(0);
        progress.setSize(progressWidth, progressHeight);
        //设置背景色
        progress.setBackground(backGroudColor);
        //设置进度条颜色
        progress.setForeground(progressColor);
        progress.setBounds(pointX, pointY, progressWidth, progressHeight);
        contentPane.add(progress);
    }

    public void start() {
        setVisible(true);
    }

    public void setProgress(String desc, int val) {
        progress.setString(desc);
        progress.setValue(val);
    }

    public void finish() {
        dispose();
    }

    public void run() {
    }

    public static void main(String[] args) {
        JWindowLoding splash = new JWindowLoding(500, 300, Color.white, new Color(250, 250, 210), 100, 200, 300, 15);
        // 运行启动界面
        splash.start();
        try {
            Thread.sleep(5000);
            splash.setProgress("3000", 30);
            Thread.sleep(5000);
            splash.setProgress("3000", 80);
            Thread.sleep(5000);
            splash.finish();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}