import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class HiirPall3 extends Applet implements MouseListener, Runnable{ double x, y, allserv=200, r=10; double samm=0, sammumuutus=0.5; boolean algus=true; Object luba=new Object(); public HiirPall3(){ addMouseListener(this); } public void mousePressed(MouseEvent e){ //Plokki pääseb ainult siis, kui luba siinse lõime käes //Muul juhul oodatakse loa vabanemist synchronized(luba){ x=e.getX(); y=e.getY(); } samm=0; if(algus){ new Thread(this).start(); algus=false; } } public void run(){ Graphics g=getGraphics(); while(true){ synchronized(luba){ g.setColor(getForeground()); g.drawOval((int)(x-r), (int)(y-r), (int)(2*r), (int)(2*r)); try{Thread.sleep(100);}catch(Exception ex){} g.setColor(getBackground()); g.drawOval((int)(x-r), (int)(y-r), (int)(2*r), (int)(2*r)); } y=y+samm; samm=samm+sammumuutus; if(y>allserv){ samm=-0.8*Math.abs(samm); } } } public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public static void main(String[] arg){ Frame f=new Frame("Kukkuv pall"); f.add(new HiirPall3()); f.setSize(300, 300); f.setVisible(true); } }