import java.applet.Applet; // need to inherit from built-in Applet
import java.awt.*; // Window I/O classes
import java.awt.event.*; // Event classes
// Dilip Barman October 13, 1997
// file hw1Applet.java
// Oct.13 Got UI working
// Oct.16 With some help from Kenny Hoff, figured out the event model!
// 1st java homework assignment, to display a message. We are to design
// a simple application that looks, initially, like:
//
// Display Message Applet
// +--------------------------------------------------+
// | +--------+ +--------+ |
// | | display| | clear | |
// | +--------+ +--------+ |
// | +----------------------------------------------+ |
// | |type message here | |
// | +----------------------------------------------+ |
// | +----------------------------------------------+ |
// | |message display area | |
// | +----------------------------------------------+ |
// +--------------------------------------------------+
//
// The user can type anything in the "type message here" (initial string); when
// s/he presses the "display" button, the content of the first box is copied
// to the second box. Whenever "clear" is pressed, both fields are cleared. The
// whole application is light bluish-gray in color, except for the input message
// field, which is white.
//
// Architecturally, this is designed as 2 panels, since these run in the overall
// context of the applet and not as independent windows.
//
// Since this is an applet, it is meant to be run in a web browser. First we compile
// the applet (javac hw1Applet.java) to get hw1Applet.class. We can
// try it out via appletviewer (appletviewer hw1Applet.java) (note file type .java)
// or call it from an html page that has tags that look like:
//
public class hw1Applet
extends Applet
implements ActionListener
// We inherit from Applet and implement ActionListener methods
// (i.e., respond to button events)
{
// Instance and class variable definitions
Panel topPanel = new Panel(); // panel for buttons
Panel mainPanel = new Panel(); // panel for two s
Button displayButton = new Button ("display");
Button clearButton = new Button ("clear");
TextField msgInputField = new TextField("* type message here *", 40);
TextField msgOutputField = new TextField("message display area", 40);
// Now we define methods. Applets look for 4 methods - init, start, stop, and destroy
// init() is called when the applet is loaded, start() when it starts execution (i.e.,
// right after init() and whenever we revisit this web page), stop() when we leave this
// web page, and destroy() when we leave the browser.
public void init()
{
// setTitle ("Dilip's Display Message Applet");
// Associate listeners to the two buttons. We'll implement the
// actions right here, so we call our own (this = self) methods.
displayButton.addActionListener(this);
clearButton.addActionListener(this);
setBackground (Color.lightGray);
setForeground (Color.black);
// Following two lines if we're not using separate panels - just add to applet
// add (displayButton);
// add (clearButton);
topPanel.add (displayButton);
topPanel.add (clearButton);
msgInputField.setBackground (Color.white);
msgInputField.setForeground (Color.black);
msgOutputField.setBackground (Color.lightGray);
msgOutputField.setForeground (Color.black);
msgOutputField.setEditable (false);
mainPanel.add (msgInputField);
mainPanel.add (msgOutputField);
// Now specify a layout manager - we'll use border layout
setLayout (new BorderLayout());
add ("North", topPanel);
add ("Center", mainPanel);
}
public void start()
{
System.out.println ("Starting applet ... \n");
}
public void stop()
{
System.out.println ("Stopping applet\n");
}
public void destroy()
{
System.out.println ("Destroying applet execution instance\n");
}
// Define an optional paint method. This method gets called and
// passed a pointer to the current graphics context whenever an
// object is exposed. The only reason I'm doing this is to add
// a string of text.
public void paint (Graphics g )
{
g.setColor (Color.black);
g.setFont (new Font ("Helvetica", Font.PLAIN, 14) );
g.drawString ("Dilip's Display Message Applet", 0, 0);
} // end paint
// Now define IMPLEMENTORS - override actionListener class's
// actionPerformed method. It gets called whenever there is an action -
// i.e., a button is pressed. This is the only method we need
// to implement. The basic logic is that if we've pressed displayButton,
// copy msgInputField to msgOutputField. Otherwise, clear msgOutputField.
public void actionPerformed ( ActionEvent e )
{
// Graphics g = this.getGraphics ();
if (e.getActionCommand() == "display")
{
msgOutputField.setText (msgInputField.getText());
} // end handling DISPLAY
else
{
msgInputField.setText ("");
msgOutputField.setText ("");
} // end handling CLEAR
} // end actionPerformed
} // end class definition