Exercises

  • Exercise 15-1. Chapters 10, 12, and 13 contain examples of Swing components that allow the user to scribble with the mouse. Choose one of these classes, rename it to ScribbleBean, and package it in a JAR file (along with any other example classes it may require). Now install it in a beanbox application of your choice, to demonstrate that it works. Give your bean an erase( ) method that erases the scribbles, and use the beanbox to create a push button of some sort that invokes this method.

  • Exercise 15-2. Modify your ScribbleBean bean so that it has color and line-width properties that specify the color and width of the lines used for the scribbles. Repackage the bean and test the properties in a beanbox.

  • Exercise 15-3. An application that uses a ScribbleBean bean might want to be notified each time the user completes a single “stroke” of the scribble (i.e., each time the user clicks, drags, and then releases the mouse). For example, an application might make an off-screen copy of the scribble after each stroke, so that it could implement an undo facility. In order to provide this kind of notification, modify your ScribbleBean bean to support a “stroke” event. Define a simple StrokeEvent class and StrokeListener interface. Modify the ScribbleBean bean so that it allows registration and removal of StrokeListener objects, and so that it notifies all registered listeners each time a stroke of the scribble is complete. Regenerate the bean’s JAR file so that it includes the StrokeEvent ...

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.