我把这个启动画面程序放在了一个计算器的小程序中,初始化:
Toolkit toolkit
=
Toolkit.getDefaultToolkit();
windowSplash = new Window(calc);
Image image = toolkit.getImage( " collage2.jpg " );
ImageCanvas canvas = new ImageCanvas( image );
windowSplash.add( canvas, " Center " );
Dimension scmSize = toolkit.getScreenSize();
int imgWidth = image.getWidth( calc );
int imgHeight = image.getHeight( calc );
windowSplash.setLocation( (scmSize.width - imgWidth) / 2 , (scmSize.height - (imgHeight)) / 2 );
windowSplash.setSize(imgWidth, imgHeight);
windowSplash = new Window(calc);
Image image = toolkit.getImage( " collage2.jpg " );
ImageCanvas canvas = new ImageCanvas( image );
windowSplash.add( canvas, " Center " );
Dimension scmSize = toolkit.getScreenSize();
int imgWidth = image.getWidth( calc );
int imgHeight = image.getHeight( calc );
windowSplash.setLocation( (scmSize.width - imgWidth) / 2 , (scmSize.height - (imgHeight)) / 2 );
windowSplash.setSize(imgWidth, imgHeight);
这边我把启动画面显示在右下角了,如果想显示在屏幕中间,把Location改成:
windowSplash.setLocation( (scmSize.width
-
imgWidth)
/
2
, (scmSize.height
-
(imgHeight))
/
2
);
ImageCanvas继承于canvas。
Canvas
组件表示屏幕上一个空白矩形区域,应用程序可以在该区域内绘图,或者可以从该区域捕获用户的输入事件。 应用程序必须为
Canvas
类创建子类,以获得有用的功能(如创建自定义组件)。必须重写
paint
方法,以便在 canvas 上执行自定义图形。
ImageCanvas是Canvas的子类:
class
ImageCanvas
extends
Canvas
{
private Image image;
public ImageCanvas( Image image )
{
MediaTracker mt = new MediaTracker( this );
mt.addImage( image, 0 );
try{
mt.waitForID(0);
}
catch( Exception e)
{}
this.image = image;
}
public void paint( Graphics g ) {
g.drawImage( image, 0, 0, this );
}
public void update( Graphics g ) {
paint( g );
}
public Dimension getPreferedSized() {
return new Dimension( image.getWidth(this), image.getHeight(this) );
}
}
{
private Image image;
public ImageCanvas( Image image )
{
MediaTracker mt = new MediaTracker( this );
mt.addImage( image, 0 );
try{
mt.waitForID(0);
}
catch( Exception e)
{}
this.image = image;
}
public void paint( Graphics g ) {
g.drawImage( image, 0, 0, this );
}
public void update( Graphics g ) {
paint( g );
}
public Dimension getPreferedSized() {
return new Dimension( image.getWidth(this), image.getHeight(this) );
}
}
开始运行启动画面:
windowSplash.setVisible(
true
);
windowSplash.toFront();
windowSplash.toFront();
因为这里借助于一个计算器作为示例,计算器的初始化所需时间极短,启动画面会一闪而过,所以我在关闭启动画面前添加了一个线程的睡眠:
try
{
Thread.sleep(4000);
} catch (InterruptedException e2) {
e2.printStackTrace();
}
Thread.sleep(4000);
} catch (InterruptedException e2) {
e2.printStackTrace();
}
接下来关闭启动画面,这段代码插在所有初始化工作完成之后,这个示例中只是界面的初始化,实际应用程序还包括后台数据库等.
windowSplash.dispose();
基本的应用程序启动画面就实现了,附上这个示例的完整源代码: /Files/JeffChen/qidonghuamian.txt