protected void paint(Graphics g)
{
if (focus == null)
getFocus();
eraseBackground(g);
g.setColor(getForeColor());
int n = children.size();
for (int i = n - 1; i >= 0; --i)
{
try
{
Area area = (Area) children.elementAt(i);
area.paint(g, (focus == area));
} catch (Exception e)
{
e.printStackTrace();
}
}
}
protected void paintArea(Graphics g, boolean hasFocus)
{
}
}
在猜数字游戏中,我们还需要一个用于显示用户输入和系统返回纪录的组件,他同样继承与Area,但是他不需要获得焦点。只是显示结果。
package com.j2medev.numbergame;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
public class Mark extends Area
{
private int px;
private int py;
private int count = 0;
private int[][] ab = new int[10][2];
private int[][] input = new int[10][4];
private boolean first = true;
private boolean open = false;
public Mark(int x, int y, int w, int h)
{
super(x, y, w, h);
px = x;
py = y;
}
public Mark(int x, int y, int w, int h, Font f)
{
super(x, y, w, h, f);
px = x;
py = y;
}
public int getCount()
{
return count;
}
public void setCount(int count)
{
this.count = count;
}
public void reset()
{
setCount(0);
this.first = true;
}
public void setAB(int[] ab)
{
this.ab[count][0] = ab[0];
this.ab[count][1] = ab[1];
}
public void setInput(int[] input)
{
for(int i = 0;i<input.length;i++)
{
this.input[count][i] = input[i];
}
}
public void setOpen(boolean open)
{
this.open = open;
}
public int getLineHeight()
{
return this.getFont().getHeight();
}
private String getInput(int count)
{
return ""+input[count][0]+input[count][1]+input[count][2]+input[count][3];
}