1 /* File: PictureTime.java 2 * 3 * This example illustrates the hierarchical use 4 * of components in a grid layout. 5 * This version uses Java 1.1 event model. 6 * New version with colored circles on the canvas by Mehmet Sen 10/97. 7 */ 8 9 import java.awt.*; 10 import java.awt.event.*; 11 12 public class PictureTime extends java.applet.Applet { 13 14 /* instance variable to hold the subpanel component */ 15 ControlPanel control; 16 17 /* instance variable to hold the canvas component */ 18 Display displaycanvas; 19 20 21 public void init() { 22 23 /* using Border layout with gap of 5 points separating each component */ 24 setLayout(new BorderLayout(5,5)); 25 setBackground(Color.orange); 26 displaycanvas = new Display(0); /* creating the canvas component */ 27 displaycanvas.setBackground(Color.blue); 28 displaycanvas.start(); 29 30 /* creating instance for PictureControl panel */ 31 control = new ControlPanel(this); 32 33 /* adding the components to the panel */ 34 add(displaycanvas,BorderLayout.CENTER); 35 add(control,BorderLayout.NORTH); 36 } 37 38 39 void changeCanvasColor(String category) { 40 if (category.equals("Button 1")) 41 displaycanvas.draw(1,Color.green); 42 else if (category.equals("Button 2")) 43 displaycanvas.draw(2,Color.cyan); 44 else if (category.equals("Button 3")) 45 displaycanvas.draw(3,Color.pink); 46 else displaycanvas.draw(0,Color.black); 47 } 48 } 49 50 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ 51 /* this class is a subclass of Panel */ 52 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ 53 class ControlPanel extends Panel implements ActionListener 54 { 55 /* instance variable to hold a pointer to calling class */ 56 PictureTime outerparent; 57 58 /* the three control buttons */ 59 Button b1,b2,b3; 60 61 ControlPanel(PictureTime target) { 62 this.outerparent = target; 63 Font font = new Font("SansSerif",Font.BOLD,16); 64 65 setLayout(new GridLayout(1,3,10,0)); 66 setBackground(Color.orange); 67 68 /* putting the buttons */ 69 b1=new Button("Button 1"); 70 add(b1); 71 b1.setForeground(Color.black); 72 b1.setFont(font); 73 b1.setBackground(Color.green); 74 b1.addActionListener(this); 75 76 b2=new Button("Button 2"); 77 add(b2); 78 b2.setForeground(Color.blue); 79 b2.setBackground(Color.cyan); 80 b2.addActionListener(this); 81 b2.setFont(font); 82 83 b3=new Button("Button 3"); 84 add(b3); 85 b3.setForeground(Color.black); 86 b3.setBackground(Color.pink); 87 b3.addActionListener(this); 88 b3.setFont(font); 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 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ 103 class Display extends Canvas implements Runnable 104 { 105 int Target; 106 Thread animate; 107 Display(int _target) 108 { 109 Target=_target; 110 } 111 public void draw(int _target,Color bg) { 112 setBackground(bg); 113 Target=_target; 114 repaint(); 115 } 116 public void start() { 117 if (animate==null){ 118 animate=new Thread(this); 119 animate.start(); 120 } 121 } 122 public void stop() { 123 if (animate!=null){ 124 animate.stop(); 125 animate=null; 126 } 127 } 128 129 public void run() { 130 while(true){ 131 try { 132 Thread.sleep(100); 133 } 134 catch (InterruptedException e) { 135 } 136 repaint(); 137 } 138 } 139 140 public void paint(Graphics g) 141 { 142 int X,Y,source_x,source_y,target_x,target_y; 143 int h=20; 144 int w=20; 145 int i=0; 146 int step=h*4/3; 147 X=getSize().width; 148 Y=getSize().height; 149 source_x=X/3; 150 source_y=Y-20; 151 target_x=X/6+(X/3)*(Target-1); 152 target_y=-20; 153 for (i=source_y;i>Y*8/9;i-=step+8){ 154 g.setColor(Color.black); 155 g.fillOval(source_x-5,i-5,w+10,h+10); 156 g.setColor(new Color((float)Math.random(),(float)Math.random(),(float)Math.random())); 157 g.fillOval(source_x,i,w,h); 158 } 159 for (int y=i,x=source_x;y>target_y;){ 160 g.setColor(Color.black); 161 g.fillOval(x-5,y-5,w+10,h+10); 162 g.setColor(new Color((float)Math.random(),(float)Math.random(),(float)Math.random())); 163 g.fillOval(x,y,w,h); 164 if(y>target_y) y-=step; 165 if((x<target_x+15)&&(x>target_x-15)) y-=8; 166 else if(x<target_x) x+=step; 167 else x-=step; 168 } 169 } 170 171 } 172 173 174 175 176