//Print the flag by pressing the "Print Screen" key; then paste the //clipboard into another document. //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 FlagSWT { 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); // Everything from Point A to Point B will be executed // whenever the window needs to be redrawn. shell.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { // Point A GC gc = e.gc; // Graphics Context: the pen you draw with final int nrows = 200; // number of rows of pixels final int ncols = 300; // number of columns of pixels for (int row = 0; row < nrows; ++row) { for (int col = 0; col < ncols; ++col) { Color color; // The last three arguments of Color are the amount of // red, green, blue: // minimum amount is zero, maximum is 255. // The && means "and". if (row < nrows / 2 && col < ncols / 2) { // blue union jack in upper left corner. color = new Color(display, 0, 0, 255); } else if (row % 20 < 10) { // red horizontal stripe color = new Color(display, 255, 0, 0); } else { // white horizontal stripe color = new Color(display, 255, 255, 255); } gc.setForeground(color); // Change the pixel at location col, row to the // foreground color. gc.drawPoint(col, row); color.dispose(); // don't need this color any more } } gc.dispose(); // don't need gc any more // Point B } }); shell.setText("American flag"); shell.open(); // The following six lines are needed to close the window. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } }