Mophun & J2ME 的游戏开发之旅(十) _____Loading 界面的制作

类别:Java 点击:0 评论:0 推荐:

HOW TO WRITE A LOADING INTERFACE
怎样写一个Loading界面?
    这是初学者容易问的问题,其实一个Loading界面的本质就是呈现给用户一个系统正在抓紧工作的假象。尽管系统正在全速运行,但如果读取资源之类的工作系统需要用很长时间来工作,则有必要告诉用户我正在努力的工作。一个慢速而表现出工作的系统,比一个相对快速闷头苦干用户却体会不到的系统没有前者被用户喜欢。
    怎么样设计呢?在下面的代码中你可以体会到最简单的Loading界面设计:
三个类:
(1)Loading
(2)LoadingResource
(3)LoadingCanvas
    我们可以理解LoadingResource 为 线程0, LoadingCanvas 为线程1

我们的代码的工作方式如下,根据系统的不同、虚拟机之间的差异等等就,可以简单的表示为下图:


工作的线程:     | 000000000 0000000000000 00000000...............
没有工作的线程: |          1             1        ...............
时间记录:       + 1 2 3 4 5 6 7 8 9 10 11 12 13 14...............
也就是说,我们关注的两个线程,LoadingCanvas给我们看了一个动态的过程,在LoadingCanvas工作的间隙,另一个线程LoadingResource默默地工作,当工作完毕之后我们获得工作完毕的标识值,则进入另一个画面.Loading结束.
        
一个J2ME例程
//| ******************************************************************************************
//| FileName             | Copyright   |  Author    | VERSION |  START DATE | LAST MODIFY DATE
//| Loading.java         | Gaogao      |  GAOGAO    |  0.01   |  2004-12-05 |     2004-11-05  
//|                                                                          
//| Abstract:                                                                
//| A project to demo the Loading Interface                                                          
//| ******************************************************************************************

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Loading extends MIDlet
{
 //
 private LoadingCanvas loadingCanvas;
 
 public void startApp()
 {
  loadingCanvas = new LoadingCanvas();
  Display.getDisplay(this).setCurrent(loadingCanvas);  
  
 }//startApp()

 public void pauseApp()
 {
 
 }//pauseApp()
 public void destroyApp(boolean unconditional)
 {
 
 }//destroyApp(boolean)
}

 

//| ******************************************************************************************
//| FileName             | Copyright   |  Author    | VERSION |  START DATE | LAST MODIFY DATE
//| LoadingCanvas.java   | Gaogao      |   GAOGAO   |  0.01   |  2004-12-05 |     2004-11-05  
//|                                                                          
//| Abstract:                                                                
//| LoadingCanvas CLASS                                                        
//| ******************************************************************************************

import javax.microedition.lcdui.*;

public class LoadingCanvas extends Canvas implements Runnable
{
   //代表LoadingCanvas的对象本身的线程
 private Thread thread;
 
 private LoadingResource loadingResource;
 
 //滚动长短控制
 private static final int TIMER_LOADING_SCORLLER = 16;
 private static int timerLoadingScorller = 0;

 //DOUBLE BUFFERING
 private Image image;
 private Graphics graphics;

 private boolean ready = false;
 //Constructor
 public LoadingCanvas()
 { 

  image =Image.createImage(getWidth(), getHeight());

  graphics = image.getGraphics();
  loadingResource = new LoadingResource();
  loadingResource.start();

  thread = new Thread(this);
  thread.start();
  ready = true;
  
  
 }//LoadingCanvas()

 
 public void paint(Graphics g)
 {
  if (ready)
  {
   g.drawImage(image, 0, 0, Graphics.TOP|Graphics.LEFT);
  }
  
 }//paint(Graphics g)

 public void run()
 {
  while (true)
  { 

   if (!loadingResource.isLoadingCompeleted())
   {
    timerLoadingScorller = ++timerLoadingScorller % TIMER_LOADING_SCORLLER;
    paintLoadingScorller();
   }//if
   else
   {
    paintGameCanvas();
    repaint();
   }//else
   try
   {
    thread.sleep(40);
    thread.yield();
   }
   catch(InterruptedException ex)
   {
    //
   }
   repaint();
   serviceRepaints();
  }//while
 }//run()

 protected void paintLoadingScorller()
 {
  //background
  graphics.setColor(255, 255, 255);//白色
  graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
  //laoding scorller
  graphics.setColor(0, 0, 0);//黑色
  graphics.drawString("LOADING...",0, 0, Graphics.LEFT|Graphics.TOP);
  graphics.setColor(255, 0, 0);//RED
  
  //for (int i = 0; i < timerLoadingScorller; i++)
  //{
  // graphics.drawString("<",
  //      i * image.getWidth() / TIMER_LOADING_SCORLLER,
  //      image.getHeight() / 2,
  //                  Graphics.LEFT|Graphics.TOP
  //      );
  //
  //}//for
  graphics.fillRect(0,
        image.getHeight() / 2,
        timerLoadingScorller * image.getWidth() / TIMER_LOADING_SCORLLER,
        5
        );
 }//paintLoadingScorller()

 protected void paintGameCanvas()
 {
  //background
  graphics.setColor(255, 255, 255);//白色
  graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
  graphics.setColor(0, 0, 0);//BLACK
  //
  graphics.drawString("GAME STARTED",0, 0, Graphics.LEFT|Graphics.TOP);
 }//paintGameCanvas()
}



//| ******************************************************************************************
//| FileName             | Copyright   |  Author    | VERSION |  START DATE | LAST MODIFY DATE
//| LoadingResource.java | Gaogao      |   GAOGAO   |  0.01   |  2004-12-05 |     2004-11-05  
//|                                                                          
//| Abstract:                                                                
//| LoadingResource CLASS                                                        
//| ******************************************************************************************

public class LoadingResource extends Thread
{
 boolean loadingCompeleted = false;
 
 public LoadingResource()
 {
  
 }//LoadingResource()

 public void run()
 {
  //following statemenet is a emu to the reading resource from mobiles ram
  int key = 1000; // the key decide how times you will wait int a random method
  while (new java.util.Random().nextInt() % key != 0);
  loadingCompeleted = true;
 }//run()

 public boolean isLoadingCompeleted()
 {
  return loadingCompeleted;
 }//isLoadingCompeleted()
}



 

本文地址:http://com.8s8s.com/it/it12818.htm