import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.net.URL; import java.math.*; //import java.lang.math; class koord{ int x; int y; public koord(int uus_x, int uus_y){ x=uus_x; y=uus_y; } } class vahemaa{ int d; public vahemaa(int uus_d){ d=uus_d; } } class endinejrkr{ int j; public endinejrkr(int uus_j){ j=uus_j; } } public class Lahim extends Applet implements MouseListener{ int x=100; int y=100; int loendur=0; koord pd[] = new koord[5]; vahemaa v[] = new vahemaa[5]; endinejrkr k[] = new endinejrkr[5]; public Lahim(){ addMouseListener(this); } public void mousePressed(MouseEvent e){ int a,b,i,j,l,kaugeim,w,x,y,z; loendur++; Graphics g=getGraphics(); if(loendur==1){ for(i=0; i<5;i++){ x = (int)(Math.random()*500); y = (int)(Math.random()*500); g.drawOval(x, y, 50, 40); System.out.println(""+x+" "+y); pd[i]=new koord(x, y); pd[i].x=x; pd[i].y=y; } }else if (loendur==2){ System.out.println("Teine vajutus"); a=e.getX(); b=e.getY(); System.out.println(a+" "+b); for(i=0; i<5;i++){ v[i]=new vahemaa(Math.abs((int)(Math.floor(Math.sqrt((a-pd[i].x)*(a-pd[i].x)+(b-pd[i].y)*(b-pd[i].y)))))); System.out.println("Vahemaade vahe on "+v[i].d); } for(i=0; i<5;i++){ k[i]=new endinejrkr(v[i].d); } for(j=0; j<4;j++){ for(i=0; i<4;i++){ if (v[i].d>v[i+1].d){ kaugeim=v[i].d; v[i].d=v[i+1].d; v[i+1].d=kaugeim; } } } System.out.println("lõpuks on kaugeim v[i].d: "+v[i].d); System.out.println("lähim: "+v[0].d); for(l=0; l<5;l++){ if(k[l].j==v[0].d){ System.out.println("l: "+l); break; } } w=pd[l].x; z=pd[l].y; System.out.println(w+" "+z); g.setColor(Color.red); g.fillOval(w, z, 50, 40); g.drawOval(w, z, 50, 40); } } 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[] argumendid){ Frame f=new Frame(); f.add(new Lahim()); f.setSize(300, 300); f.setVisible(true); } public void run(){ Graphics g=getGraphics(); g.drawOval(x, y, 50, 40); try{Thread.sleep(50);}catch(Exception wait){} g.setColor(Color.black); } }