import java.awt.*; import java.awt.event.*; import java.applet.*; public class Autosoit3 extends Applet implements KeyListener{ double x=100, y=50, pikkus=30, a=Math.PI/6, da=0.1; double ks=5, kx, ky; public Autosoit3(){ addKeyListener(this); arvutaKiiruseKomponendid(); } public void paint(Graphics g){ double x2=x+Math.cos(a)*pikkus; double y2=y+Math.sin(a)*pikkus; g.drawLine((int)x, (int)y, (int)x2, (int)y2); g.drawOval((int)x-5, (int)y-5, 10, 10); } void arvutaKiiruseKomponendid(){ kx=Math.cos(a)*ks; ky=Math.sin(a)*ks; } public void keyPressed(KeyEvent e){ int kood=e.getKeyCode(); if(kood==KeyEvent.VK_LEFT){ a=a-da; arvutaKiiruseKomponendid(); } if(kood==KeyEvent.VK_RIGHT){ a=a+da; arvutaKiiruseKomponendid(); } if(kood==KeyEvent.VK_UP){ x=x+kx; y=y+ky; } if(kood==KeyEvent.VK_DOWN){ x=x-kx; y=y-ky; } repaint(); } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} public static void main(String[] arg){ Frame f=new Frame("Joonest auto"); f.add(new Autosoit3()); f.setSize(200, 200); f.setVisible(true); } }