Although the ClockView
shows a single animated clock, creating an independent widget will allow the clock to be reused in other places.
Create a new class in the
com.packtpub.e4.clock.ui
package, calledClockWidget
, that extendsCanvas
.Create a constructor that takes a
Composite
parent and anint style
bits parameter, and pass them to the superclass:public ClockWidget(Composite parent, int style) { super(parent, style); }
Move the implementation of the
drawClock
method from theClockView
to theClockWidget
. Remove thePaintListener
references from theClockView
class.In the
ClockWidget
constructor, register aPaintListener
that delegates the call to thedrawClock
method:addPaintListener(this::drawClock);
Move the
TickTock
thread from theClockView
to theClockWidget
constructor; this will allow theClockWidget
to operate independently. Change any references forclock
to this:Runnable redraw = () -> { while (!this.isDisposed()) { this.getDisplay...