import java.awt.*;

// An instance of this class models the desired-speed setting for the
// cruise control mechanism. An instance of this class has a window 
// (a Frame) that displays the desired speed. The desired speed is changed
// by the user by typing in the textField and pressing a button. An
// instance has its own thread of execution. Its method run appears in
// superclass ClockedFrame.
public class DesiredSpeed extends ClockedFrame {

// Components for the Frame
	Label label= new Label("Desired speed:   ");
	TextField textField= new TextField();
	Button button = new Button("Read speed");

private int speed;	// Desired speed
  
// Constructor: desired-speed control, using clock c
// and with initial desired speed s
public DesiredSpeed(SystemClock c, int s) {
    super(c, "Desired");
	speed = s;
	add("North", label);
	add("Center", textField);
	textField.setText("" + speed);
	add("South", button);
	resize(600, 200);
	pack();
    move(430, 100);
	show();
  	}

// Return the desired speed.
public synchronized int readValue() {
    notifyAll();
    return(speed);
  	}

// If a button was pressed, process it; otherwise, return
// result of super.action. The only button is "Read speed"
public boolean action(Event e, Object arg) {
	if (arg.equals(button.getLabel())) {
		// Handle press of "Read speed". Here, method getText
		// obtains the sequence of characters in component textField
		// on the Frame; trim removes whitespace at either end of
		// the String, and parseInt converts the String to an integer.
			String s= textField.getText().trim();
			speed= Integer.parseInt(s);
		return true;
		}
    return super.action(e, arg);
  	}
}

