My first attempt at programming a
Thread; want an sinewave graph to run in a thread. Simplified code posted here, as long as the sinewave is running, the main window is unresponsive.
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class WBthreadBare {
protected Shell shell;
/**
* Launch the application.
*
* @param args
*/
public static void main(
String[] args) {
try {
WBthreadBare window = new WBthreadBare();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Thread Test");
// Trace trace = new Trace();
(new Trace()).start();
}
class Trace extends Thread {
public Trace() {
Run();
}
public void Run() {
shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
for (int b = 0; b < 720; b++) {
for (int a = 1; a < 120 * Math.PI; a++) {
double theta1 = +(b + a - 1) % 360 * 2 * Math.PI / 180;
double theta2 = +(b + a) % 360 * 2 * Math.PI / 180;
int y1 = ((int) (Math.sin((theta1)) * 1000)) / 15 + 90;
int y2 = ((int) (Math.sin((theta2)) * 1000)) / 15 + 90;
e.gc.setForeground(e.display
.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
e.gc.drawLine(a, y1, a + 1, y2);
e.gc.setForeground(e.display
.getSystemColor(SWT.COLOR_GREEN));
e.gc.drawLine(a - 1, y1, a, y2);
} // end for a
} // end for b
} // end paintControl
}); // end PaintListener
} // end Run()
} // end class Trace
}
Code doesn't look like it is formatted as code; not sure how to do that here.