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 * New Version with colored circles on the canvas by Mehmet Sen 10/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 Display displaycanvas; 17 18 19 public void init() { 20 21 /* using grid layout with gap of 15 points separating each component */ 22 setLayout(new BorderLayout(5,0)); 23 24 displaycanvas = new Display(0); /* creating the canvas component */ 25 displaycanvas.setBackground(Color.blue); 26 displaycanvas.start(); 27 28 /* creating instance for PictureControl panel */ 29 control = new ControlPanel(this); 30 31 /* adding the components to the panel */ 32 add(displaycanvas,BorderLayout.CENTER); 33 add(control,BorderLayout.EAST); 34 } 35 36 37 void changeCanvasColor(String category) { 38 if (category.equals("Button 1")) 39 displaycanvas.draw(1,Color.green); 40 else if (category.equals("Button 2")) 41 displaycanvas.draw(2,Color.cyan); 42 else if (category.equals("Button 3")) 43 displaycanvas.draw(3,Color.pink); 44 else displaycanvas.draw(0,Color.black); 45 } 46 } 47 48 49 50 51 52 53 54 /* this class is a subclass of Panel */ 55 56 class ControlPanel extends Panel implements ActionListener 57 { 58 /* instance variable to hold a pointer to calling class */ 59 PictureTime outerparent; 60 61 /* the three control buttons */ 62 Button b1,b2,b3; 63 64 ControlPanel(PictureTime target) { 65 this.outerparent = target; 66 setLayout(new BorderLayout(0,140)); 67 68 /* putting the buttons */ 69 setBackground(Color.lightGray); 70 71 b1=new Button("Button 1"); 72 add(b1,BorderLayout.NORTH); 73 b1.setForeground(Color.black); 74 b1.setBackground(Color.green); 75 b1.addActionListener(this); 76 77 b2=new Button("Button 2"); 78 add(b2,BorderLayout.CENTER); 79 b2.setForeground(Color.blue); 80 b2.setBackground(Color.cyan); 81 b2.addActionListener(this); 82 83 b3=new Button("Button 3"); 84 add(b3,BorderLayout.SOUTH); 85 b3.setForeground(Color.black); 86 b3.setBackground(Color.pink); 87 b3.addActionListener(this); 88 89 } 90 91 public void actionPerformed(ActionEvent evt) { 92 /* getActionCommand will return label of the button clicked 93 or we could have used getSource() to return pointer to the button, 94 i.e. either t, d, or s */ 95 String label = evt.getActionCommand(); 96 this.outerparent.changeCanvasColor(label); 97 } 98 } 99 100 101 // Display canvas show animated little balls from left towards pressed buton 102 class Display extends Canvas implements Runnable 103 { 104 int Target; 105 Thread animate; 106 Display(int _target) 107 { 108 Target=_target; 109 } 110 public void draw(int _target,Color bg) { 111 setBackground(bg); 112 Target=_target; 113 repaint(); 114 } 115 public void start() { 116 if (animate==null){ 117 animate=new Thread(this); 118 animate.start(); 119 } 120 } 121 public void stop() { 122 if (animate!=null){ 123 animate.stop(); 124 animate=null; 125 } 126 } 127 128 public void run() { 129 while(true){ 130 try { 131 Thread.sleep(100); 132 } 133 catch (InterruptedException e) { 134 } 135 repaint(); 136 } 137 } 138 139 public void paint(Graphics g) 140 { 141 int X,Y,source_x,source_y,target_x,target_y; 142 int h=20; 143 int w=20; 144 int i=0; 145 int wstep=h*4/3; 146 int hstep=h*4/3; 147 X=getSize().width; 148 Y=getSize().height; 149 source_x=0; 150 source_y=Y/3; 151 target_x=X; 152 if (Target==1 ) 153 target_y=5; 154 else if (Target==2) 155 target_y=Y/2; 156 else if (Target==3) 157 target_y=Y-20; 158 else { 159 target_x=X/2; 160 target_y=-50; 161 } 162 163 for (i=source_x;i<X/4;i+=wstep+8){ 164 g.setColor(Color.black); 165 g.fillOval(i-5,source_y-5,w+10,h+10); 166 g.setColor(new Color((float)Math.random(),(float)Math.random(),(float)Math.random())); 167 g.fillOval(i,source_y,w,h); 168 } 169 for (int x=i,y=source_y;x<target_x;){ 170 g.setColor(Color.black); 171 g.fillOval(x-5,y-5,w+10,h+10); 172 g.setColor(new Color((float)Math.random(),(float)Math.random(),(float)Math.random())); 173 g.fillOval(x,y,w,h); 174 if(x<target_x) x+=wstep; 175 if((y<target_y+10)&&(y>target_y-10)) x+=8; 176 else if(y<target_y) y+=hstep; 177 else y-=hstep; 178 } 179 } 180 181 } 182