基于Nokia S40的猜数字游戏之一[5]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

     我们推荐制作一个容器类来管理我们的Button,他会知道什么时候应该移动焦点,什么时候该去绘制它的“孩子”们。我们把这个类叫Manager。

package com.j2medev.numbergame;
import java.util.*;
import javax.microedition.lcdui.*;

// A subclass of Area that can act as
// the parent for other components.

public class Manager extends Area
{
    protected Vector children = new Vector();

    protected Area focus = null;

    public Manager()
    {
        super(0, 0, 0, 0, null);
        w = getCanvasWidth();
        h = getCanvasHeight();
    }

    public void add(Area child)
    {
        if (!children.contains(child))
        {
            children.addElement(child);
            child.setParent(this);
            repaintArea(child, false);
        }
    }

protected Area getFocus()
    {
        if (focus == null && children.size() > 0)
        {
            focus = (Area) children.elementAt(0);
        }

        return focus;
    }

    public void keyPressed(int keyCode)
    {
        Area focus = getFocus();
        if (focus != null && focus != this)
        {
            focus.keyPressed(keyCode);
        }
    }

    public void keyReleased(int keyCode)
    {

    }

    public void keyRepeated(int keyCode)
    {

    }

    // Called to move the focus to the next
    // or previous component

    protected void moveFocus(boolean forward)
    {
        Area oldFocus = getFocus();
        if (oldFocus != null)
        {
            int i = children.indexOf(oldFocus);
            int last = children.size() - 2;
            if (forward)
            {
                if (++i > last)
                    i = 0;
            } else
            {
                if (--i < 0)
                    i = last;
            }

            focus = (Area) children.elementAt(i);
            repaintArea(oldFocus, false);
            repaintArea(focus, true);
        }
    }

    public void remove(Area child)
    {
        if (children.removeElement(child))
        {
            child.setParent(null);
            repaintArea(child, false);
        }
    }

本文关键:基于Nokia S40的猜数字游戏之一
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top