//Intruder Alarm Simulation
//Prototype by John Fenton
//Framework for HND/HNC IPDS Programming Exercise
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class IntAlm2 extends Applet implements
ActionListener,ItemListener
{
private Panel displayP,displayP2,displayP3;
private Label labEC;
private TextField tf;
private Button okBut,but1,but2,but3,but4;
private String code = "";
private final String defaultCode = "1234";
private String userCode = null;
private boolean controlFlag = false;
private CheckboxGroup bGrp;
private Checkbox rBut1,rBut2,rBut3;
public IntAlm2( )
{
setLayout(new BorderLayout( ));
topPanel( );
add (displayP,BorderLayout.NORTH);
midPanel( );
add (displayP2,BorderLayout.CENTER);
controlPanel( );
}
public void topPanel( )
{
displayP = new Panel( );
labEC = new Label("Enter Code: ");
displayP.add(labEC);
tf = new TextField(7);
displayP.add(tf);
okBut = new Button("OK");
okBut.addActionListener(this);
displayP.add(okBut);
}
public void midPanel()
{
displayP2 = new Panel( );
displayP2.setLayout(new FlowLayout( ));
displayP2.setFont(new Font("SansSerif",
Font.PLAIN,20));
but1 = new Button("1");
but2 = new Button("2");
but3 = new Button("3");
but4 = new Button("4");
but1.addActionListener(this);
but2.addActionListener(this);
but3.addActionListener(this);
but4.addActionListener(this);
displayP2.add(but1);
displayP2.add(but2);
displayP2.add(but3);
displayP2.add(but4);
}
public void controlPanel( )
{
displayP3 = new Panel( );
bGrp = new CheckboxGroup( );
rBut1 = new Checkbox("Change code", false, bGrp);
rBut2 = new Checkbox("Disable alarm", false, bGrp);
rBut3 = new Checkbox("Dummy", false, bGrp);
rBut1.addItemListener(this);
rBut2.addItemListener(this);
displayP3.add(rBut1);
displayP3.add(rBut2);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource( ) == but1)
{
code = code + "1";
tf.setText(code);
}
if(ae.getSource( ) == but2)
{
code = code + "2";
tf.setText(code);
}
if(ae.getSource( ) == but3)
{
code = code + "3";
tf.setText(code);
}
if(ae.getSource( ) == but4)
{
code = code + "4";
tf.setText(code);
}
if(ae.getSource( ) == okBut)
{
code = tf.getText( );
if ((code.equals(defaultCode))||
(code.equals(userCode))||
(controlFlag == true))
{
if (rBut1.getState( ) == true)
{
userCode = code;
code = "";
waitaBit( );
tf.setText("New Code");
rBut3.setState(true);//switches off rBut1
waitaBit( );
disableController( );
tf.setText("");
}
else
{
enableController( );
}
if (rBut2.getState( ) == true)
{
}
}
}
}
public void enableController( )
{
add (displayP3,BorderLayout.SOUTH);
displayP3.setVisible(true);
validate( );
controlFlag = true;
}
public void disableController( )
{
displayP3.setVisible(false);
validate( );
controlFlag = false;
}
public void itemStateChanged(ItemEvent ie)
{
if (ie.getSource( ) == rBut1)
{
tf.setText("");
code = "";
}
}
public void waitaBit( )
{
long timeInMis = System.currentTimeMillis( );
long t = 0;
do
{
t = t + 1;
} while (System.currentTimeMillis( ) < timeInMis
+ 1000);
}
}
//end.
|