Task 1
Add the instance variable count to the applet and define the
method increaseCount() so that it simply increments count
each time it's invoked.
increaseCount() should have the signature:
public void increaseCount() {
// update count variable
}
Task 2
Add the method getCount() so that it returns count
each time it's invoked.
getCount() should have the signature:
public int getCount() {
// return count variable
}
Task 3
The core event handling classes are in the java.awt.event
package. Import the entire contents of this package.
You can import the package using:
import java.awt.event.*;
Task 4
To handle the command button action event, add the applet itself as an ActionListener
target for the button. That is, the applet should implement this interface.
To the class definition, add:
implements ActionListener
In the init() method, add:
b.addActionListener(this);
This means that the current object is functioning as the listener, that is, the
target, for action events distributed by the source, in this case, the button b.
Thus, this object, that is, the applet, must define the method(s) specified in
the ActionListener interface.
Task 5
Define the actionPerformed() method so that it calls increaseCount()
to update the count instance variable and repaint() to display the
current count in the applet window. (You do not have to define repaint().)
Implement your actionPerformed method as follows:
public void actionPerformed(ActionEvent e) {
increaseCount();
repaint();
}
Do not call paint() directly; instead, use repaint(),
which asks AWT to call paint() when possible.
Task 6
Define the paint() method so that it uses drawString(),
from the Graphics object that's
passed as an argument, to
display the current count. Your paint() method will be called by repaint()
in actionPerformed().
paint() should be similar to the following:
public void paint(Graphics g) {
g.drawString("Count: " + getCount(), 20, 50);
}
The environment that instantiates the applet, typically, a browser, will
invokes the paint() method when the window has been exposed
(becomes out of date). This host environment instantiates a Graphics
object Graphics and passes it as
an argument to paint().
Thus, paint() can use the Graphics object for
drawing operations in the applet's area of the browser's window, in this case,
we use the method drawString().