/* PictureTime.java - This example illustrates the hierarchical use * of components in a grid layout. * This version uses Java 1.1 event model. * New Version with colored circles on the canvas by Mehmet Sen 10/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 */ Display displaycanvas; public void init() { /* using grid layout with gap of 15 points separating each component */ setLayout(new BorderLayout(5,0)); displaycanvas = new Display(0); /* creating the canvas component */ displaycanvas.setBackground(Color.blue); displaycanvas.start(); /* creating instance for PictureControl panel */ control = new ControlPanel(this); /* adding the components to the panel */ add(displaycanvas,BorderLayout.CENTER); add(control,BorderLayout.EAST); } void changeCanvasColor(String category) { if (category.equals("Button 1")) displaycanvas.draw(1,Color.green); else if (category.equals("Button 2")) displaycanvas.draw(2,Color.cyan); else if (category.equals("Button 3")) displaycanvas.draw(3,Color.pink); else displaycanvas.draw(0,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 b1,b2,b3; ControlPanel(PictureTime target) { this.outerparent = target; setLayout(new BorderLayout(0,140)); /* putting the buttons */ setBackground(Color.lightGray); b1=new Button("Button 1"); add(b1,BorderLayout.NORTH); b1.setForeground(Color.black); b1.setBackground(Color.green); b1.addActionListener(this); b2=new Button("Button 2"); add(b2,BorderLayout.CENTER); b2.setForeground(Color.blue); b2.setBackground(Color.cyan); b2.addActionListener(this); b3=new Button("Button 3"); add(b3,BorderLayout.SOUTH); b3.setForeground(Color.black); b3.setBackground(Color.pink); b3.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); } } // Display canvas show animated little balls from left towards pressed buton class Display extends Canvas implements Runnable { int Target; Thread animate; Display(int _target) { Target=_target; } public void draw(int _target,Color bg) { setBackground(bg); Target=_target; repaint(); } public void start() { if (animate==null){ animate=new Thread(this); animate.start(); } } public void stop() { if (animate!=null){ animate.stop(); animate=null; } } public void run() { while(true){ try { Thread.sleep(100); } catch (InterruptedException e) { } repaint(); } } public void paint(Graphics g) { int X,Y,source_x,source_y,target_x,target_y; int h=20; int w=20; int i=0; int wstep=h*4/3; int hstep=h*4/3; X=getSize().width; Y=getSize().height; source_x=0; source_y=Y/3; target_x=X; if (Target==1 ) target_y=5; else if (Target==2) target_y=Y/2; else if (Target==3) target_y=Y-20; else { target_x=X/2; target_y=-50; } for (i=source_x;itarget_y-10)) x+=8; else if(y