28
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * D Port:
|
|
11 * Bill Baxter <wbaxter> at gmail com
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.snippets.Snippet275;
|
|
14
|
|
15 /*
|
|
16 * Canvas snippet: update a portion of a Canvas frequently
|
|
17 *
|
|
18 * For a list of all SWT example snippets see
|
|
19 * http://www.eclipse.org/swt/snippets/
|
|
20 */
|
|
21 import org.eclipse.swt.SWT;
|
|
22 import org.eclipse.swt.graphics.GC;
|
|
23 import org.eclipse.swt.graphics.Color;
|
|
24 import org.eclipse.swt.graphics.Point;
|
|
25 import org.eclipse.swt.graphics.Image;
|
|
26 import org.eclipse.swt.widgets.Display;
|
|
27 import org.eclipse.swt.widgets.Shell;
|
|
28 import org.eclipse.swt.widgets.Event;
|
|
29 import org.eclipse.swt.widgets.Listener;
|
|
30 import org.eclipse.swt.widgets.Canvas;
|
|
31
|
|
32 import java.lang.all;
|
|
33
|
|
34 import tango.util.Convert;
|
|
35
|
|
36 static String value;
|
|
37 public static void main () {
|
|
38 final int INTERVAL = 888;
|
|
39 final Display display = new Display ();
|
|
40 final Image image = new Image (display, 750, 750);
|
|
41 GC gc = new GC (image);
|
|
42 gc.setBackground (display.getSystemColor (SWT.COLOR_RED));
|
|
43 gc.fillRectangle (image.getBounds ());
|
|
44 gc.dispose ();
|
|
45
|
|
46 Shell shell = new Shell (display);
|
|
47 shell.setBounds (10, 10, 790, 790);
|
|
48 final Canvas canvas = new Canvas (shell, SWT.NONE);
|
|
49 canvas.setBounds (10, 10, 750, 750);
|
|
50
|
|
51 void onPaint (Event event) {
|
|
52 value = to!(char[])(System.currentTimeMillis ());
|
|
53 event.gc.drawImage (image, 0, 0);
|
|
54 event.gc.drawString (value, 10, 10, true);
|
|
55 }
|
|
56 canvas.addListener (SWT.Paint, dgListener(&onPaint));
|
|
57
|
|
58 display.timerExec (INTERVAL, new class Runnable {
|
|
59 void run () {
|
|
60 if (canvas.isDisposed ()) return;
|
|
61 // canvas.redraw (); // <-- bad, damages more than is needed
|
|
62 GC gc = new GC (canvas);
|
|
63 Point extent = gc.stringExtent (value ~ '0');
|
|
64 gc.dispose ();
|
|
65 canvas.redraw (10, 10, extent.x, extent.y, false);
|
|
66 display.timerExec (INTERVAL, this);
|
|
67 }
|
|
68 });
|
|
69 shell.open ();
|
|
70 while (!shell.isDisposed ()){
|
|
71 if (!display.readAndDispatch ()) display.sleep ();
|
|
72 }
|
|
73 image.dispose ();
|
|
74 display.dispose ();
|
|
75 }
|