服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

用java的jwindow实现程序运行出的splash画面


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

扫描关注微信公众号