import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.Random;
public class CanvasForm extends MIDlet implements CommandListener{
private Display display;
private Form form;
private Displayable current;
private TextField one, two, opp;
private StringItem item;
private String txt;
private long result;
private Command quit, next ;
private CanvasClass canvas;
int first=0, second=0;
public CanvasForm(){}
public void startApp(){
display=Display.getDisplay(this);
form = new Form("welcome to maths game");
form.append("This is your first question...");
one = new TextField(null,"", 1, TextField.ANY );
one.setMaxSize(200);
random();
two = new TextField(null,"", 1, TextField.ANY );
two.setMaxSize(200);
randomnum();
opp = new TextField(null,"", 1, TextField.ANY );
opp.setMaxSize(200);
randomopp();
item = new StringItem("Result", "");
quit = new Command("Quit", Command.EXIT, 0);
next = new Command("Next", Command.SCREEN, 0);
form.append(one);
form.append(two);
form.append(item);
form.append(opp);
form.addCommand(next);
form.addCommand(quit);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp(){}
public void destroyApp(boolean b){}
public void commandAction(Command c, Displayable s){
if (c == quit){
notifyDestroyed();
return;
}
calculate();
if (canvas==null) canvas= new CanvasClass() ;
current=canvas;
display.setCurrent(canvas);
}
private void calculate(){
try {
first = Integer.parseInt( one.getString() );
second = Integer.parseInt( two.getString() );
result = first + second ;
item.setText( result + "" );
} catch (NumberFormatException e){
item.setText("unknown");
e.printStackTrace();
}
}
public void random(){
Random number = new Random();
int f = number.nextInt(10);
number.setSeed(System.currentTimeMillis());
one.setString(""+f);
}
public void randomnum(){
Random number = new Random();
int f = number.nextInt(10);
number.setSeed(System.currentTimeMillis());
two.setString(""+f);
}
public void randomopp(){
Random operator = new Random();
int f = operator.nextInt('+');
int f1 = operator.nextInt('-');
int f2 = operator.nextInt('*');
operator.setSeed(System.currentTimeMillis());
opp.setString(""+f);
opp.setString(""+f1);
opp.setString(""+f2);
}
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){
current=form;
display.setCurrent(form);
}
}
}
|