import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class SonaPaigutus5 extends JPanel implements MouseListener, MouseMotionListener, 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(){ setLayout(new BorderLayout()); sonad.add(s1); sonad.add(s2); add(tf, BorderLayout.SOUTH); tf.addActionListener(this); addMouseListener(this); addMouseMotionListener(this); } public void paintComponent(Graphics g){ super.paintComponent(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){ liigutaValitudSona(e); } public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void liigutaValitudSona(MouseEvent e){ if(valitud!=null){ valitud.muudaAsukoht(e.getX(), e.getY()); repaint(); } } public void mouseMoved(MouseEvent e){} public void mouseDragged(MouseEvent e){ liigutaValitudSona(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); } }