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