Rendering images with Processing.org on Java servl

2020-03-26 04:08发布

问题:

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);

  }
}

回答1:

I've just posted some example code here, which sounds like it would solve your problem too, take a look. It's Java, not Scala, but the conversion should be trivial.



回答2:

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.



回答3:

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.