Login  Register

Re: Patch for ImageJ that fixes a HeadlessException error

Posted by Victor Petrov on Jul 19, 2007; 4:25am
URL: http://imagej.273.s1.nabble.com/Patch-for-ImageJ-that-fixes-a-HeadlessException-error-tp3698809p3698812.html

Dear Johannes,

Thank you for pointing that out. Originally, setupFrame() was using the
global variable "graphics", which was a placeholder for the Graphics object.
I guess in the process of testing, I changed setupFrame() to use a local
Graphics g variable and forgot to change it back.
I wasn't using the Antialiased option so I didn't have any issues due to
graphics being null.
I've changed the setupFrame function to use the global "graphics" variable
and here's the patch for it (apparently, my diff doesn't support the --git
option):

------------------------------------------------------------------------------------------------------------------------------------------------------------
$ diff -Naur ../ImageJ-Source-1.37v/ij/process/ImageProcessor.java
ij/process/ImageProcessor.java
------------------------------------------------------------------------------------------------------------------------------------------------------------
--- original/ij/process/ImageProcessor.java       2006-08-24 16:09:
16.000000000 -0400
+++ new/ij/process/ImageProcessor.java      2007-07-18 23:09:34.000000000-0400
@@ -3,6 +3,7 @@
 import java.util.*;
 import java.awt.*;
 import java.awt.image.*;
+import java.awt.FontMetrics;
 import java.lang.reflect.*;
 import ij.gui.*;
 import ij.util.Java2;
@@ -44,6 +45,7 @@
        protected boolean antialiasedText;
        protected boolean boldFont;
        static Frame frame;
+       static Graphics graphics;

     ProgressBar progressBar;
     boolean pixelsModified;
@@ -856,6 +858,21 @@
     private ImageProcessor dotMask;

        private void setupFrame() {
+
+               if (System.getProperty("java.awt.headless
").equalsIgnoreCase("true"))
+               {
+                       if (image==null)
+                               image=new BufferedImage(img.getWidth(null),
img.getHeight(null),BufferedImage.TYPE_INT_RGB);
+
+                       if (font==null)
+                               font = new Font("SansSerif", Font.PLAIN,
12);
+                       if (fontMetrics==null)
+                       {
+                               graphics=image.getGraphics();
+                               fontMetrics=graphics.getFontMetrics(font);
+                       }
+                       return;
+               }
                if (frame==null) {
                        frame = new Frame();
                        frame.pack();
@@ -873,9 +890,12 @@
        public void drawString(String s) {
                if (s.equals(""))
                        return;
+               //If running in headless mode, setupFrame generates
HeadlessException,
+               //therefore setupFrame should only run when
java.awt.headless is not defined
                setupFrame();
                if (ij.IJ.isMacOSX())
                        s += " ";
+
                int w =  getStringWidth(s);
                int cxx = cx;
                if (justification==CENTER_JUSTIFY)
@@ -885,10 +905,11 @@
                int h =  fontMetrics.getHeight();
                if (w<=0 || h<=0) return;
                Image img;
-               if (ij.IJ.isLinux() && ij.IJ.isJava2())
+               if ((ij.IJ.isLinux() && ij.IJ.isJava2()) || (
System.getProperty("java.awt.headless").equalsIgnoreCase("true")))
                        img = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
                else
                        img = frame.createImage(w, h);
+
                Graphics g = img.getGraphics();
                FontMetrics metrics = g.getFontMetrics(font);
                int fontHeight = metrics.getHeight();
@@ -972,7 +993,10 @@
                setupFrame();
                int w;
                if (antialiasedText) {
-                       Graphics g = frame.getGraphics();
+                       //Graphics g = frame.getGraphics();
+                       //-BEGIN-CHANGE
+                       Graphics g=graphics;
+                       //-END-CHANGE
                        if (g==null) {
                                frame = null;
                                setupFrame();

-----------------------------------------------------------------------------------

I hope this helps.

Sincerely,
Victor Petrov