There’s been a bit of word done on rendering the images produced by Swing and AWT components as images. Specifically, Jacobus Steenkamp has authored an article over at java.net on Bringing Swing to the Web. I think it’s more interesting to port components over to Echo2 or ZK with all of their RI behavior, but for graph or charting components, the purely graphical result can still be quite useful.
Echo2 has the AwtImageReference class which allows one to add an java.awt.Image as an Echo2 component. There’s a few gotcha’s in Steenkamp’s example. First, on a Unix or Linux environment, you have to add "-Djava.awt.headless=true" to the java command line options of your Tomcat, or Jetty, or whatever app server, otherwise invoking AWT will cause the JVM to look for an X-server. The other tweak is adding in a line in Steenkamp’s utility SwingImageCreator class to avoid a bug in some 1.4 and 1.5 versions of the JVM.
public static BufferedImage createImage(JComponent component, int imageType) { component.setDoubleBuffered(false); // avoid bug Dimension componentSize = component.getPreferredSize(); component.setSize(componentSize); //Make sure these //are the same BufferedImage img = new BufferedImage(componentSize.width, componentSize.height, imageType); Graphics2D grap = img.createGraphics(); grap.fillRect(0,0,img.getWidth(),img.getHeight()); component.paint(grap); return img;}Now this thing is ready for primetime, i.e. it will actually work.
private void setupJGraph(java.awt.Color bg) { Image image; // create the graph GraphModel model = new DefaultGraphModel(); JGraph graph = new JGraph(model); graph.setEditable(false); // put the vertices in DefaultGraphCell[] cells = genVertices(bg); graph.getGraphLayoutCache().setSelectsAllInsertedCells(false); graph.getGraphLayoutCache().insert(cells); // render it image = SwingImageCreator.createImage(graph); AwtImageReference jgimage = new AwtImageReference(image); graphLabel.setIcon(jgimage); }I’ve got an example app here. It simply allows you to change the gradient colors of the vertices and advance the plot a few notches like an osciloscope. The Echo2 framework could use a way of preloading the image to avoid flicker, but so far so good.

