Timer in SWT

T

trispo

Hallo Leute,

habe folgendes Problem:

ich möchte in SWT eine Grafik mittels for-Schleife aktualisieren. Das funktioniert auch schon, aber ich möchte nun, dass ich jeden Schleifendurchlauf sehen und nicht nur das Endergebnis.
Gibt es in SWT irgendwie ein wait() oder so eine Art Timer-Funktion? Wie benutze ich die in meine for-Schleife?

Bin absoluter Newbie, also entschludigt, wenn ich so banale Sachen fragen!
 
Hallo!

Schau mal hier:
Code:
/*
 * Created on 27.12.2004@22:51:16
 *
 * TODO Licence info
 */
package de.tutorials;

import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * @author Administrator
 * 
 * TODO Explain what I do...
 */
public class SWTGFXUpdate {

	private int x;

	private int y = 120;
	
	private final static long SLEEP = 150L;

	public static void main(String[] args) {
		new SWTGFXUpdate().run();
	}

	/**
	 * 
	 */
	private void run() {
		Display display = new Display();
		Shell shell = new Shell();

		shell.setText("SWTResize");
		Canvas canvas = new Canvas(shell, SWT.NONE);
		canvas.setSize(320, 240);

		final GC gc = new GC(canvas);

		Timer timer = new Timer();

		timer.schedule(new TimerTask() {
			public void run() {
				int oldX = x;
				int oldY = y;

				x += 2;
				y = 120 + (int) (Math.sin(x) * 50);
				gc.drawLine(oldX, oldY, x, y);
			}
		}, 50L, SLEEP);

		shell.pack();
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.isDisposed()) {
				display.readAndDispatch();
			}
		}

		timer.cancel();

	}
}

Gruß Tom
 

Neue Beiträge

Zurück