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
 
Training Index

Introduction to the JavaBeans API, Magercise 4
The StickFigure Bean with Control Methods

by John Zukowski, MageLang Institute

[Help | API Docs | Short Course | Magercises]

This Magercise focuses on design issues. The primary task is to add a state variable, surprised, to the StickFigure Bean, plus access/control methods for turning the surprise state on and off. Secondary tasks include building, packaging, and loading the Bean into a Bean-aware tool for verification.

Magercise 4 Prerequisites

Skeleton Code

Tasks

  1. The first step is to create a working directory somewhere on your filesystem, hereafter referred to as workingdir.

  2. Next, create the directory stickfigure (all lowercase characters) directly under workingdir.

  3. Next, (1) save all the skeleton files, except the *.java files, within workingdir and (2) save the *.java skeleton files within stickfigure, that is, within workingdir/stickfigure.

  4. The next, and primary, task is to add surprise functionality to the StickFigure Bean. You must implement the addSurprise() and removeSurprise() control methods, as well as the boolean variable surprised (note the "d" ending, for the variable only), which is manipulated by these two methods. This variable should be initialized to false.

    The surprise state logic is that if a StickFigure Bean instance is in the surprised state, the drawing operations will draw the mouth differently. Note that the control methods must invoke repaint() after setting the surprised variable. Do not substitute a setSurprise() method for these two control methods--the surprise state is not a property.

    The logic for handling the surprised state is already incorporated into the Bean's drawing operations--for this reason, you must use the variable name surprised. The point of this Magercise is to add the control methods for externally manipulating the state of surprise, and then connect these methods to external controls (command buttons) from a development tool.

  5. Compile all JavaTM source files, *.java, from workingdir by including the stickfigure directory in the path specification.

  6. Build StickFigure.jar from workingdir by executing the file MakeStickFigureJar.bat (Windows) or MakeStickFigureJar.sh (UNIX).

  7. Next, load the StickFigure Bean into the BeanBox (or a comparable tool) using "LoadJar..." from the BeanBox "File" menu.

  8. The next task is to configure surprise and no-surprise controls for a StickFigure Bean. First, select and place a StickFigure and two OurButton Beans on the worksheet.

  9. Next, change the label for either OurButton Bean to "Surprise Me" and connect its action-performed functionality to the StickFigure Bean's add-surprise functionality.

  10. Next, change the label for the other OurButton Bean to "No More Surprises" and connect its action-performed functionality to the StickFigure Bean's remove-surprise functionality. After setting up these connections, you will be able to use the two command buttons to control the stick figure's state of surprise.

Where help exists, the task numbers above are linked to the step-by-step help page.

Note that the "mood" property displays the integer value -1. The set of possible values includes -1, -2, and -3. The meaningless magic numbers, of -1, -2, and -3, are unacceptable for Beans designed to be used by other programmers; hence, in an upcoming Magercise, you can add a custom property editor that displays meaningful string values.

Solution Source

Demonstration

After loading the StickFigure Bean, it should appear in the palette, from where it can be selected and instantiated on the worksheet:

Magercise 5
Short Course
About This Short Course

Copyright © 1998-1999 MageLang Institute. All Rights Reserved.


[ This page was updated: 27-Oct-99 ]

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-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.