import java.applet.Applet; import java.awt.event.*; import java.awt.*; public class Auto4 extends Applet implements KeyListener, Runnable{ double x=100, y=100; double nurk=0, pikkus=30, samm=5; double xt=200, yt=100; double nurkt=0, pikkust=30, sammt=5; boolean algus=true; public Auto4(){ addKeyListener(this); } public void paint(Graphics g){ double x2=x+pikkus*Math.cos(nurk); double y2=y+pikkus*Math.sin(nurk); g.drawLine((int)x, (int)y, (int)x2, (int)y2); g.drawOval((int)x-5, (int)y-5, 10, 10); double x2t=xt+pikkust*Math.cos(nurkt); double y2t=yt+pikkust*Math.sin(nurkt); g.drawLine((int)xt, (int)yt, (int)x2t, (int)y2t); g.drawOval((int)xt-5, (int)yt-5, 10, 10); } public void run(){ while(true){ x=x+Math.cos(nurk)*samm; y=y+Math.sin(nurk)*samm; xt=xt+Math.cos(nurkt)*sammt; yt=yt+Math.sin(nurkt)*sammt; repaint(); try{Thread.sleep(200);} catch(Exception ex){} } } public void keyPressed(KeyEvent e){ if(algus){ new Thread(this).start(); algus=false; } int kood=e.getKeyCode(); if(kood==KeyEvent.VK_LEFT)nurk=nurk-0.1; if(kood==KeyEvent.VK_RIGHT)nurk=nurk+0.1; if(kood==KeyEvent.VK_UP){samm=samm+1;} if(kood==KeyEvent.VK_DOWN){samm=samm-1;} if(kood==KeyEvent.VK_S)nurkt=nurkt-0.1; if(kood==KeyEvent.VK_D)nurkt=nurkt+0.1; if(kood==KeyEvent.VK_E){sammt=sammt+1;} if(kood==KeyEvent.VK_X){sammt=sammt-1;} } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} public static void main(String argumendid[]){ Frame f=new Frame("Klahvikuular"); f.add(new Auto4()); f.setSize(300, 300); f.setVisible(true); } }