1  // File:  Paint.java
  2  //
  3  // Developed by Mehmet Sen. Spring 1996
  4  // Ported to Java 1.1 by Ozgur Balsoy. Fall 1997
  5  
  6  import java.awt.*;
  7  import java.awt.event.*;
  8  import java.applet.*;
  9  
 10  public class Paint 
 11  		extends Applet 
 12  		implements ActionListener // must overwrite actionPerformed() method
 13  {
 14  	Label Status;
 15  	DrawCanvas c;
 16  	
 17      public void init() {
 18  		setLayout(new BorderLayout());
 19  
 20  		setBackground(Color.gray);
 21  		setForeground(Color.black);
 22  
 23  		Panel w=new Panel();
 24  		w.setLayout(new GridLayout(7,0));
 25  		
 26  		Button b;
 27  		w.add(b=new Button("Rect"));
 28  		b.addActionListener(this);			// the applet is one of the listeners
 29  		w.add(b=new Button("Round"));
 30  		b.addActionListener(this);			// the applet is one of the listeners
 31  		w.add(b=new Button("Circ"));
 32  		b.addActionListener(this);			// the applet is one of the listeners
 33  		w.add(b=new Button("Line"));
 34  		b.addActionListener(this);			// the applet is one of the listeners
 35  		w.add(b=new Button("Draw"));
 36  		b.addActionListener(this);			// the applet is one of the listeners
 37  		w.add(b=new Button("Redraw"));
 38  		b.addActionListener(this);			// the applet is one of the listeners
 39  		w.add(b=new Button("Clear"));
 40  		b.addActionListener(this);			// the applet is one of the listeners
 41  		add(BorderLayout.WEST, w);
 42  
 43  		Status=new Label("Status Line");
 44  		Panel p=new Panel();
 45  		p.setBackground(Color.gray);
 46  		p.add(Status);
 47  		add(BorderLayout.SOUTH,p);
 48  
 49  		c=new DrawCanvas();
 50  		c.setBackground(Color.blue);
 51  		add(BorderLayout.CENTER, c);
 52  	}
 53  
 54  	public void paint(Graphics g) {
 55  		c.paint(g);
 56  	}
 57  
 58  	public void actionPerformed(ActionEvent e) {
 59  	 	String command = e.getActionCommand();
 60  
 61  		// We want to change the label of the button,
 62  		// but getSource function returns an Object type object.
 63  		// By type casting, we use it as if it is a button object.
 64  		// Actually, it is a button, but getSource returns it 
 65  		// as an Object type object.
 66  		// Note: We cannot use type casting if we are not sure 
 67  		// that the returning object is a button. In this case, 
 68  		// it is safe to use the following code:
 69  		// if (e.getSource() instanceof Button) {
 70  		//		Button b=(Button)e.getSource();
 71  		//		...
 72  		Button b=(Button)e.getSource();
 73  
 74  		Status.setText(command);
 75  		if (command.equals("Draw")) {
 76  			b.setLabel("Fill");
 77  			c.fill=false;
 78  		} else if (command.equals("Fill")) {
 79  			b.setLabel("Draw");
 80  			c.fill=true;
 81  		} else c.setDrawMode(command);
 82  	}
 83  }
 84