由于MIDP1.0中提供的API比较有限,因此Nokia实现了自己的类库来提供补充。其中包括声音的支持、屏幕的背景光和机器震动、图像的翻转、Alpha通道以及对像素的操作支持。学习Nokia UI的使用的最好例子莫过于Nokia官方提供的FishTank了。本文讲主要介绍一下这个范例,其中的一些思路和技巧是值得我们学习的。

下面我们看看整个应用的结构

程序总共有四个类组成,其中我们主要关注的是FishTankCanvas、Fish和Weeds。其中Fish和Weeds比较相似,他们代表了两个对象鱼儿和水草。而FishTankCanvas是FullCanvas的子类并且实现了Runnable接口,它是一个独立的线程。每隔一段时间他会更新一下Fish和Weeds的状态,然后重新绘制屏幕。run()方法中的代码如下
public void run()
{
Thread currentThread = Thread.currentThread();
try
{
// This ends when animationThread is set to null, or when
// it is subsequently set to a new thread; either way, the
// current thread should terminate
while (currentThread == animationThread)
{
long startTime = System.currentTimeMillis();
// Only animate when the canvas is visible.
if (isShown())
{
tick();
// Repaint everything above the sand, the fish
// never swim at h > waterHeight.
repaint(0, 0, waterWidth, waterHeight);
serviceRepaints();
}