import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class Liikumine1 extends Applet implements ActionListener, KeyListener{ int x=100; int y=50; int sammX=0; int sammY=1; int d=15; Timer t=new Timer(500, this); public Liikumine1(){ t.start(); addKeyListener(this); } public void paint(Graphics g){ super.paint(g); g.drawRect(x, y, d, d); } public void liigu(){ x=x+sammX; y=y+sammY; repaint(); } public void actionPerformed(ActionEvent e){ if(e.getSource()==t){ liigu(); } } public void keyPressed(KeyEvent e){ int kood=e.getKeyCode(); if(kood==KeyEvent.VK_LEFT){sammX=-1;} if(kood==KeyEvent.VK_RIGHT){sammX=1;} if(kood==KeyEvent.VK_UP){sammY=-1;} if(kood==KeyEvent.VK_DOWN){sammY=1;} repaint(); } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} public static void main(String[] arg){ Frame f=new Frame("Liikumine"); f.add(new Liikumine1()); f.setSize(300, 300); f.setVisible(true); } }