////Kaido Heinsalu(IF-21) kodune ülesanne ////Ülesanne nägi ette, et ekraanile paigutatakse hiirega hulk ringe. Vajutades hiirega alla ühe ringi sees ning lastes nupu lahti teise ringi sees tõmmatakse nende ringide keskkohtade vahele joon import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.util.LinkedList; public class Lohistamine extends Applet implements MouseListener{ int m, n, o, p; LinkedList RingidList = new LinkedList(); public Lohistamine(){ addMouseListener(this); } class yldRing { int x; int y; public yldRing(int x_abi, int y_abi) { x = x_abi - 20; y = y_abi - 20; } } public void mousePressed(MouseEvent e) { m=0; n=0; boolean caught = false; if (RingidList.size() == 0){ RingidList.add(new yldRing(e.getX(), e.getY())); } else { for (int i = 0; i < RingidList.size(); i++) { yldRing ringIsend = (yldRing)RingidList.get(i); if (kasRingil(ringIsend, e.getX(), e.getY())) { caught = true; m=ringIsend.x+20; n=ringIsend.y+20; break; } } if (!caught){ RingidList.add(new yldRing(e.getX(), e.getY())); } } //repaint(); } public void mouseReleased(MouseEvent e){ o=0; p=0; for (int i = 0; i < RingidList.size(); i++) { yldRing ringIsend = (yldRing)RingidList.get(i); if (kasRingil(ringIsend, e.getX(), e.getY())) { o=ringIsend.x+20; p=ringIsend.y+20; repaint(); break; } } } public boolean kasRingil(yldRing ringIsend, int x, int y) { int xKaugus = x - ringIsend.x; int yKaugus = y - ringIsend.y; if (xKaugus <= 39 && xKaugus >= 0 && yKaugus <= 39 && yKaugus >= 0){ return true; } else{ return false; } } public void paint(Graphics g){ for (int i = 0; i < RingidList.size(); i++) { yldRing ringIsend = (yldRing)RingidList.get(i); g.setColor(Color.blue); g.drawOval(ringIsend.x, ringIsend.y, 40, 40); } if (m!=0 && n!=0 && o!=0 && p!=0){ g.setColor(Color.red); g.drawLine(m, n, o, p); } } public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseMoved(MouseEvent e){} public static void main(String[] argumendid) { Frame f=new Frame(); f.add(new Lohistamine()); f.setSize(500, 500); f.setTitle("Lohista hiirega ühelt ringilt teisele, siis saab nalja!"); f.setVisible(true); f.addWindowListener(new Aken()); } } class Aken extends WindowAdapter { public void windowClosing(WindowEvent PALLIAKEN) { System.exit(0); } }