⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 splashscreen.java

📁 java netbeans 学习程序合集
💻 JAVA
字号:
/*
 * SplashScreen.java
 *
 * Created on 2007年9月12日, 下午11:01
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.Adam;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;

import java.net.URL;
/**
 *
 * @author Administrator
 */
public class SplashScreen extends Frame{
    
    /** Creates a new instance of SplashScreen */
    public SplashScreen(String aImageId) {
        /* Implementation Note
        * Args.checkForContent is not called here, in an attempt to minimize 
        * class loading.
        */
        if ( aImageId == null || aImageId.trim().length() == 0 ){
          throw new IllegalArgumentException("Image Id does not have content.");
        }
        fImageId = aImageId;
    }
    
    /**
     * Show the splash screen to the end user.
     * 
     * <P>Once this method returns, the splash screen is realized, which means 
     * that almost all work on the splash screen should proceed through the event 
     * dispatch thread. In particular, any call to <foodCode>dispose</foodCode> for the 
     * splash screen must be performed in the event dispatch thread.
     */
    public void splash(){
      initImageAndTracker(); // 初始化图象并且跟踪
      setSize(fImage.getWidth(null), fImage.getHeight(null)); 
      center(); // 初始化框架居中
      
      fMediaTracker.addImage(fImage, 0); // 设置媒体跟踪
      try {
        fMediaTracker.waitForID(0);
      }
      catch(InterruptedException ie){
        System.out.println("Cannot track image load.");
      }
      // 显示软件封面窗口
      SplashWindow splashWindow = new SplashWindow(this,fImage);
    }
    
    // PRIVATE//
    private final String fImageId;
    private MediaTracker fMediaTracker;
    private Image fImage;
    // 初始化图象并且跟踪
    private void initImageAndTracker(){
      fMediaTracker = new MediaTracker(this);
      URL imageURL = SplashScreen.class.getResource(fImageId); // class.getResource 得到图象的 URL 绝对路径
      fImage = Toolkit.getDefaultToolkit().getImage(imageURL); // 用工具取图象信息
    }

    /**
    * 居中框架到屏幕中间
    * This centering service is more or less in {@link UiUtil}; this duplication 
    * is justified only because the use of {@link UiUtil} would entail more 
    * class loading, which is not desirable for a splash screen.
    */
    private void center(){
      Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
      Rectangle frame = getBounds();
      setLocation((screen.width - frame.width)/2, (screen.height - frame.height)/2);
    }
    // 显示软件封面
    private class SplashWindow extends Window {
      SplashWindow(Frame aParent, Image aImage) {
         super(aParent); // 在最上面显示
         fImage = aImage; // 取图象信息
         setSize(fImage.getWidth(null), fImage.getHeight(null)); // 设置图象大小
         Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); // 取屏幕大小
         Rectangle window = getBounds(); // 取屏幕边大小
         setLocation((screen.width - window.width) / 2, (screen.height - window.height) / 2); // 设置位置
         setVisible(true); // 让窗口可见
      }
      public void paint(Graphics graphics) {
        if (fImage != null) {
          graphics.drawImage(fImage,0,0,this);
        }
      }
      private Image fImage;
    } 
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -