import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class SonaPaigutus5 extends JApplet implements MouseListener, ActionListener{ 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 SonaPaigutus5(){ sonad.add(s1); sonad.add(s2); add(tf, BorderLayout.SOUTH); tf.addActionListener(this); 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(""); } if(valitud!=null){ if((e.getModifiersEx() & e.SHIFT_DOWN_MASK) !=0 ){ sonad.remove(valitud); //shift all kustutab } else { tf.setText(valitud.kysiTekst()); } } 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 void actionPerformed(ActionEvent e){ if(e.getSource()==tf && valitud!=null){ valitud.muudaTekst(tf.getText()); repaint(); } } public static void main(String[] arg){ JFrame f=new JFrame("Sõna paigutus"); f.getContentPane().add(new SonaPaigutus5()); f.setSize(400, 300); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }