I have java 1.4 application that is running on server. I need to calculate the width of strings before I create pdf files that include them. When running the app locally (on my pc) it is o.k. , but when I run it on a server I get an error :
Exception in thread "main" java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY
variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:77)
at java.lang.Class.forName1(Native Method)
at java.lang.Class.forName(Class.java:142)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:72)
at java.awt.Font.initializeFont(Font.java:285)
at java.awt.Font.<init>(Font.java:319)
the code that create the problem calculate the width of a string as follows :
protected static String mCutStr(String iText , int iWidthPt , int iEstimatedNumOfChars)
{
iWidthPt = iWidthPt-4; //remove padding
AffineTransform affinetransform = new AffineTransform();
FontRenderContext frc = new FontRenderContext(affinetransform,true,true);
Font font = new Font("Times", Font.PLAIN, 9);
int textwidth = (int)(font.getStringBounds(iText, frc).getWidth());
// System.out.println("iText="+iText+"; textwidth = "+textwidth+" iWidthPt ="+iWidthPt);
if(textwidth <= iWidthPt)
return iText;
String vTestStr = iText.substring(0 , iEstimatedNumOfChars)+">>";
textwidth = (int)(font.getStringBounds(vTestStr, frc).getWidth());
while(textwidth < iWidthPt)
{
iEstimatedNumOfChars++;
vTestStr = iText.substring(0 , iEstimatedNumOfChars)+">>";
textwidth = (int)(font.getStringBounds(vTestStr, frc).getWidth());
// System.out.println("vTestStr="+vTestStr+" textwidth = "+textwidth);
}
vTestStr = iText.substring(0 , iEstimatedNumOfChars-1)+">>";
return vTestStr;
}
Is there an other way to calc this without using AWT? (it must support minimum java 5)