/* PictureTime.java - This example illustrates the hierarchical use of components in a grid layout. Author: Meryem Ispirli Revised: Nancy McCracken 7/21/97 */ import java.awt.*; 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); } public Insets insets() { return new Insets(20,0,20,0); } 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 { /* 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); d=new Button("San Diego"); add(d); d.setForeground(Color.blue); d.setBackground(Color.cyan); s=new Button("Syracuse"); add(s); s.setForeground(Color.black); s.setBackground(Color.pink); } public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) this.outerparent.changeCanvasColor((String)arg); return true; } }