NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.
To learn about what's going on, see About the Archive Site.
Hi Lak et al,The other option is to make sure that you are only ever using a 2D display. Then it all works nicely in headless mode.
For example, using Ugo's P1_01 tutorial program, I can embed it in a Java Server Page (with a few minor mods, such as a "getDisplay" method, and making sure it calls the offscreen version of the DisplayImplJ2D contstructor).
In case others are interest, to embed it into a jsp I did the following:1) created a web page in the relevant tomcat directory called testDisplay.html with the single line:
<img src = createDisplay.jsp> 2) put createDisplay.jsp in the same directory (see below for listing) 3) then pointed a web browser at testDisplay.html Cheers, James createDisplay.jsp:<%@ page import="java.util.*,visad.*,visad.java2d.DisplayImplJ2D,test.P1_01" %>
<HTML> <BODY> <!-- Example of how to embed a Java expression --> Hello! The time is now <%= new Date() %> <!-- Example of block of Java code (scriptlet) --> <% P1_01 p1 = new P1_01(); DisplayImpl display = p1.getDisplay(); java.awt.image.BufferedImage buffImage = display.getImage(true); java.io.OutputStream output = response.getOutputStream(); javax.imageio.ImageIO.write(buffImage, "JPG", output); %>
</BODY> </HTML>
Valliappa Lakshmanan wrote:
A quick update for any one else who may find themselves in the same boat ... A poster on the Java3D list indicated that Java3D does not work in the headless mode (my hack below produced a GraphicsConfiguraiton that led to a ClassCastException deeper down inside Java3D). So, can you use VisAD from within servlets? Yes, but in a more round-about way. This is what I did: 1) I created a Java application that had a visible JFrame. The server dynamically creates images and writes them out to the webserver directory. 2) The servlet opens up a socket connection to the application, and passes on the request. The app creates the image, writes it out, and notifies the servlet via the same socket. 3) The servlet returns an image tag back to the client. This works, but as would be expected with off-screen rendering, it is quite slow. Lak Valliappa Lakshmanan wrote:Bill, Here's a hack that works -- read an image, and use the Graphics2D from that Image to get a GraphicsConfiguration. Perhaps, you can change one of the APIs to accept such a graphics configuration from the application, since you probably don't want the VisAD classes reading local files :) Thanks for your help. Lak // run as: java -Djava.awt.headless=true OffscreenImage import java.awt.*; import java.awt.image.*; import javax.imageio.*; import java.io.*; class OffscreenImage { public static void main(String[] args) throws Exception { BufferedImage template = ImageIO.read( new File("test.jpg") ); Graphics2D g = template.createGraphics(); GraphicsConfiguration config = g.getDeviceConfiguration(); if ( config != null ) System.out.println( "Got valid GraphicsConfiguration" ); } } Bill Hibbard wrote:Hi Lak, On Fri, 27 Aug 2004, Valliappa Lakshmanan wrote:Thanks, Ugo. I was not aware that you could specify a headless environment that way. I think this is the right way to go, because ultimately, I want to create images using VisAD within a servlet, and servlets are headless. I added -Djava.awt.headless=true to the invocation of the java VM when I ran my test program.I now get a InitializerException thrown from DisplayImplJ3D.initialize()because VisADConfig.makeConfig calls getDefaultScreenDevice() which throws a java.awt.HeadlessException. I'm thinking that: (a) if I'm creating an offscreen DisplayImplJ3D, it should not be asking for a default screen device.or (b) DisplayImplJ3D.initialize() should gracefully handle a headless case.Good idea. However, the VisADCanvasJ3D constructor needs a non-null GraphicsConfiguration to pass as an argument to the super() constructor for its parent Canvas3D class. I took a quick look at the GraphicsConfiguration, GraphicsDevice and GraphicsEnvironment JavaDocs but couldn't see an obvious way to construct a GraphicsConfiguration without a GraphicsDevice. If you or someone else can suggest a way, we can catch the java.awt.HeadlessException in VisADCanvasJ3D.makeConfig() and use your alternative. There is hopefully an easy way to do this that my quick look missed. Thanks, Bill
visad
archives: