private int mCount, mMaximum;
private int mInterval;
private int mWidth, mHeight, mX, mY, mRadius;
private String mMessage;
private boolean run = false;
public WaitCanvas(String message, boolean run)
{
this.mMessage = message;
mCount = 0;
mMaximum = 36;
mInterval = 100;
mWidth = getWidth();
mHeight = getHeight();
// Calculate the radius.
int halfWidth = (mWidth - mRadius) / 2;
int halfHeight = (mHeight - mRadius) / 2;
mRadius = Math.min(halfWidth, halfHeight);
// Calculate the location.
mX = halfWidth - mRadius / 2;
mY = halfHeight - mRadius / 2;
// Create a Timer to update the display.
if (run)
{
TimerTask task = new TimerTask()
{
public void run()
{
mCount = (mCount + 1) % mMaximum;
repaint();
}
};
Timer timer = new Timer();
timer.schedule(task, 0, mInterval);
}
}
public void paint(Graphics g)
{
int theta = -(mCount * 360 / mMaximum);
// Clear the whole screen.
g.setColor(255, 255, 255);
g.fillRect(0, 0, mWidth, mHeight);
// Now draw the pinwheel.
g.setColor(128, 128, 255);
g.drawArc(mX, mY, mRadius, mRadius, 0, 360);
g.fillArc(mX, mY, mRadius, mRadius, theta + 90, 90);
g.fillArc(mX, mY, mRadius, mRadius, theta + 270, 90);
// Draw the message, if there is a message.
if (mMessage != null)
{
g.drawString(mMessage, mWidth / 2, mHeight, Graphics.BOTTOM
| Graphics.HCENTER);
}
}
}
过控制boolean run的值可以决定是不是让这个画面动起来。下面两个例子是Dialog的子类,他们实现了它的抽象方法。我直接给出代码:
import javax.microedition.lcdui.*;
public class ConfirmationDialog extends Dialog implements CommandListener
{
public static final int YES = 0;
public static final int NO = 1;
protected Canvas canvas;
protected Command noCommand;
protected Command yesCommand;
private String message;
private String yesLabel;
private String noLabel;
public ConfirmationDialog(Display display, String message)
{
this(display, message, null, null);
}