0

Optional Ref (Regarding bulding gui and widgets in java)

AS said here i am making a simple clock widget. I chose SWt because i found it easier to learn and implement. The widget consists of several layers of concentric circles to impart different colors to each layer. The color of each layer depends on the time of the day and is controlled by a color function. The layers are ready but color function still needs to be made.

I was going through the docs of Java and found that all graphic objects ( like rectangles and circles) must be manually disposed to free system resources. Now my basic problem is this :

Basically i want the widget to run indefinitely until the window containing widget is closed( because there is a minute layer and hour layer which change colors). How will i free the system resources and will the widget be a memory monster coz of infinteness ? Please answer with ref to Swt.

Additionally i wanted to know if this tyoe of animation strategy is suitable for this widget ? If not please suggest other alternatives keeping in mind my beginner level.

Community
  • 1
  • 1
Plutonium smuggler
  • 329
  • 5
  • 6
  • 17

1 Answers1

1

For the clock you need:

  • A widget to display the time (for example org.eclipse.swt.widgets.Canvas with a PaintListener to draw the clock)
  • A (daemon) thread which redraws your clock each second. The redraw call must be delegated to the EDT (event dispatch thread)

The thread should run as long as your widget isn't disposed. To clean up any resources (fonts, colors, etc.) when your widget is disposed, use DisposeListener.

Code template:

public class ClockWidget extends Canvas {

    public ClockWidget (Composite parent, int style) {
        super(parent, style | SWT.DOUBLE_BUFFERED);

        addPaintListener(new PaintListener() {

            @Override
            public void paintControl (PaintEvent e) {
                GC gc = e.gc;
                // paint clock on the graphics context
            }
        });
        addDisposeListener(new DisposeListener() {

            @Override
            public void widgetDisposed (DisposeEvent e) {
                // dispose all fonts and colors you created
            }
        });

        final Display display = Display.getCurrent();
        Thread timer = new Thread() {

            @Override
            public void run () {
                while (!isDisposed()) {
                    display.syncExec(new Runnable() {

                        @Override
                        public void run () {
                            if (!isDisposed()) {
                                redraw();
                            }
                        }
                    });
                    long msToNextSec = 1000 - (System.currentTimeMillis() % 1000);
                    try {
                        Thread.sleep(msToNextSec);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        timer.setPriority(Thread.MIN_PRIORITY);
        timer.setDaemon(true);
        timer.start();
    }
}
Peter Walser
  • 15,208
  • 4
  • 51
  • 78