import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import javax.microedition.rms.*; import java.lang.Integer.*; import java.util.*; import java.lang.*; public class Kirp extends MIDlet{ Canvas louend=new plats(); StringItem kaugus=new StringItem("", ""); protected void startApp() throws MIDletStateChangeException{ Display.getDisplay(this).setCurrent(louend); } protected void pauseApp(){} protected void destroyApp(boolean kohustus) throws MIDletStateChangeException{} } class plats extends Canvas{ int tulemus, tulemusx, tulemusy, rekord, x=1, y=1; String hoidlanimi="rekordhoidla2"; boolean valma=false; Timer t; protected void paint(Graphics g){ System.out.println(rekord); try{ RecordStore rs=RecordStore.openRecordStore(hoidlanimi, true); if(rs.getNumRecords()==0){ rekord=0; rs.addRecord(new byte[]{(byte)(rekord)}, 0, 1); }else{ rekord=(int)(rs.getRecord(1)[0]); } }catch(Exception e){e.printStackTrace();} if (valma==false){ //joonistame valge ruudu taustaks g.setColor(255, 255, 255); //joonsitame kasti ümber mängu väljaku g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(0, 0, 0); g.drawLine(2, 2, getWidth()-2, 2); //x1, y1, x2, y2 ylemine g.drawLine(2, 2, 2, getHeight()-2-getHeight()/3); //x1, y1, x2, y2 vasak g.drawLine(2, getHeight()-2-getHeight()/3, getWidth()-2, getHeight()-2-getHeight()/3); //x1, y1, x2, y2 alumine g.drawLine(getWidth()-2, 2, getWidth()-2, getHeight()-2-getHeight()/3); //x1, y1, x2, y2 parem //joonistame mängu väljaku ja kirbu lava piiri g.drawLine(getWidth()/3+2, 2, getWidth()/3+2, getHeight()-2-getHeight()/3); //x1, y1, x2, y2 vasak //joonistame kirbu lavale g.fillArc((int)(getWidth()/6)-5, (int)(getHeight()/3)-5, 10, 10, 0, 360); //joonistame nüüd märklaua g.drawArc((int)(getWidth()/3*2)-5, (int)(getHeight()/3)-5, 10, 10, 0, 360); g.drawArc(getWidth()/3*2-12, (int)(getHeight()/3)-12, 24, 24, 0, 360); g.drawArc(getWidth()/3*2-24, (int)(getHeight()/3)-24, 48, 48, 0, 360); g.drawArc(getWidth()/3*2-35, (int)(getHeight()/3)-35, 70, 70, 0, 360); g.drawArc(getWidth()/3*2-50, (int)(getHeight()/3)-50, 100, 100, 0, 360); //joonistame nüüd vajalikud tekstid ja kastid, kus hakkavad kordinaadid tõmblema g.drawString("Kaugus", 30, 15, Graphics.BOTTOM | Graphics.HCENTER); g.drawString("Kõrgus", 30, getWidth()/3*2-3, Graphics.BOTTOM | Graphics.HCENTER); g.drawString(String.valueOf(x), 30, 25, Graphics.BOTTOM | Graphics.HCENTER); g.drawString(String.valueOf(y), 30, getWidth()/3*2-13, Graphics.BOTTOM | Graphics.HCENTER); }else{ g.setColor(255, 255, 255); g.fillArc((int)(getWidth()/6)-5, (int)(getHeight()/3)-5, 10, 10, 0, 360); g.setColor(0, 0, 0); g.fillArc(x-5, y-5, 10, 10, 0, 360); g.drawString("Tulemus:", 30, 50, Graphics.BOTTOM | Graphics.HCENTER); if(x>(int)(getWidth()/3*2)){ tulemusx=x-(int)(getWidth()/3*2); }else{ tulemusx=(int)(getWidth()/3*2)-x; } if(y>(int)(getHeight()/3)){ tulemusy=y-(int)(getHeight()/3); }else{ tulemusy=(int)(getHeight()/3)-y; } tulemus=tulemusx+tulemusy; g.drawString(String.valueOf(tulemus), 30, 60, Graphics.BOTTOM | Graphics.HCENTER); g.drawString("ühikut.", 30, 70, Graphics.BOTTOM | Graphics.HCENTER); if(rekord>tulemus || rekord==0){ try{ RecordStore rs=RecordStore.openRecordStore(hoidlanimi, true); rs.setRecord(1, new byte[]{(byte)(tulemus)}, 0, 1); }catch(Exception e){e.printStackTrace();} } } g.drawString("Rekord: "+String.valueOf(rekord), getWidth()/2, getHeight()/3*2+30, Graphics.BOTTOM | Graphics.HCENTER); g.drawString("Hüppa kirbuga märklaua keskele", getWidth()/2, getHeight()/3*2+50, Graphics.BOTTOM | Graphics.HCENTER); } public void showNotify(){ t=new Timer(); TimerTask tt=new TimerTask(){ public void run(){ tomble(); } }; t.schedule(tt, 500, 100); //viivitus, intervall } protected void keyPressed(int kood){ if(getGameAction(kood)==8){ t.cancel(); valma=true; repaint(); } } void tomble(){ x=Math.abs(new Random().nextInt() % (getWidth()-getWidth()/3))+getWidth()/3; y=Math.abs(new Random().nextInt() % (getHeight()-getHeight()/3)); repaint(); } }