import javax.microedition.lcdui.*; import java.util.*; class Louend4 extends Canvas{ int x, y, samm=0, r=5; Timer t; protected void paint(Graphics g){ g.setColor(255, 255, 255); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(0, 0, 0); g.drawRect(x-r, y-r, 2*r, 2*r); } protected void keyPressed(int kood){ if(getGameAction(kood)==Canvas.LEFT){samm=-1;} if(getGameAction(kood)==Canvas.RIGHT){samm=1;} } public void showNotify(){ x=getWidth()/2; y=getHeight()/2; t=new Timer(); TimerTask tt=new TimerTask(){ public void run(){ liigu(); } }; t.schedule(tt, 500, 100); //viivitus, intervall } public void hideNotify(){ t.cancel(); } void liigu(){ if((samm>0 && x+samm+r>getWidth()) || (samm<0 && x+samm-r<0)){ samm=0; } else { x+=samm; repaint(); } } }