3

How to render Processing.org images on Java servlet?

My scala code is:

class Image extends PApplet {
  override def setup {
    size(200,200)
    background(0)
  }

  override def draw {
    stroke(255)
    line(10,10,50,50)
  }

  def renderImage = g.getImage

}


class ImageServlet extends HttpServlet {
  override def doGet(request: HttpServletRequest, response: HttpServletResponse) {

    response.setContentType("image/gif")

    val os: OutputStream = response.getOutputStream
    val image = new Image

    javax.imageio.ImageIO.write(image.renderImage.asInstanceOf[RenderedImage],"GIF86", os);

  }
}
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
Rafał Sobota
  • 1,468
  • 1
  • 17
  • 32

2 Answers2

0

Applets are usually executed client-side (i.e. inside a browser). If you simply call new Image the plumbing around it, for example calling setup() won't be executed.

Perhaps try some of the lower level Processing.org API classes. I don't know the API but PGraphics or one of its subclasses look promising.

leonm
  • 6,454
  • 30
  • 38
0

You can get processing to render from a servlet but unless you have a monitor plugged into your server you will get "headless" exceptions. Checkout ServletUtils from Fluid Forms Libs.

If your server doesn't have a screen plugged in you can still instantiate any of Processings PGraphics classes. PApplet, the base class for processing applets, basically passes all drawing API, calls such as rect(), onto a PGraphics class.

Onato
  • 9,916
  • 5
  • 46
  • 54