在游戏开发的时候,我们可能会需要制作文字滚屏的效果。比如一行文字从屏幕下部滚动到屏幕的中间,在J2ME中我们可以借助Canvas类以及Timer类来完成类似的效果。

    在J2ME的高级的图形用户界面API中并没有提供类似滚屏的效果,因此我们需要自己通过Canvas类来实现,我们应该在Canvas类的paint()的方法中绘制我们要滚动的文字,基本代码如下所示:
    protected void paint(Graphics arg0)
    {
        int oldColor = arg0.getColor();
        arg0.setColor(255, 255, 255);
        arg0.fillRect(0, 0, width, height);
        arg0.setColor(oldColor);
        arg0.drawString(welcome, left, i, Graphics.LEFT | Graphics.TOP);

    }
当我们初始化一个Canvas的子类的时候,paint()方法将被调用,我们需要它定时得往上滚动,因此我们可以借助 Timer和TimerTask类,我们在TimerTask类中通过实现public void run()方法来对文字的y坐标减小一定的数值。当文字到达屏幕的中间的时候停止减少并停止timer。在这里我是实现了一个内部类,当然你也可以实现成匿名内部类。
   private class ScrollTask extends TimerTask
    {
        public void run()
        {
           
            if (i > height / 2)
            {
                i = i - fontHeight;
                repaint();
            } else
            {
                going = false;
                timer.cancel();
            }

        }
    }
我们的WelcomeCanvas类扩展Canvas并实现它的paint()方法,在WelcomeCanvas的构造器中我们把display和下一个Displayable的对象传给他,并在其中完成一些具体尺寸的计算。
    public WelcomeCanvas(Display display,Displayable disp) {
        super();
        Font font = Font.getDefaultFont();
        left = (width - font.stringWidth(welcome))/2;
        fontHeight = font.getHeight();
        this.display = display;
        this.next = disp;
        ScrollTask st = new ScrollTask();
        timer.schedule(st, 100, 100);
               
    }
当文字滚动到中间后,用户可以通过按下任意键继续操作。因此我们需要keyPressed()方法,如果是触摸屏的话你可以用pointerPressed()方法实现。
    public void keyPressed(int keyCode)
    {
        if(!going)
        display.setCurrent(next);
    }
给出源代码。其实很多类似的效果都可以通过这样的方法来实现。
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class GuessNumber extends MIDlet
{
    private Display display;
    private WelcomeCanvas welcomeCanvas;
    private Form mainForm;


    protected void startApp() throws MIDletStateChangeException
    {
    
        initMIDlet();

    }
   
    private void initMIDlet()
    {
        display = Display.getDisplay(this);
        mainForm = new Form("Guess Number");
        mainForm.append("this is a game");
        welcomeCanvas = new WelcomeCanvas(display,mainForm);
        display.setCurrent(welcomeCanvas);
    }


    protected void pauseApp()
    {
       
    }


    protected void destroyApp(boolean arg0) throws MIDletStateChangeException
    {
       
    }

}
/*
 * Created on 2004-9-24
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author E2412C
 *
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class WelcomeCanvas extends Canvas
{
    public static final String welcome = "Press any key to start";
    private Display display;
    private Displayable next;
    private Timer timer = new Timer();
    private int width = getWidth();
    private int height = getHeight();
    private int i = height;
    private int left;
    private int fontHeight;
    private boolean going = true;

    public WelcomeCanvas(Display display,Displayable disp) {
        super();
        Font font = Font.getDefaultFont();
        left = (width - font.stringWidth(welcome))/2;
        fontHeight = font.getHeight();
        this.display = display;
        this.next = disp;
        ScrollTask st = new ScrollTask();
        timer.schedule(st, 100, 100);
               
    }

    protected void paint(Graphics arg0)
    {
        int oldColor = arg0.getColor();
        arg0.setColor(255, 255, 255);
        arg0.fillRect(0, 0, width, height);
        arg0.setColor(oldColor);
        arg0.drawString(welcome, left, i, Graphics.LEFT | Graphics.TOP);

    }
   
    public void keyPressed(int keyCode)
    {
        if(!going)
        display.setCurrent(next);
    }

    private class ScrollTask extends TimerTask
    {
        public void run()
        {
           
            if (i > height / 2)
            {
                i = i - fontHeight;
                repaint();
            } else
            {
                going = false;
                timer.cancel();
            }

        }
    }
}