import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Tasto3 extends JApplet implements ActionListener {

 Display disegno;
 Color c;
 JButton cancella,rosso,verde,blu;
 JPanel b;
 boolean cancellaDisegno; 

 public void init(){
  disegno = new Display();
  cancellaDisegno=false;
  getContentPane().add(disegno, BorderLayout.CENTER);
  b = new JPanel();
  rosso = new JButton("Rosso");
  rosso.addActionListener(this);
  b.add(rosso);
  verde = new JButton("Verde");
  verde.addActionListener(this);
  b.add(verde);
  blu = new JButton("Blu");
  blu.addActionListener(this);
  b.add(blu);
  getContentPane().add(b, BorderLayout.SOUTH); 
  cancella = new JButton("Cancella");
  cancella.addActionListener(this);
  getContentPane().add(cancella, BorderLayout.NORTH); 
 }

 public void actionPerformed(ActionEvent evt) {
    if(evt.getSource()==cancella){ 
    cancellaDisegno=true;
     disegno.repaint();
   } else {
     if(evt.getSource()==verde)c=Color.green;
     if(evt.getSource()==rosso)c=Color.red;
     if(evt.getSource()==blu)c=Color.blue;
     disegno.repaint();
     }
   }

class Display extends JPanel {
 public void paintComponent(Graphics g){
   super.paintComponent(g);
   g.setColor(c);
   if(!cancellaDisegno)g.drawString("CiaoATutti",5,25);
    else cancellaDisegno=false;
  }
 }
 }
