熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

用java的jwindow運行出的splash畫面

2022-06-13   來源: Java核心技術 

  import javaxswing*;
import javaawt*;
/*
JWindow 是一個能夠在用戶桌面的任何地方顯示的容器
所以能夠使用它構成程序剛運行時的splash畫面
*/
public class ESplash extends JWindow implements Runnable {
  
    private Thread thread = null;
    private Image logo = null;
    private Color bg_color = new Color( );
    private Toolkit toolkit =getToolkit();
    private int image_width;
    private int image_height;

  public ESplash() {
      logo = new ECreateIcon()getSplashImage();
      loadImage(logo );
      image_width = logogetWidth(this);
      image_height = logogetHeight(this);
      setBackground(bg_color);
      setCursor(new Cursor());
      setSize(image_width + image_height + );
      //設置JWindow的顯示位置
      int Xpos = (toolkitgetScreenSize()width getSize()width) / ;
      int Ypos = (toolkitgetScreenSize()height getSize()height) / ;
      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);
            trackeraddImage(image ID);
            try {
                trackerwaitForID(ID);
            }
            catch(InterruptedException _ex) { }
        }
    }

  /*
     在JWindow部件上繪制圖像
    */
   
    public void paint(Graphics g) {
        gdrawImage(logo image_width image_height this);
       
        //設置字體的色彩
        gsetColor(new Color( ));
        gdrawString(正在初始化系統 getSize()height );
        //設置矩形框的背景色彩
        gsetColor(new Color( ));
       
        //繪制矩形框
        gfillRect( getSize()height );
        gdrawRect( getSize()height );
       
        //重新設置將要填塗在矩形框中的顏色
        gsetColor(new Color( ));
        for(int n = ; n < ; n += )
            try {
             //線程休眠毫秒
             Threadsleep(L);
             //填塗矩形框
             gfillRect( getSize()height n );
            }
            catch(Exception _ex) { }
        }

  public void run() {
        //設置鼠標為等待狀態
        setCursor(new Cursor());
        repaint();
    }

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

  //更新圖形區防止繪圖時產生閃爍現象

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

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

  import javaawt*;
import javaawtimage*;
import javaawtevent*;
import javaxswing*;

  public class ECreateIcon{
 
 private static Image splashimage;
 
 public ECreateIcon(){
 splashimage = getImageFromResource(resources/images/Esplashgif);
   }
 //獲得圖像
 private Image getImageFromResource(String image_path) {
         return ToolkitgetDefaultToolkit()getImage(image_path);
   }

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


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26698.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.