/* File: 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 Border layout with gap of 5 points separating each component */ setLayout(new BorderLayout(5,5)); setBackground(Color.orange); 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.NORTH); } 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; Font font = new Font("SansSerif",Font.BOLD,16); setLayout(new GridLayout(1,3,10,0)); setBackground(Color.orange); /* putting the buttons */ b1=new Button("Button 1"); add(b1); b1.setForeground(Color.black); b1.setFont(font); b1.setBackground(Color.green); b1.addActionListener(this); b2=new Button("Button 2"); add(b2); b2.setForeground(Color.blue); b2.setBackground(Color.cyan); b2.addActionListener(this); b2.setFont(font); b3=new Button("Button 3"); add(b3); b3.setForeground(Color.black); b3.setBackground(Color.pink); b3.addActionListener(this); b3.setFont(font); } 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 step=h*4/3; X=getSize().width; Y=getSize().height; source_x=X/3; source_y=Y-20; target_x=X/6+(X/3)*(Target-1); target_y=-20; for (i=source_y;i>Y*8/9;i-=step+8){ g.setColor(Color.black); g.fillOval(source_x-5,i-5,w+10,h+10); g.setColor(new Color((float)Math.random(),(float)Math.random(),(float)Math.random())); g.fillOval(source_x,i,w,h); } for (int y=i,x=source_x;y>target_y;){ g.setColor(Color.black); g.fillOval(x-5,y-5,w+10,h+10); g.setColor(new Color((float)Math.random(),(float)Math.random(),(float)Math.random())); g.fillOval(x,y,w,h); if(y>target_y) y-=step; if((xtarget_x-15)) y-=8; else if(x