1 /* PictureTime.java - This example illustrates the hierarchical use 2 of components in a grid layout. 3 Author: Meryem Ispirli Revised: Nancy McCracken 7/21/97 4 */ 5 6 import java.awt.*; 7 8 public class PictureTime extends java.applet.Applet { 9 10 /* instance variable to hold the subpanel component */ 11 ControlPanel control; 12 13 /* instance variable to hold the canvas component */ 14 Canvas displaycanvas; 15 16 17 public void init() { 18 19 /* using grid layout with gap of 15 points separating each component */ 20 setLayout(new GridLayout(1,2,15,15)); 21 22 displaycanvas = new Canvas(); /* creating the canvas component */ 23 displaycanvas.setBackground(Color.blue); 24 25 /* creating instance for PictureControl panel */ 26 control = new ControlPanel(this); 27 28 /* adding the components to the panel */ 29 add(displaycanvas); 30 add(control); 31 } 32 33 public Insets insets() { 34 return new Insets(20,0,20,0); 35 } 36 37 void changeCanvasColor(String category) { 38 if (category.equals("Turkiye")) 39 displaycanvas.setBackground(Color.magenta); 40 else if (category.equals("San Diego")) 41 displaycanvas.setBackground(Color.cyan); 42 else if (category.equals("Syracuse")) 43 displaycanvas.setBackground(Color.pink); 44 else displaycanvas.setBackground(Color.black); 45 } 46 } 47 48 /* this class is a subclass of Panel */ 49 50 class ControlPanel extends Panel { 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 t=new Button("Turkiye"); 64 add(t); 65 t.setForeground(Color.white); 66 t.setBackground(Color.magenta); 67 d=new Button("San Diego"); 68 add(d); 69 d.setForeground(Color.blue); 70 d.setBackground(Color.cyan); 71 s=new Button("Syracuse"); 72 add(s); 73 s.setForeground(Color.black); 74 s.setBackground(Color.pink); 75 } 76 77 public boolean action(Event evt, Object arg) { 78 if (evt.target instanceof Button) 79 this.outerparent.changeCanvasColor((String)arg); 80 return true; 81 } 82 } 83 84