Disegna a mano libera

Tratto da: G. Callegarin – Java e Strutture dati – CEDAM.

Tramite la gestione degli eventi del mouse è possibile disegnare tratti di linee sul frame

import java.awt.*;
import java.awt.event.*;
 
public class ProvaDisegnoManoLibera
{
   public static void main(String[] args)
   {
      FrameManoLibera f=new FrameManoLibera("Disegno a mano libera");
                      f.setLocation(300,300);
                      f.setVisible(true);
   }
}

class FrameManoLibera extends    Frame
                      implements WindowListener,
                                 MouseListener,
                                 MouseMotionListener,
                                 ActionListener,
                                 ItemListener
{
    private Color[] cCOLORI=
    {
       Color.black    , Color.blue   , Color.cyan  , Color.darkGray, Color.gray, Color.green,
       Color.lightGray, Color.magenta, Color.orange, Color.pink    , Color.red , Color.white,
       Color.yellow 
    };
    private String[] sCOLORI=
    {
       "Nero"         , "Blu"    , "Azzurro"  , "Grigio scuro", "Grigio", "Verde" ,
       "Grigio chiaro", "Magenta", "Arancione", "Rosa"        , "Rosso" , "Bianco",
       "Giallo" 
    };
    
    private Choice   sceltaColore;
    private Color    coloreCorrente;         
    private int      lastX,
                     lastY;
    
    FrameManoLibera(String s)
    {
      this.setTitle(s);
      this.setSize(480,360);    
      this.setResizable(false);
      this.setLayout(new FlowLayout()); 
      this.addWindowListener(this);
      this.addMouseListener(this);
      this.addMouseMotionListener(this);
      sceltaColore  = new Choice();           sceltaColore.addItemListener(this);
                                              for(int i=0; i < sCOLORI.length;i++)
                                                  sceltaColore.add(sCOLORI[i]);
                                              this.add(sceltaColore);
      Button pulisci= new Button("PULISCI");  pulisci.addActionListener(this);
                                              this.add(pulisci);
      coloreCorrente=cCOLORI[0];
   }

   public void actionPerformed(ActionEvent e)   { Graphics cg=getGraphics();
                                                  cg.clearRect(0, 0, getWidth(), getHeight());    
                                                }
   public void itemStateChanged(ItemEvent e)    { int indice=sceltaColore.getSelectedIndex();
                                                  coloreCorrente=cCOLORI[indice];
                                                }
   public void windowClosing(WindowEvent e)     { dispose();
                                                }
   public void mousePressed(MouseEvent e)       { lastX=e.getX();
                                                  lastY=e.getY();
                                                }
   public void mouseDragged(MouseEvent e)       { Graphics cg=getGraphics();
                                                  cg.setColor(coloreCorrente);
                                                  int x=e.getX();
                                                  int y=e.getY();
                                                  cg.drawLine(lastX, lastY, x, y);
                                                  lastX=x;
                                                  lastY=y;
                                                }                     
   public void mouseReleased(MouseEvent e)      {} // eventi non utilizzati
   public void mouseEntered(MouseEvent e)       {}
   public void mouseExited(MouseEvent e)        {}
   public void mouseMoved(MouseEvent e)         {}
   public void windowClosed(WindowEvent e)      {}
   public void windowIconified(WindowEvent e)   {}
   public void windowDeiconified(WindowEvent e) {}
   public void mouseClicked(MouseEvent e)       {}
   public void windowActivated(WindowEvent e)   {}
   public void windowDeactivated(WindowEvent e) {}
   public void windowOpened(WindowEvent e)      {}
}

Osserva

WindowListener, MouseListener, MouseMotionListener, ActionListener, ItemListener