//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 DominicanSWT { 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 width = 400; //in pixels final int height = width * 5 / 8; //in pixels final int stripe = height / 5; //width of white stripes, in pixels //Convenient to put the origin (0, 0) at center of flag. final int ymax = height / 2; final int ymin = ymax - height; final int xmax = width / 2; final int xmin = xmax - width; final int s = stripe / 2; final int radius = s; //of yellow circle in center for (int x = xmin; x < xmax; ++x) { for (int y = ymin; y < ymax; ++y) { Color color; // The last three arguments of Color are the amount of // red, green, blue: // minimum amount is zero, maximum is 255. // && means "and", || means "or". if (Math.hypot(x, y) < radius) { //could also say if (Math.sqrt(x * x + y * y) < radius) { //yellow circle in center color = new Color(display, 255, 255, 0); } else if (Math.abs(x) < s || Math.abs(y) < s) { //white horizontal an vertical stripes color = new Color(display, 255, 255, 255); } else if (x > 0 && y > 0 || x < 0 && y < 0) { //upper right or lower left: red color = new Color(display, 255, 0, 0); } else { //lower right or upper left: blue color = new Color(display, 0, 0, 255); } gc.setForeground(color); /* Change the pixel at location x, y to the foreground color. Our x's run from xmin (on the left) to xmax-1 (on the right). But the first argument of drawPoint must run from zero (on the left) to width-1 (on the right), so we subtract xmin. Our y's run from ymin (on the bottom) to ymax-1 (on the top). But the second argument of drawPoint must run from zero (on the top) to height-1 (on the bottom), so we add height and ymin to -y. */ gc.drawPoint(x - xmin, height - y + ymin); color.dispose(); // don't need this color any more } } gc.dispose(); // don't need gc any more // Point B } }); shell.setText("Dominican flag"); shell.open(); // The following six lines are needed to close the window. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } }