1  /* PictureTime.java - This example illustrates the hierarchical use
  2     of components in a grid layout.
  3     This version uses Java 1.1 event model.
  4     Author:  Meryem Ispirli  Revised:  Nancy McCracken 7/21/97
  5  */
  6  
  7  import java.awt.*;
  8  import java.awt.event.*;
  9  
 10  public class PictureTime extends java.applet.Applet {
 11  
 12  /* instance variable to hold the subpanel component */  
 13    ControlPanel control;
 14  
 15  /* instance variable to hold the canvas component */
 16    Canvas displaycanvas;
 17  
 18  
 19    public void init() {  
 20      
 21    /* using grid layout with gap of 15 points separating each component */
 22      setLayout(new GridLayout(1,2,15,15)); 
 23     
 24      displaycanvas = new Canvas();  /* creating the canvas component */
 25      displaycanvas.setBackground(Color.blue); 
 26      
 27    /* creating instance for PictureControl panel */
 28      control = new ControlPanel(this);
 29  
 30    /* adding the components to the panel */
 31      add(displaycanvas);  
 32      add(control);
 33    }
 34  
 35    
 36    void changeCanvasColor(String category) {
 37      if (category.equals("Turkiye")) 
 38                  displaycanvas.setBackground(Color.magenta);
 39      else if (category.equals("San Diego")) 
 40                  displaycanvas.setBackground(Color.cyan);
 41      else if (category.equals("Syracuse")) 
 42                  displaycanvas.setBackground(Color.pink);
 43      else displaycanvas.setBackground(Color.black);
 44    }
 45  }
 46  
 47  /* this class is a subclass of Panel */
 48  
 49  class ControlPanel extends Panel implements ActionListener
 50  { 
 51  /* instance variable to hold a pointer to calling class */
 52    PictureTime outerparent;
 53  
 54  /* the three control buttons */
 55    Button t, d, s;   
 56    
 57    ControlPanel(PictureTime target) {
 58      this.outerparent = target;
 59      setLayout(new GridLayout(3,1,0,60));
 60      
 61      /* putting the buttons */
 62      setBackground(Color.lightGray);
 63  
 64      t=new Button("Turkiye");
 65      add(t);
 66      t.setForeground(Color.white);
 67      t.setBackground(Color.magenta);
 68      t.addActionListener(this);
 69  
 70      d=new Button("San Diego");
 71      add(d);
 72      d.setForeground(Color.blue);
 73      d.setBackground(Color.cyan);
 74      d.addActionListener(this);
 75  
 76      s=new Button("Syracuse");
 77      add(s);
 78      s.setForeground(Color.black);
 79      s.setBackground(Color.pink);
 80      s.addActionListener(this);
 81  
 82    }
 83    
 84    public void actionPerformed(ActionEvent evt) {
 85  /* getActionCommand will return label of the button clicked
 86     or we could have used getSource() to return pointer to the button,
 87     i.e. either t, d, or s */
 88  
 89      String label = evt.getActionCommand();
 90      this.outerparent.changeCanvasColor(label);
 91    }
 92  }
 93  
 94