//The following two lines are needed to open and close a window. import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; //The following two lines are needed to let the window repaint itself. import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; //The following two lines are needed for what we want to draw in the window. import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.GC; public class For2SWT { public static void main(String[] args) { //The following two lines are needed to open the window. final Display display = new Display(); final Shell shell = new Shell(display); shell.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { GC gc = e.gc; //Graphics Context Color color = new Color(display, 0, 0, 0); //rgb gc.setBackground(color); for (int row = 0; row < 10; ++row) { for (int col = 0; col < 10; ++col) { gc.fillRectangle(25 * col, 25 * row, 20, 20); } } //Don't need color or gc any more. color.dispose(); gc.dispose(); } }); shell.setText("10 rows and 10 columns of squares"); shell.open(); //The following six lines are needed to close the window. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } }