// File: Paint.java // // Developed by Mehmet Sen. Spring 1996 // Ported to Java 1.1 by Ozgur Balsoy. Fall 1997 import java.awt.*; import java.awt.event.*; import java.applet.*; public class Paint extends Applet implements ActionListener // must overwrite actionPerformed() method { Label Status; DrawCanvas c; public void init() { setLayout(new BorderLayout()); setBackground(Color.gray); setForeground(Color.black); Panel w=new Panel(); w.setLayout(new GridLayout(7,0)); Button b; w.add(b=new Button("Rect")); b.addActionListener(this); // the applet is one of the listeners w.add(b=new Button("Round")); b.addActionListener(this); // the applet is one of the listeners w.add(b=new Button("Circ")); b.addActionListener(this); // the applet is one of the listeners w.add(b=new Button("Line")); b.addActionListener(this); // the applet is one of the listeners w.add(b=new Button("Draw")); b.addActionListener(this); // the applet is one of the listeners w.add(b=new Button("Redraw")); b.addActionListener(this); // the applet is one of the listeners w.add(b=new Button("Clear")); b.addActionListener(this); // the applet is one of the listeners add(BorderLayout.WEST, w); Status=new Label("Status Line"); Panel p=new Panel(); p.setBackground(Color.gray); p.add(Status); add(BorderLayout.SOUTH,p); c=new DrawCanvas(); c.setBackground(Color.blue); add(BorderLayout.CENTER, c); } public void paint(Graphics g) { c.paint(g); } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); // We want to change the label of the button, // but getSource function returns an Object type object. // By type casting, we use it as if it is a button object. // Actually, it is a button, but getSource returns it // as an Object type object. // Note: We cannot use type casting if we are not sure // that the returning object is a button. In this case, // it is safe to use the following code: // if (e.getSource() instanceof Button) { // Button b=(Button)e.getSource(); // ... Button b=(Button)e.getSource(); Status.setText(command); if (command.equals("Draw")) { b.setLabel("Fill"); c.fill=false; } else if (command.equals("Fill")) { b.setLabel("Draw"); c.fill=true; } else c.setDrawMode(command); } }