package swtSnippet; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.opengl.GLCanvas; import org.eclipse.swt.opengl.GLData; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Shell; /** * This snippet serves two purposes:

* * Demonstrate Eclipse Bug #573782 - https://bugs.eclipse.org/bugs/show_bug.cgi?id=573782
* On Eclipse 4.14, using a GLCanvas, it is not redrawn after paintControl runs.

* * Demonstrate Eclipse Bug #570649 - https://bugs.eclipse.org/bugs/show_bug.cgi?id=570649
* On Eclipse 4.19, using a GLCanvas, it throws an error on the readAndDispatch call.

* * When it runs correctly, it opens a Shell with a red line that moves around when you move your mouse over * the canvas. */ public class SWTSnippet { public static void main( String[] args ) { Display display = new Display(); Shell shell = new Shell( display ); shell.setLayout( new FillLayout() ); shell.setBounds( 100, 100, 400, 400 ); // This does not work. Canvas canvas = new GLCanvas( shell, SWT.NONE, new GLData() ); // This works. // Canvas canvas = new Canvas( shell, SWT.NONE ); canvas.addListener( SWT.Paint, event -> handlePaint( event ) ); canvas.addListener( SWT.MouseMove, event -> canvas.redraw() ); shell.open(); while ( !shell.isDisposed() ) { if ( !display.readAndDispatch() ) { display.sleep(); } } display.dispose(); } private static void handlePaint( Event event ) { int xBegin = 10 + (int) ( Math.random() * 10 ); int xEnd = 390 - (int) ( Math.random() * 10 ); int yBegin = 10 + (int) ( Math.random() * 10 ); int yEnd = 350 - (int) ( Math.random() * 10 ); event.gc.setLineWidth( 3 ); event.gc.setForeground( Display.getDefault().getSystemColor( SWT.COLOR_RED ) ); event.gc.drawLine( xBegin, yBegin, xEnd, yEnd ); } }