明辉站/技术开发/内容

用java的jwindow完成程序运行出的splash画面

技术开发2023-07-14 阅读
[摘要]import javax.swing.*;import java.awt.*;/*JWindow 是一个能够在用户桌面的任何地方显示的容器。所以能够使用它构成程序刚运行时的splash画面。*/public class ESplash extends JWindow implements Runn...
import javax.swing.*;
import java.awt.*;
/*
JWindow 是一个能够在用户桌面的任何地方显示的容器。
所以能够使用它构成程序刚运行时的splash画面。
*/
public class ESplash extends JWindow implements Runnable {
   
    private Thread thread = null;
    private Image logo = null;
    private Color bg_color = new Color(255, 255, 255);
    private Toolkit toolkit =getToolkit();
    private int image_width;
    private int image_height;

    public ESplash() {
      logo = new ECreateIcon().getSplashImage();
      loadImage(logo, 0);
      image_width = logo.getWidth(this);
      image_height = logo.getHeight(this);
      setBackground(bg_color);
      setCursor(new Cursor(3));
      setSize(image_width + 10, image_height + 10);
      //设置JWindow的显示位置
      int Xpos = (toolkit.getScreenSize().width - getSize().width) / 2;
      int Ypos = (toolkit.getScreenSize().height - getSize().height) / 2;
      setBounds(Xpos, Ypos, getSize().width, getSize().height);
      setVisible(true);
    }
    /*
     通过使用MediaTracker加载图像,确保图像被正确的加载。
     图像被加载后,将进行绘图。
    */
    private void loadImage(Image image, int ID) {
        if(image != null) {
            MediaTracker tracker = new MediaTracker(this);
            tracker.addImage(image, ID);
            try {
                tracker.waitForID(ID);
            }
            catch(InterruptedException _ex) { }
        }
    }

    /*
     在JWindow部件上绘制图像。
    */
    
    public void paint(Graphics g) {
        g.drawImage(logo, 5, 5, image_width, image_height, this);
        
        //设置字体的色彩
        g.setColor(new Color(102, 102, 150));
        g.drawString("正在初始化系统......", 7, getSize().height - 72);
        //设置矩形框的背景色彩。
        g.setColor(new Color(255, 255, 255));
        
        //绘制矩形框
        g.fillRect(5, getSize().height - 70, 317, 7);
        g.drawRect(5, getSize().height - 70, 317, 7);
        
        //重新设置将要填涂在矩形框中的颜色
        g.setColor(new Color(102, 102, 150));
        for(int n = 0; n < 317; n += 5)
            try {
             //线程休眠50毫秒
             Thread.sleep(50L);
             //填涂矩形框
             g.fillRect(5, getSize().height - 70, n, 5);
            }
            catch(Exception _ex) { }
        }

    public void run() {
        //设置鼠标为等待状态
        setCursor(new Cursor(3));
        repaint();
    }

    public void stop() {
        //结束线程
        thread = null;
        logo = null;
    }

//更新图形区,防止绘图时产生闪烁现象。

    public void update(Graphics g) {
        paint(g);
    }
  }

/////////////////////////////////////////////////////

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

public class ECreateIcon{

private static Image splashimage;

public ECreateIcon(){
splashimage = getImageFromResource("resources/images/Esplash.gif");
   }
//获得图像
private Image getImageFromResource(String image_path) {
         return Toolkit.getDefaultToolkit().getImage(image_path);
   }

public ImageIcon createImageIcon(String filename) {
  String path = "/resources/images/" + filename;
  return new ImageIcon(getClass().getResource(path));
   }
   
   public Image getSplashImage() {
       return splashimage;
   }
}

……

相关阅读