Skip to content

Instantly share code, notes, and snippets.

@chl
Created March 19, 2010 22:03
Show Gist options
  • Save chl/338246 to your computer and use it in GitHub Desktop.
Save chl/338246 to your computer and use it in GitHub Desktop.
// start w/ ringo-web j2d
// j2d/config.js
exports.urls = [['/', 'actions']];
exports.app = require("ringo/webapp").handleRequest;
// j2d/actions.js
import("binary");
var awt = java.awt;
function Canvas(w, h) {
this.image = new awt.image.BufferedImage(
w, h,
BufferedImage.TYPE_INT_ARGB
);
this.graphics = this.image.createGraphics();
this.graphics.setRenderingHint(
awt.RenderingHints.KEY_ANTIALIASING,
awt.RenderingHints.VALUE_ANTIALIAS_ON
);
this.graphics.setRenderingHint(
awt.RenderingHints.KEY_RENDERING,
awt.RenderingHints.VALUE_RENDER_QUALITY
);
}
function ImageResponse(image, format, contentType) {
var format = format || "png";
var contentType = contentType || "image/" + format;
var out = new java.io.ByteArrayOutputStream();
Packages.javax.imageio.ImageIO.write(image, format, out);
return {
status: 200,
headers: {"Content-Type": contentType},
body: [new binary.ByteArray(out.toByteArray())]
};
}
exports.index = function(req) {
var {image, graphics} = new Canvas(100, 100);
with (graphics) {
setStroke(new awt.BasicStroke(5));
setColor(awt.Color.red);
drawLine(10, 10, 90, 90);
drawLine(10, 90, 90, 10);
}
return new ImageResponse(image, "png");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment