Code:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class CanvasForm extends MIDlet implements CommandListener
{
private Display dis;
private Form frm;
private Displayable currt;
private TextField one, two;
private StringItem stritem;
private long result;
private Command quit, add ;
private CanvasClass canvascls;
int first=0, second=0;
public CanvasForm()
{
frm = new Form("Calculator");
one = new TextField(null, "10000002", 8, TextField.NUMERIC );
two = new TextField(null, "10000003", 8, TextField.NUMERIC );
stritem = new StringItem("Result", "");
quit = new Command("Quit", Command.EXIT, 0);
add = new Command("Add", Command.SCREEN, 0);
frm.append(one);
frm.append(two);
frm.append(stritem);
frm.addCommand(add);
frm.addCommand(quit);
frm.setCommandListener(this);
}
public void startApp()
{
dis=dis.getDisplay(this);
if (currt!=null)
{
dis.setCurrent(currt);
}
else
{
dis.setCurrent(frm);
currt=frm;
}
}
public void pauseApp()
{
}
public void destroyApp(boolean b)
{
}
private void calculate()
{
try
{
first = Integer.parseInt( one.getString() );
second = Integer.parseInt( two.getString() );
result = first + second ;
stritem.setText( result + "" );
}
catch (NumberFormatException e)
{
stritem.setText("unknown");
e.printStackTrace();
}
}
public void commandAction(Command c, Displayable s)
{
if (c == quit)
{
notifyDestroyed();
return;
}
calculate();
if (canvascls==null) canvascls= new CanvasClass() ;
currt=canvascls;
dis.setCurrent(canvascls);
}
class CanvasClass extends Canvas implements CommandListener
{
CanvasClass()
{
this.addCommand( new Command("Back", Command.BACK, 0 ) );
this.setCommandListener(this);
}
protected void paint(Graphics g)
{
int w = getWidth();
int h = getHeight();
g.setColor(244,244,244);
g.fillRect( 0,0,w,h );
g.setGrayScale(12*14);
h = Math.min( w, h );
long mf = 100000000;
int angle = (int)(( (first*(36000*mf/result)) +50*mf)/(100*mf)) ;
int origin=180;
g.fillArc(0,0,h,h, origin, (int)angle);
g.setGrayScale(13*16);
g.fillArc(0,0,h,h, (int)(origin+angle), (int)(360-angle) );
g.setColor(123);
g.drawString("A = "+first+" ", h/2, h/2-10, Graphics.BASELINE|Graphics.RIGHT);
g.drawString(" B = "+second, h/2, h/2-10, Graphics.BASELINE|Graphics.LEFT);
g.drawString("Total = "+(first+second), h/2, h/2, Graphics.TOP|Graphics.HCENTER);
}
public void commandAction( Command c, Displayable d)
{
curr=frm;
dis.setCurrent(frm);
}
}
}
Bookmarks