Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
Print Button
 

Help is available for each task.



    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().

Copyright 1996-2000 jGuru.com. All Rights Reserved.


Print Button
[ This page was updated: 14-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.