import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class SonaPaigutus4 extends JApplet implements MouseListener{ java.util.List sonad=new ArrayList(); Sona s1=new Sona("Kauss", 50, 30); Sona s2=new Sona("Tass", 150, 30); JTextField tf=new JTextField(); Sona valitud=null; public SonaPaigutus4(){ sonad.add(s1); sonad.add(s2); add(tf, BorderLayout.SOUTH); addMouseListener(this); } public void paint(Graphics g){ super.paint(g); for(Sona s:sonad){ s.joonista(g); } } Sona leiaSonaKohal(int x, int y){ for(Sona s:sonad){ if(s.kasSees(x, y)){return s;} } return null; } public void mousePressed(MouseEvent e){ valitud=leiaSonaKohal(e.getX(), e.getY()); if(valitud==null && tf.getText().length()>0){ sonad.add(new Sona(tf.getText(), e.getX(), e.getY())); tf.setText(""); } repaint(); } public void mouseReleased(MouseEvent e){ if(valitud!=null){ valitud.muudaAsukoht(e.getX(), e.getY()); repaint(); } } public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public static void main(String[] arg){ JFrame f=new JFrame("Sõna paigutus"); f.getContentPane().add(new SonaPaigutus4()); f.setSize(400, 300); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }