明輝手游網(wǎng)中心:是一個免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

用java的jwindow完成程序運(yùn)行出的splash畫面

[摘要]import javax.swing.*;import java.awt.*;/*JWindow 是一個能夠在用戶桌面的任何地方顯示的容器。所以能夠使用它構(gòu)成程序剛運(yùn)行時的splash畫面。*/pu...
import javax.swing.*;
import java.awt.*;
/*
JWindow 是一個能夠在用戶桌面的任何地方顯示的容器。
所以能夠使用它構(gòu)成程序剛運(yùn)行時的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);
      //設(shè)置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加載圖像,確保圖像被正確的加載。
     圖像被加載后,將進(jìn)行繪圖。
    */
    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);
        
        //設(shè)置字體的色彩
        g.setColor(new Color(102, 102, 150));
        g.drawString("正在初始化系統(tǒng)......", 7, getSize().height - 72);
        //設(shè)置矩形框的背景色彩。
        g.setColor(new Color(255, 255, 255));
        
        //繪制矩形框
        g.fillRect(5, getSize().height - 70, 317, 7);
        g.drawRect(5, getSize().height - 70, 317, 7);
        
        //重新設(shè)置將要填涂在矩形框中的顏色
        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() {
        //設(shè)置鼠標(biāo)為等待狀態(tài)
        setCursor(new Cursor(3));
        repaint();
    }

    public void stop() {
        //結(jié)束線程
        thread = null;
        logo = null;
    }

//更新圖形區(qū),防止繪圖時產(chǎn)生閃爍現(xiàn)象。

    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;
   }
}