This is a simple example that creates a 30x30 cell:

public static void main(String... args) throws IOException { int w = 30, h = 30; // create the binary mapping byte BLACK = (byte)0, WHITE = (byte)255; byte[] map = {BLACK, WHITE}; IndexColorModel icm = new IndexColorModel(1, map.length, map, map, map); // create checkered data int[] data = new int[w*h]; for(int i=0; i<w; i++) for(int j=0; j<h; j++) data[i*h + j] = i%4<2 && j%4<2 || i%4>=2 && j%4>=2 ? BLACK:WHITE; // create image from color model and data WritableRaster raster = icm.createCompatibleWritableRaster(w, h); raster.setPixels(0, 0, w, h, data); BufferedImage bi = new BufferedImage(icm, raster, false, null); // output to a file ImageIO.write(bi, "jpg", new File("C:\\Users\\user\\Desktop\\test.jpg")); }
EDIT:
For what you do, you actually do not need to create your own ImageColorModel, you can use the built-in type: BufferedImage.TYPE_BYTE_GRAY or TYPE_BYTE_BINARY. Here is a better example and shows how to use shades of gray to get a checkered border:

public static void main(String... args) throws IOException { int w = 40, h = 40, divs = 5; BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY); WritableRaster raster = bi.getRaster(); for(int i=0; i<w; i++) for(int j=0; j<h; j++) raster.setSample(i,j,0,128+(int)(127*Math.sin(Math.PI*i/w*divs)*Math.sin(Math.PI*j/h*divs))); ImageIO.write(bi, "jpg", new File("C:\\Users\\user\\Desktop\\test.jpg")); }
source share