/* PictureTime.java - This example illustrates the hierarchical use of components in a grid layout. This version uses Java 1.1 event model. Author: Meryem Ispirli Revised: Nancy McCracken 7/21/97 */ import java.awt.*; import java.awt.event.*; public class PictureTime extends java.applet.Applet { /* instance variable to hold the subpanel component */ ControlPanel control; /* instance variable to hold the canvas component */ Canvas displaycanvas; public void init() { /* using grid layout with gap of 15 points separating each component */ setLayout(new GridLayout(1,2,15,15)); displaycanvas = new Canvas(); /* creating the canvas component */ displaycanvas.setBackground(Color.blue); /* creating instance for PictureControl panel */ control = new ControlPanel(this); /* adding the components to the panel */ add(displaycanvas); add(control); } void changeCanvasColor(String category) { if (category.equals("Turkiye")) displaycanvas.setBackground(Color.magenta); else if (category.equals("San Diego")) displaycanvas.setBackground(Color.cyan); else if (category.equals("Syracuse")) displaycanvas.setBackground(Color.pink); else displaycanvas.setBackground(Color.black); } } /* this class is a subclass of Panel */ class ControlPanel extends Panel implements ActionListener { /* instance variable to hold a pointer to calling class */ PictureTime outerparent; /* the three control buttons */ Button t, d, s; ControlPanel(PictureTime target) { this.outerparent = target; setLayout(new GridLayout(3,1,0,60)); /* putting the buttons */ setBackground(Color.lightGray); t=new Button("Turkiye"); add(t); t.setForeground(Color.white); t.setBackground(Color.magenta); t.addActionListener(this); d=new Button("San Diego"); add(d); d.setForeground(Color.blue); d.setBackground(Color.cyan); d.addActionListener(this); s=new Button("Syracuse"); add(s); s.setForeground(Color.black); s.setBackground(Color.pink); s.addActionListener(this); } public void actionPerformed(ActionEvent evt) { /* getActionCommand will return label of the button clicked or we could have used getSource() to return pointer to the button, i.e. either t, d, or s */ String label = evt.getActionCommand(); this.outerparent.changeCanvasColor(label); } }