Our new component is called the Colors Bean. It appears as either a rectangle or ellipse that is filled with a color. A color is chosen at random when the Bean begins execution. A public method can be invoked to change it. Each time the mouse is clicked on the Bean, another random color is chosen. There is one boolean read/write property that determines the shape.
The BDK is used to lay out an application with one instance of the Colors Bean and one instance of the OurButton Bean. The button is labeled “Change.” Each time it is pressed, the color changes.
SOFTWARE DEVELOPMENT
USING JAVA
Create a New Bean
Here are the steps that you must follow to create a new Bean:
1. Create a directory for the new Bean.
2. Create the Java source file(s).
3. Compile the source file(s).
4. Create a manifest file.
5. Generate a JAR file.
6. Start the BDK.
7. Test.
The following sections discuss each of these steps in detail.
Create a Directory for the New Bean
You need to make a directory for the Bean. To follow along with this example, create c:\bdk\demo\sunw\demo\colors. Then change to that directory.
The Colors and OurButton Beans
Create the Source File for the New Bean
The source code for the Colors component is shown in the following listing. It is located in the file Colors.java.
The import statement at the beginning of the file places it in the package named sunw.demo.colors. Recall from Chapter 9 that the directory hierarchy corresponds to the package hierarchy. Therefore, this file must be located in a subdirectory named sunw\demo\colors relative to the CLASSPATH environment variable.
The color of the component is determined by the private Color variable color, and its shape is determined by the private boolean variable rectangular.
The constructor defines an anonymous inner class that extends MouseAdapter and
overrides