A Sample Simulator Applet
Let’s create a sample applet class called
SimulatorSample that uses the Beans we created for
the simulator. The applet will create instances of
Temperature, Thermostat,
Boiler, and Cooler, and make
the required connections so that they work together. This is a pretty
simple process. All of the work is done in the applet’s
init() method. Everything else is handled by the
objects themselves.
import java.applet.*;
import java.awt.*;
import BeansBook.Simulator.*;
import java.beans.*;
// the SimulatorSample applet class
public class SimulatorSample extends Applet
{
// the applet init method
public void init()
{The applet uses a simple flow layout, which is set up by calling the
setLayout() method. Next, the simulator Beans are
created by calling their default constructors. The three visual Beans
are then added to the applet by passing each as a parameter to the
applet’s add() method.
// use a flow layout, with components centered, and a 20 pixel
// separation between components
setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
// create a temperature
Temperature t = new Temperature();
// create a thermostat
Thermostat th = new Thermostat();
// create a cooler
Cooler c = new Cooler();
// create a boiler
Boiler b = new Boiler();
// add the thermostat, cooler, and boiler to the applet
add(th);
add(c);
add(b);Now that all of the objects have been created, we need to wire them
up. The Thermostat monitors changes to the
Temperature object’s
Temperature property ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access