import java.awt.* ;
import java.awt.event.* ;
import java.applet.* ;

public class sample03 extends Applet
                      implements ActionListener,
                                 ItemListener,
                                 AdjustmentListener,
                                 MouseListener {

  Button    bt = new Button("Reset") ;
  TextField tf = new TextField(10) ;
  TextArea  ta = new TextArea(10,40) ;
  Choice    ch = new Choice() ;
  int       colorIndex = 0 ;
  String[]  colorNames = {
    "Red", "Green", "Blue", "Yellow", "Magenta", "Cyan", "White", "Black"
    } ;
  Color[]   colorObj = {
    Color.red, Color.green, Color.blue, Color.yellow, Color.magenta,
    Color.cyan, Color.white, Color.black
    } ;
  Checkbox  cb1 = new Checkbox("Fortran") ;
  Checkbox  cb2 = new Checkbox("PL/I") ;
  Checkbox  cb3 = new Checkbox("Basic") ;
  Checkbox  cb4 = new Checkbox("C or C++") ;
  Checkbox  cb5 = new Checkbox("Java") ;
  CheckboxGroup cg = new CheckboxGroup() ;
  Checkbox  cg1 = new Checkbox("Never",    true, cg) ;
  Checkbox  cg2 = new Checkbox("Sometimes",false,cg) ;
  Checkbox  cg3 = new Checkbox("Often",    false,cg) ;
  Scrollbar sc = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 0, 100+10 ) ;
  DrawingCanvas myCanvas = new DrawingCanvas(300,225) ;

  public void init() {
    int i ;
    for( i=0 ; i<colorNames.length ; i++ )
      ch.addItem(colorNames[i]) ;
    ch.select(colorNames[colorIndex]) ;
    add(new Label("TextField:",Label.RIGHT)) ;  add(tf) ;
    add(new Label("BackGround:",Label.RIGHT)) ; add(ch) ;
    add(bt) ;
    add(cb1) ; add(cb2) ; add(cb3) ; add(cb4) ; add(cb5) ;
    add(cg1) ; add(cg2) ; add(cg3) ;
    add(sc) ;
    add(ta) ;
    add(myCanvas) ;
    
    tf.addActionListener(this) ;
    bt.addActionListener(this) ;
    ch.addItemListener(this) ;
    cb1.addItemListener(this) ;
    cb2.addItemListener(this) ;
    cb3.addItemListener(this) ;
    cb4.addItemListener(this) ;
    cb5.addItemListener(this) ;
    sc.addAdjustmentListener(this) ;
    addMouseListener(this) ;
  }

  public void paint(Graphics g) {
    int w, h, c ;
    w = getSize().width ;
    h = getSize().height ;
    g.setColor(colorObj[colorIndex]) ;
    g.fillRect(0,0,w,h) ;
  }
  
  // ActionListener
  public void actionPerformed(ActionEvent e) {
    if( e.getSource() == tf ) {
      ta.append("Caught: TextField\n") ;
    }
    else if( e.getSource() == bt ) {
      tf.setText("") ;
      colorIndex=0 ;
      ch.select(colorIndex) ;
      cb1.setState(false) ; cb2.setState(false) ; cb3.setState(false) ;
      cb4.setState(false) ; cb5.setState(false) ;
      cg1.setState(true) ;
      sc.setValue(50) ;
      ta.append("Caught: Button\n") ;
      repaint() ;
    }
  }

  // ItemListener
  public void itemStateChanged(ItemEvent e) {
    if( e.getSource() == ch ) {
      ta.append("Caught: Choice\n") ;
      colorIndex = ch.getSelectedIndex() ;
      repaint() ;
    }
    else if( e.getSource() instanceof Checkbox ) {
      ta.append("Caught: Checkbox ["+
                (cb1.getState() ? "ON ":"OFF ")+
                (cb2.getState() ? "ON ":"OFF ")+
                (cb3.getState() ? "ON ":"OFF ")+
                (cb4.getState() ? "ON ":"OFF ")+
                (cb5.getState() ? "ON ":"OFF ")+"]\n") ;
    }
  }

  // AdjustmentListener
  public void adjustmentValueChanged(AdjustmentEvent e) {
    if( e.getSource() == sc ) {
      ta.append("Caught: Scrollbar v="+sc.getValue()+"\n") ;
    }
  }

  // MouseListener (使用しないものでも定義は必要)  
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
  public void mousePressed(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
  public void mouseClicked(MouseEvent e) {
    ta.append("Clicked at ("+e.getX()+","+e.getY()+")\n") ;
  }
}

// Canvas から派生させたクラス(publicにはしない)
//   --  ひとつのファイルには public のクラスはひとつしか持てない
class DrawingCanvas extends Canvas
                           implements MouseListener, MouseMotionListener {
  public int initX=-1, initY=-1, lastX=-1, lastY=-1 ;
  
  public DrawingCanvas(int w, int h) {
    setSize(w,h) ;
    addMouseListener(this) ;
    addMouseMotionListener(this) ;
  }

  public void update(Graphics g) {
    paint(g) ;
  }
  public void paint(Graphics g) {
    g.drawLine( initX, initY, lastX, lastY ) ;
    initX = lastX ; initY = lastY ;
  }
  
  // MouseListener
  public void mousePressed(MouseEvent e) {
    initX = e.getX() ; initY = e.getY() ;
  }
  public void mouseReleased(MouseEvent e) {}
  public void mouseClicked(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
  // MouseMotionListener
  public void mouseMoved(MouseEvent e) {}
  public void mouseDragged(MouseEvent e) {
    lastX = e.getX() ; lastY = e.getY() ;
    repaint() ;
  }
}