To add an option for the ClockWidget
to have a different color, an instance must be obtained instead of the hardcoded BLUE
reference. Since Color
objects are Resource
objects, they must be disposed correctly when the widget is disposed.
To avoid passing in a Color
directly, the constructor will be changed to take an RGB
value (which is three int
values), and use that to instantiate a Color
object to store for later. The lifetime of the Color
instance can be tied to the lifetime of the ClockWidget
.
Add a private final
Color
field calledcolor
to theClockWidget
:private final Color color;
Modify the constructor of the
ClockWidget
to take anRGB
instance, and use it to instantiate aColor
object. Note that the color is leaked at this point, and will be fixed later:public ClockWidget(Composite parent, int style, RGB rgb) { super(parent, style); // FIXME color is leaked! this.color = new Color(parent.getDisplay(), rgb); ...
Modify the
drawClock
method to...