//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 Solve { 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 xmin = -4; final int xmax = 4; final int ymin = -3; final int ymax = 3; final int m = 100; //magnification: how many pixels per unit Color black = new Color(display, 0, 0, 0); Color white = new Color(display, 255, 255, 255); for (int x = m * xmin; x <= m * xmax; ++x) { for (int y = m * ymin; y <= m * ymax; ++y) { Color color; // The last three arguments of Color are the amount of // red, green, blue: // minimum amount is zero, maximum is 255. if (x == 0 //Y axis is vertical || y == 0 //X axis is horizontal || x % m == 0 && Math.abs(y) < 10 //ticks along X axis || y % m == 0 && Math.abs(x) < 10 //ticks along Y axis ) { color = black; } else { // background color = white; } gc.setForeground(color); gc.drawPoint(x - m * xmin, y - m * ymin); } final int y = x * x * x - x * x; gc.setForeground(black); gc.drawPoint(x - m * xmin, y - m * ymin); } gc.dispose(); // don't need gc any more // Point B } }); shell.setText("Solve an equation"); shell.open(); // The following six lines are needed to close the window. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } }