- We design and build extraordinary applications for companies looking to make the next great idea a reality.
- learn more
Swing Components in Echo2
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.
Topics: Ajax Frameworks, Echo2, Frameworks
Leave a comment
About Pathfinder
Recent
- IE8 Beta 2 Released
- Faster JavaScript for Firefox 3.1 Thru JIT
- Implementing linked multiselects with jQuery, LiveQuery, and Low Pro: Part 2: First pass at the actual code
- I’m Cranky Because I’m Not Getting Enough REST
- Flex Gauge Component Example with source
- Plugging Some Cool Tools
- Implementing linked multiselects with jQuery, LiveQuery, and Low Pro: Part 1: Requirements and interaction design
- Many Varied Components, or… Multi Variable Complexity, or… Mainly Vanilla Coding
- Custom Flex 3 Lightweight Preloader with source code
- Mass Assigning Inheritance Column Values for ActiveRecord STI with Rails
Archives
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006


