// AppletClient.java
import java.rmi.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class AppletClient extends Applet 
    implements ActionListener { 
    Count remCount;
    TextField tfCnt;
    Button bStart, bGet;
    String bslabel = "Start";
    String bglabel = "Get";
    public void init() { 
	try { 
	    setLayout(new GridLayout(2,2));
	    add(new Label("Count:"));
	    tfCnt = new TextField(7);
	    tfCnt.setEditable(false);
	    add(tfCnt);
	    bStart = new Button(bslabel); 
	    bStart.addActionListener(this);
	    bGet = new Button(bglabel);
	    bGet.addActionListener(this);
	    add(bStart);
	    add(bGet);
	    showStatus("Binding remote object");
	    remCount = (Count) Naming.lookup("Count001");
	    tfCnt.setText(Integer.toString(remCount.get()));
	}
	catch (Exception e) {
	    e.printStackTrace();
	}
    }

    public void paint() {
	try {
	    tfCnt.setText(Integer.toString(remCount.get()));
	}
	catch (Exception e) {
	    e.printStackTrace();
	}
    }

    public void actionPerformed (ActionEvent ev) {
	try {
	    String botao = ev.getActionCommand();
	    if (botao.equals(bslabel)) {
		showStatus("Incrementing...");
		for (int i = 0 ; i < 1000 ; i++ ) 
		    remCount.increment();
		showStatus("Done");
	    }
	    else {
		showStatus("Current count");
		paint();
	    }
	}
	catch (Exception e) {
	    e.printStackTrace();
	}
    }
}