package ie.dcu.swt; import java.util.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.widgets.*; /** * Create cursors. * * @author Kevin McGuinness */ public class CursorFactory { /** * Create a cross-hair cursor. * * @return The newly created cursor. */ public static Cursor createCrosshairCursor() { // Size of cursor final int s = 24; // Number of points in cursor final int n = s*s; // Midpoint of cursor final int m = s/2; // Color indices final byte black = 1; final byte light = 2; final byte trans = 0; // Cursor array byte[] cursor = new byte[n]; // Flood with transparency Arrays.fill(cursor, (byte) 0); // Set crosshair pixels for (int i = 0; i < s; i++) { // Choose color byte color = (i == m-2 || i == m+2) ? light : black; // Set pixels if (i < m-1 || i > m+1) { int idx1 = i+m*s; int idx2 = m+i*s; cursor[idx1] = (byte) color; cursor[idx2] = (byte) color; } } // Set center pixel cursor[m+m*s] = black; // Create colors RGB[] colors = new RGB[] { new RGB(255,255,255), new RGB(0,0,0), new RGB(128,128,128) }; // Create palette PaletteData palette = new PaletteData(colors); // Create image ImageData image = new ImageData(s, s, 4, palette); image.transparentPixel = trans; image.setPixels(0, 0, n, cursor, 0); // Create cursor return new Cursor(Display.getCurrent(), image, s/2, s/2); } }