import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class PolylineBug { public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.addPaintListener(new PaintListener() { public void paintControl(PaintEvent event) { Rectangle rect = shell.getClientArea(); drawTwoLines(event.gc, rect); } }); shell.setBounds(100, 100, 200, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } protected static void drawTwoLines(GC gc, Rectangle rect) { // With advanced set to true performance is better and the machine // does not lock up while the lines are being drawn. gc.setAdvanced(false); int[] points = new int[rect.width * 8]; int[] points2 = new int[rect.width * 8]; int maxy = rect.height - 1; int miny = 0; // Create a line which zig-zags between min and max y. for (int i = 0; i < points.length - 3; i += 4) { int x = i / 8; points[i] = x; points[i + 1] = miny; points[i + 2] = x + 1; points[i + 3] = maxy; // Create the other line going the other way. points2[i] = x; points2[i + 1] = maxy; points2[i + 2] = x + 1; points2[i + 3] = miny; } int style = gc.getLineStyle(); Color color = gc.getForeground(); Color blue = new Color(gc.getDevice(), 0, 0, 128); Color green = new Color(gc.getDevice(), 0, 128, 0); long startTime = System.currentTimeMillis(); long endTime; System.err .println("If setAdvanced is false the machine may be non-responsive while the styled lines are drawn."); System.err .println("The effect is more noticeable the larger the window is."); System.err .println("Try moving the mouse around while the image is being drawn on a maximised window."); // If the line style is SWT.SOLID there is no problem. gc.setLineStyle(SWT.LINE_DASH); gc.setForeground(blue); gc.drawPolyline(points); endTime = System.currentTimeMillis(); System.err.println("First line complete."); System.err.println("First line took " + (endTime - startTime) + " milliseconds with " + (points.length / 2) + " points and gc.setAdvanced(" + gc.getAdvanced() + ")"); startTime = System.currentTimeMillis(); gc.setLineStyle(SWT.LINE_DOT); gc.setForeground(green); gc.drawPolyline(points2); endTime = System.currentTimeMillis(); System.err.println("Second line complete."); System.err.println("Second line took " + (endTime - startTime) + " milliseconds with " + (points.length / 2) + " points and gc.setAdvanced(" + gc.getAdvanced() + ")"); blue.dispose(); green.dispose(); gc.setLineStyle(style); gc.setForeground(color); } }