

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

public class HelloWorld3bis extends Applet {

   Button theButton ;
   MyCanvas theCanvas ;
   Panel thePanel ;

   Color [] someColors = {
      Color.black, Color.red, Color.green,
      Color.blue, Color.magenta } ;
   int colorIndex ;

   public void init() {

      setLayout( new BorderLayout() ) ;
      thePanel = new Panel() ;
      add("North",thePanel) ;
      theCanvas = new MyCanvas() ;
      add("Center",theCanvas) ;

      theButton = new Button("Change Color") ;
      thePanel.add(theButton) ;
      theCanvas.setMessage(getParameter("message")) ;
      validate() ;
      
   }

   public boolean action( Event e, Object arg ) {
      if ( e.target==theButton ) {
         changeColor() ;
         return true ;
      }
      return false ;
   }

   synchronized private Color currentColor() {
      return someColors[colorIndex] ;
   }

   synchronized private void changeColor() {
      if ( ++colorIndex == someColors.length ) colorIndex = 0 ;
      theButton.setForeground( currentColor() ) ;
      thePanel.repaint() ;
      theCanvas.setColor(currentColor()) ;
      theCanvas.repaint() ;
   }

}

class MyCanvas extends Canvas {

   Color currentColor ;
   int messageX=125, messageY=125 ;
   String theMessage ;

   void setColor(Color c ) {
      currentColor = c ;
      repaint() ;
   }

   void setMessage( String s ) {
      theMessage = s ;
   }
   
   public void paint(Graphics gc) {
      gc.setColor(currentColor) ;
      gc.drawString(theMessage,messageX,messageY) ;
   }

   public boolean mouseDrag( Event e, int x, int y ) {
      messageX = x ;
      messageY = y ;
      repaint() ;
      return true ;
   }

}
