import java.applet.Applet; import java.awt.event.*; import java.awt.*; public class Kast extends Applet implements KeyListener, Runnable{ int laius=300; int korgus=300; int pikkus = 20; int samm = 4; double x = laius/2; double y = 0; boolean algus=true; public Kast(){ addKeyListener(this); } public void paint(Graphics g){ g.setColor(Color.white); g.fillRect(0, 0, laius, korgus); g.setColor(Color.black); g.drawRect((int)x, (int)y, 10, 15); } public void run(){ while(true){ if ( (y+ samm+40) < korgus) { y += samm; } repaint(); try{Thread.sleep(200);} catch(Exception ex){} } } public void keyPressed(KeyEvent e){ if(algus){ new Thread(this).start(); algus=false; } int key=e.getKeyCode(); if (key==KeyEvent.VK_LEFT && x > samm ) x -=samm; if (key==KeyEvent.VK_RIGHT && (x+samm+18) < laius ) x +=samm; } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} public static void main(String argumendid[]){ Frame f=new Frame(""); f.add(new Kast()); f.setSize(300, 300); f.setVisible(true); } }