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

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

本文简介:

    public void repaintArea(Area child, boolean now)
    {
        if (parent != null)
        {
            parent.repaintArea(child, now);
        } else
        {
            repaint(child.getX(), child.getY(), child.getWidth(), child
                    .getHeight());

        if (now)
            {
                serviceRepaints();
            }
        }
    }

    public void setBackColor(int col)
    {
        backcolor = col;
    }

    public void setForeColor(int col)
    {
        forecolor = col;
    }

    protected void setParent(Manager parent)
    {
        this.parent = parent;
    }

}
这是一个抽象类,他的抽象方法protected abstract void paintArea(Graphics g, boolean hasFocus);是留给他的子类实现的。因此我们在子类中重点需要关注的就是如何来绘制自己和根据用户的输入做出响应。下面我们来实现Button类,我们需要提供一个ButtonListener类,通过他我们可以实现回调的功能,即在按键按下的时候去做ButtonListener中规定的方法,非常类似于CommandListener的功能。

package com.j2medev.numbergame;

public interface ButtonListener
{
    public void buttonPressed(Button pushed);

}
下面是Button类的代码

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

public class Button extends Area
{

    private String label;

    private boolean selected;

    private ButtonListener listener;

    private boolean modifiable = true;
   
    private int marginx = 4;
   
    private int marginy = 4;

    public Button(String label, int x, int y)
    {
        this(label, x, y, 0, 0, null);
    }

    public Button(String label, int x, int y, Font f)
    {
        this(label, x, y, 0, 0, f);
    }

    public Button(String label, int x, int y, int w, int h)
    {
        this(label, x, y, w, h, null);
    }

    public Button(String label, int x, int y, int w, int h, Font f)
    {
        super(x, y, w, h, f);

        if (label == null)
            label = "";

        int tw = calcMinWidth(label, getFont());
        int th = calcMinHeight(label, getFont());

        if (tw > w)
            this.w = tw;
        if (th > h)
            this.h = th;

        this.label = label;
    }

    public void setModifiable(boolean flag)
    {
        this.modifiable = flag;
    }

    public static int calcMinWidth(String text, Font f)
    {
        return f.stringWidth(text) + 8;
    }

    public static int calcMinHeight(String text, Font f)
    {
        return f.getHeight() + 8;
    }

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

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

go top