private boolean generate(Painter painter, int resolutionX, int resolutionY, String filename, String imgformat) {
	  
	  // create a buffered image object using RGB+A pixel format 
    BufferedImage image = new BufferedImage(resolutionX, resolutionY, BufferedImage.TYPE_INT_ARGB);

    // Set a color for each pixel. Y pixel indices are flipped so that the origin (0,0)
    //   is at the bottom left of the image.
    for (int i = 0; i < resolutionX; i++)
      for (int j = 0; j < resolutionY; j++)
        image.setRGB( i, j, painter.pixelColorAt(i, resolutionY-j-1, resolutionX, resolutionY).toAwtColor() );

    // Write the image to disk.
    try {
      File file = new File(filename);
      ImageIO.write(image, imgformat, file);
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
    
    // success
    return true;
  }