SplashScreen.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:3k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.lcdui.*;
  2. class SplashScreen    extends Canvas    implements Runnable
  3. {
  4.     private final escapeeMIDlet midlet;
  5.     private Image splashImage;
  6.     private volatile boolean dismissed = false;
  7.     SplashScreen( escapeeMIDlet midlet)
  8.     {
  9.         this.midlet = midlet;
  10.         setFullScreenMode(true);
  11.         splashImage =  escapeeMIDlet.createImage("/splash.png");
  12.         new Thread(this).start();
  13.     }
  14.     public void run()
  15.     {
  16.         synchronized(this)
  17.         {
  18.             try
  19.             {
  20.                 wait(3000L);   // 3 seconds
  21.             }
  22.             catch (InterruptedException e)
  23.             {
  24.                 // can't happen in MIDP: no Thread.interrupt method
  25.             }
  26.             dismiss();
  27.         }
  28.     }
  29.     public void paint(Graphics g)
  30.     {
  31.         int CanvasWidth = getWidth();
  32.         int CanvasHeight = getHeight();
  33.         g.setColor(0x00FFFFFF);  // white
  34.         g.fillRect(0, 0, CanvasWidth, CanvasHeight);
  35.         g.setColor(0x00FF0000);  // red
  36.         g.drawRect(1, 1, CanvasWidth-3, CanvasHeight-3);  // red border one pixel from edge
  37.         if (splashImage != null)
  38.         {
  39.             g.drawImage(splashImage,
  40.                         CanvasWidth/2,
  41.                         CanvasHeight/2,
  42.                         Graphics.VCENTER | Graphics.HCENTER);
  43.             splashImage = null;
  44.             
  45.             
  46.         }
  47.         g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,
  48.                                Font.STYLE_BOLD,
  49.                                Font.SIZE_LARGE));
  50.         int centerX = CanvasWidth / 2;
  51.         int centerY = CanvasHeight / 2+60;
  52.         g.setColor(0x00FFFFFF);   // white
  53.         drawText(g, centerX, centerY - 1);
  54.         drawText(g, centerX, centerY + 1);
  55.         drawText(g, centerX - 1, centerY);
  56.         drawText(g, centerX + 1, centerY);
  57.         g.setColor(0x00000000);   // black
  58.         drawText(g, centerX, centerY);
  59.         midlet.splashScreenPainted();
  60.     }
  61.     private void drawText(Graphics g, int centerX, int centerY)
  62.     {
  63.         int fontHeight = g.getFont().getHeight();
  64.         int textHeight = 2 * fontHeight;
  65.         int topY = centerY - textHeight / 2;
  66.         g.drawString("逃亡者游戏",
  67.                      centerX,
  68.                      topY,
  69.                      Graphics.HCENTER | Graphics.TOP);
  70.         g.drawString("版本: " + midlet.getAppProperty("MIDlet-Version"),
  71.                      centerX,
  72.                      topY + fontHeight,
  73.                      Graphics.HCENTER | Graphics.TOP);
  74.     }
  75.     public synchronized void keyPressed(int keyCode)
  76.     {
  77.         dismiss();
  78.     }
  79.     private void dismiss()
  80.     {
  81.         if (!dismissed)
  82.         {
  83.             dismissed = true;
  84.             midlet.splashScreenDone();
  85.         }
  86.     }
  87. }