import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class DoodleCanvas extends Canvas implements CommandListener
{
private Command cmExit;
private Command cmClear;
private int startx = 0;
private int starty = 0;
private int currentx = 0;
private int currenty = 0;
private Doodle midlet;
private boolean clearDisplay = false;
private String keyText = null;
public DoodleCanvas(Doodle midlet)
{
this.midlet = midlet;
cmExit = new Command("Exit", Command.EXIT, 1);
cmClear= new Command("Clear", Command.SCREEN, 1);
addCommand(cmExit);
addCommand(cmClear);
setCommandListener(this);
}
protected void paint(Graphics g)
{
//Clear the bachground to white
if(clearDisplay)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
clearDisplay = false;
startx = currentx = starty = currenty = 0;
return;
}
//Draw with black pen
g.setColor(0, 0, 0);
//Draw Line
g.drawLine(startx, starty, currentx, currenty);
if(keyText != null)
g.drawString(keyText, 0, 0, Graphics.TOP | Graphics.HCENTER);
//New starting point is current position
startx = currentx;
starty = currenty;
}
public void commandAction(Command c, Displayable d)
{
if(c == cmExit)
midlet.exitMIDlet();
else if (c == cmClear)
{
clearDisplay = true;
repaint();
}
}
protected void pointerPressed(int x, int y)
{
startx = x;
starty = y;
keyText ="Presed";
}
protected void pointerDeagged(int x, int y)
{
currentx = x;
currenty = y;
keyText ="Draging";
repaint();
}
protected void pointerReleased(int x, int y)
{
keyText ="Released";
}
}