// Run with
//   java Example7 <size>

// Generate SVG of this form (for size = 2):
// <?xml version="1.0" encoding="UTF-8"?>
// <svg viewBox="0 0 40 40">
//   <g>
//     <rect fill="red" x="0" y="0" width="20" height="20" />
//     <rect fill="blue" x="20" y="0" width="20" height="20" />
//     <rect fill="blue" x="0" y="20" width="20" height="20" />
//     <rect fill="red" x="20" y="20" width="20" height="20" />
//   </g>
// </svg>

import java.io.*; 
import org.jdom.*; 
import org.jdom.output.*; 

class Example7 {
  public static void main(String[] args) 
    throws IOException, JDOMException {
    if (args.length != 1) 
      System.out.println("Usage: Example4 <size>\n");
    else {
      int size = Integer.parseInt(args[0]);
      Element svgElement = new Element("svg");
      svgElement.setAttribute("viewBox", "0 0 " + 20*size + " " + 20 * size);
      Element g = new Element("g");
      svgElement.addContent(g);
      for (int row=0; row<size; row++) {
	for (int col=0; col<size; col++) {
	  Element rectElement = new Element("rect");
	  String color = (row+col)%2==0 ? "red" : "blue";
	  rectElement.setAttribute("fill", color);
	  rectElement.setAttribute("x", Integer.toString(col*20));
	  rectElement.setAttribute("y", Integer.toString(row*20));
	  rectElement.setAttribute("width", "20");
	  rectElement.setAttribute("height", "20");
	  g.addContent(rectElement);
	}
      }
      Document doc = new Document(svgElement); 
      new XMLOutputter(Format.getPrettyFormat()).output(doc, System.out);
    }
  }
}

