import java.applet.Applet; import java.awt.* ; public class OldVaporWare extends Applet { int numClientSent ; boolean connected ; Label labelClientSent ; Label labelServerSent ; Button buttonSendIt ; String buttonSendIt_ActionCommand ; Color waitColor ; Color readyColor ; // for simulated networking int numServerSent ; // end of simulated networking public void SendMessage() { ++ numClientSent ; labelClientSent.setText(Integer.toString(numClientSent)) ; buttonSendIt.setLabel("Transmitting") ; buttonSendIt.setBackground(waitColor) ; buttonSendIt_ActionCommand = "Waiting" ; repaint() ; // for simulated networking int sloop, rbump, counter ; sloop = 100 + (int) (Math.random() * 100.0) ; for (counter = sloop; counter >= 0 ; --counter ) { showStatus(Integer.toString(counter)) ; } showStatus("") ; ++numServerSent ; if (sloop % 7 == 0) { ++numServerSent ; } if (sloop % 21 == 0) { ConnectionFailed() ; } else { PacketReceived(numServerSent) ; } // end of simulated networking } public void ConnectionEstablished() { connected = true ; buttonSendIt.setLabel("Ready to fire") ; buttonSendIt.setBackground(readyColor) ; buttonSendIt_ActionCommand = "Ready" ; repaint() ; } public void ConnectionFailed() { connected = false ; buttonSendIt.setLabel("Connection failure") ; buttonSendIt.setBackground(Color.red) ; buttonSendIt_ActionCommand = "Failed" ; repaint() ; } public void PacketReceived(int newNum) { labelServerSent.setText(Integer.toString(newNum)) ; buttonSendIt.setLabel("Ready to fire") ; buttonSendIt.setBackground(readyColor) ; buttonSendIt_ActionCommand = "Ready" ; repaint() ; } public void init() { Font fixedFont = new Font("Monospaced", Font.PLAIN, 30); Font prettyFont = new Font("TimesRoman", Font.PLAIN, 20); waitColor = new Color(255, 100, 100) ; readyColor = new Color(100, 255, 255) ; numClientSent = 0 ; connected = false ; labelClientSent = new Label("0", Label.RIGHT) ; labelClientSent.setFont(fixedFont) ; // labelClientSent.setBackground(Color.yellow) ; labelServerSent = new Label("", Label.RIGHT) ; labelServerSent.setFont(fixedFont) ; // labelServerSent.setBackground(Color.orange) ; buttonSendIt = new Button("Trying to connect") ; buttonSendIt.setFont(prettyFont) ; buttonSendIt.setBackground(waitColor) ; buttonSendIt_ActionCommand = "Waiting" ; GridBagLayout gbl = new GridBagLayout() ; GridBagConstraints gbc = new GridBagConstraints() ; setLayout(gbl) ; gbc.fill = GridBagConstraints.BOTH ; gbc.gridx = 0 ; gbc.gridy = 0 ; gbc.weightx = 1.0 ; gbl.setConstraints(labelClientSent, gbc); add(labelClientSent) ; gbc.gridx = 1 ; gbl.setConstraints(labelServerSent, gbc); add(labelServerSent) ; gbc.gridx = 0 ; gbc.gridy = 1 ; gbc.gridwidth = 2 ; gbl.setConstraints(buttonSendIt, gbc); add(buttonSendIt) ; repaint() ; // for simulated networking int sloop, rbump, counter ; sloop = 1000 + (int) (Math.random() * 1000.0) ; for (counter = sloop; counter >= 0 ; --counter ) { showStatus(Integer.toString(counter)) ; } showStatus("") ; numServerSent = (int) (Math.random() * 100000.0) ; ConnectionEstablished() ; // end of simulated networking } public boolean handleEvent(Event e) { if (e.id == Event.ACTION_EVENT && e.target == buttonSendIt) { if (buttonSendIt_ActionCommand == "Ready") { SendMessage() ; } } return (super.handleEvent(e)) ; } }